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 const char *__attribute_const__
rdma_event_msg(enum rdma_cm_event_type event
)
75 return (index
< ARRAY_SIZE(cma_events
) && cma_events
[index
]) ?
76 cma_events
[index
] : "unrecognized event";
78 EXPORT_SYMBOL(rdma_event_msg
);
80 const char *__attribute_const__
rdma_reject_msg(struct rdma_cm_id
*id
,
83 if (rdma_ib_or_roce(id
->device
, id
->port_num
))
84 return ibcm_reject_msg(reason
);
86 if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
87 return iwcm_reject_msg(reason
);
90 return "unrecognized transport";
92 EXPORT_SYMBOL(rdma_reject_msg
);
94 bool rdma_is_consumer_reject(struct rdma_cm_id
*id
, int reason
)
96 if (rdma_ib_or_roce(id
->device
, id
->port_num
))
97 return reason
== IB_CM_REJ_CONSUMER_DEFINED
;
99 if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
100 return reason
== -ECONNREFUSED
;
105 EXPORT_SYMBOL(rdma_is_consumer_reject
);
107 const void *rdma_consumer_reject_data(struct rdma_cm_id
*id
,
108 struct rdma_cm_event
*ev
, u8
*data_len
)
112 if (rdma_is_consumer_reject(id
, ev
->status
)) {
113 *data_len
= ev
->param
.conn
.private_data_len
;
114 p
= ev
->param
.conn
.private_data
;
121 EXPORT_SYMBOL(rdma_consumer_reject_data
);
124 * rdma_iw_cm_id() - return the iw_cm_id pointer for this cm_id.
125 * @id: Communication Identifier
127 struct iw_cm_id
*rdma_iw_cm_id(struct rdma_cm_id
*id
)
129 struct rdma_id_private
*id_priv
;
131 id_priv
= container_of(id
, struct rdma_id_private
, id
);
132 if (id
->device
->node_type
== RDMA_NODE_RNIC
)
133 return id_priv
->cm_id
.iw
;
136 EXPORT_SYMBOL(rdma_iw_cm_id
);
139 * rdma_res_to_id() - return the rdma_cm_id pointer for this restrack.
140 * @res: rdma resource tracking entry pointer
142 struct rdma_cm_id
*rdma_res_to_id(struct rdma_restrack_entry
*res
)
144 struct rdma_id_private
*id_priv
=
145 container_of(res
, struct rdma_id_private
, res
);
149 EXPORT_SYMBOL(rdma_res_to_id
);
151 static void cma_add_one(struct ib_device
*device
);
152 static void cma_remove_one(struct ib_device
*device
, void *client_data
);
154 static struct ib_client cma_client
= {
157 .remove
= cma_remove_one
160 static struct ib_sa_client sa_client
;
161 static LIST_HEAD(dev_list
);
162 static LIST_HEAD(listen_any_list
);
163 static DEFINE_MUTEX(lock
);
164 static struct workqueue_struct
*cma_wq
;
165 static unsigned int cma_pernet_id
;
168 struct xarray tcp_ps
;
169 struct xarray udp_ps
;
170 struct xarray ipoib_ps
;
174 static struct cma_pernet
*cma_pernet(struct net
*net
)
176 return net_generic(net
, cma_pernet_id
);
180 struct xarray
*cma_pernet_xa(struct net
*net
, enum rdma_ucm_port_space ps
)
182 struct cma_pernet
*pernet
= cma_pernet(net
);
186 return &pernet
->tcp_ps
;
188 return &pernet
->udp_ps
;
190 return &pernet
->ipoib_ps
;
192 return &pernet
->ib_ps
;
199 struct list_head list
;
200 struct ib_device
*device
;
201 struct completion comp
;
203 struct list_head id_list
;
204 enum ib_gid_type
*default_gid_type
;
205 u8
*default_roce_tos
;
208 struct rdma_bind_list
{
209 enum rdma_ucm_port_space ps
;
210 struct hlist_head owners
;
214 struct class_port_info_context
{
215 struct ib_class_port_info
*class_port_info
;
216 struct ib_device
*device
;
217 struct completion done
;
218 struct ib_sa_query
*sa_query
;
222 static int cma_ps_alloc(struct net
*net
, enum rdma_ucm_port_space ps
,
223 struct rdma_bind_list
*bind_list
, int snum
)
225 struct xarray
*xa
= cma_pernet_xa(net
, ps
);
227 return xa_insert(xa
, snum
, bind_list
, GFP_KERNEL
);
230 static struct rdma_bind_list
*cma_ps_find(struct net
*net
,
231 enum rdma_ucm_port_space ps
, int snum
)
233 struct xarray
*xa
= cma_pernet_xa(net
, ps
);
235 return xa_load(xa
, snum
);
238 static void cma_ps_remove(struct net
*net
, enum rdma_ucm_port_space ps
,
241 struct xarray
*xa
= cma_pernet_xa(net
, ps
);
250 void cma_dev_get(struct cma_device
*cma_dev
)
252 refcount_inc(&cma_dev
->refcount
);
255 void cma_dev_put(struct cma_device
*cma_dev
)
257 if (refcount_dec_and_test(&cma_dev
->refcount
))
258 complete(&cma_dev
->comp
);
261 struct cma_device
*cma_enum_devices_by_ibdev(cma_device_filter filter
,
264 struct cma_device
*cma_dev
;
265 struct cma_device
*found_cma_dev
= NULL
;
269 list_for_each_entry(cma_dev
, &dev_list
, list
)
270 if (filter(cma_dev
->device
, cookie
)) {
271 found_cma_dev
= cma_dev
;
276 cma_dev_get(found_cma_dev
);
278 return found_cma_dev
;
281 int cma_get_default_gid_type(struct cma_device
*cma_dev
,
284 if (!rdma_is_port_valid(cma_dev
->device
, port
))
287 return cma_dev
->default_gid_type
[port
- rdma_start_port(cma_dev
->device
)];
290 int cma_set_default_gid_type(struct cma_device
*cma_dev
,
292 enum ib_gid_type default_gid_type
)
294 unsigned long supported_gids
;
296 if (!rdma_is_port_valid(cma_dev
->device
, port
))
299 supported_gids
= roce_gid_type_mask_support(cma_dev
->device
, port
);
301 if (!(supported_gids
& 1 << default_gid_type
))
304 cma_dev
->default_gid_type
[port
- rdma_start_port(cma_dev
->device
)] =
310 int cma_get_default_roce_tos(struct cma_device
*cma_dev
, unsigned int port
)
312 if (!rdma_is_port_valid(cma_dev
->device
, port
))
315 return cma_dev
->default_roce_tos
[port
- rdma_start_port(cma_dev
->device
)];
318 int cma_set_default_roce_tos(struct cma_device
*cma_dev
, unsigned int port
,
321 if (!rdma_is_port_valid(cma_dev
->device
, port
))
324 cma_dev
->default_roce_tos
[port
- rdma_start_port(cma_dev
->device
)] =
329 struct ib_device
*cma_get_ib_dev(struct cma_device
*cma_dev
)
331 return cma_dev
->device
;
335 * Device removal can occur at anytime, so we need extra handling to
336 * serialize notifying the user of device removal with other callbacks.
337 * We do this by disabling removal notification while a callback is in process,
338 * and reporting it after the callback completes.
341 struct cma_multicast
{
342 struct rdma_id_private
*id_priv
;
344 struct ib_sa_multicast
*ib
;
346 struct list_head list
;
348 struct sockaddr_storage addr
;
354 struct work_struct work
;
355 struct rdma_id_private
*id
;
356 enum rdma_cm_state old_state
;
357 enum rdma_cm_state new_state
;
358 struct rdma_cm_event event
;
361 struct cma_ndev_work
{
362 struct work_struct work
;
363 struct rdma_id_private
*id
;
364 struct rdma_cm_event event
;
367 struct iboe_mcast_work
{
368 struct work_struct work
;
369 struct rdma_id_private
*id
;
370 struct cma_multicast
*mc
;
383 u8 ip_version
; /* IP version: 7:4 */
385 union cma_ip_addr src_addr
;
386 union cma_ip_addr dst_addr
;
389 #define CMA_VERSION 0x00
391 struct cma_req_info
{
392 struct sockaddr_storage listen_addr_storage
;
393 struct sockaddr_storage src_addr_storage
;
394 struct ib_device
*device
;
395 union ib_gid local_gid
;
402 static int cma_comp(struct rdma_id_private
*id_priv
, enum rdma_cm_state comp
)
407 spin_lock_irqsave(&id_priv
->lock
, flags
);
408 ret
= (id_priv
->state
== comp
);
409 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
413 static int cma_comp_exch(struct rdma_id_private
*id_priv
,
414 enum rdma_cm_state comp
, enum rdma_cm_state exch
)
419 spin_lock_irqsave(&id_priv
->lock
, flags
);
420 if ((ret
= (id_priv
->state
== comp
)))
421 id_priv
->state
= exch
;
422 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
426 static enum rdma_cm_state
cma_exch(struct rdma_id_private
*id_priv
,
427 enum rdma_cm_state exch
)
430 enum rdma_cm_state old
;
432 spin_lock_irqsave(&id_priv
->lock
, flags
);
433 old
= id_priv
->state
;
434 id_priv
->state
= exch
;
435 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
439 static inline u8
cma_get_ip_ver(const struct cma_hdr
*hdr
)
441 return hdr
->ip_version
>> 4;
444 static inline void cma_set_ip_ver(struct cma_hdr
*hdr
, u8 ip_ver
)
446 hdr
->ip_version
= (ip_ver
<< 4) | (hdr
->ip_version
& 0xF);
449 static int cma_igmp_send(struct net_device
*ndev
, union ib_gid
*mgid
, bool join
)
451 struct in_device
*in_dev
= NULL
;
455 in_dev
= __in_dev_get_rtnl(ndev
);
458 ip_mc_inc_group(in_dev
,
459 *(__be32
*)(mgid
->raw
+ 12));
461 ip_mc_dec_group(in_dev
,
462 *(__be32
*)(mgid
->raw
+ 12));
466 return (in_dev
) ? 0 : -ENODEV
;
469 static void _cma_attach_to_dev(struct rdma_id_private
*id_priv
,
470 struct cma_device
*cma_dev
)
472 cma_dev_get(cma_dev
);
473 id_priv
->cma_dev
= cma_dev
;
474 id_priv
->id
.device
= cma_dev
->device
;
475 id_priv
->id
.route
.addr
.dev_addr
.transport
=
476 rdma_node_get_transport(cma_dev
->device
->node_type
);
477 list_add_tail(&id_priv
->list
, &cma_dev
->id_list
);
478 if (id_priv
->res
.kern_name
)
479 rdma_restrack_kadd(&id_priv
->res
);
481 rdma_restrack_uadd(&id_priv
->res
);
484 static void cma_attach_to_dev(struct rdma_id_private
*id_priv
,
485 struct cma_device
*cma_dev
)
487 _cma_attach_to_dev(id_priv
, cma_dev
);
489 cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
490 rdma_start_port(cma_dev
->device
)];
493 static inline void release_mc(struct kref
*kref
)
495 struct cma_multicast
*mc
= container_of(kref
, struct cma_multicast
, mcref
);
497 kfree(mc
->multicast
.ib
);
501 static void cma_release_dev(struct rdma_id_private
*id_priv
)
504 list_del(&id_priv
->list
);
505 cma_dev_put(id_priv
->cma_dev
);
506 id_priv
->cma_dev
= NULL
;
510 static inline struct sockaddr
*cma_src_addr(struct rdma_id_private
*id_priv
)
512 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
515 static inline struct sockaddr
*cma_dst_addr(struct rdma_id_private
*id_priv
)
517 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.dst_addr
;
520 static inline unsigned short cma_family(struct rdma_id_private
*id_priv
)
522 return id_priv
->id
.route
.addr
.src_addr
.ss_family
;
525 static int cma_set_qkey(struct rdma_id_private
*id_priv
, u32 qkey
)
527 struct ib_sa_mcmember_rec rec
;
531 if (qkey
&& id_priv
->qkey
!= qkey
)
537 id_priv
->qkey
= qkey
;
541 switch (id_priv
->id
.ps
) {
544 id_priv
->qkey
= RDMA_UDP_QKEY
;
547 ib_addr_get_mgid(&id_priv
->id
.route
.addr
.dev_addr
, &rec
.mgid
);
548 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
,
549 id_priv
->id
.port_num
, &rec
.mgid
,
552 id_priv
->qkey
= be32_to_cpu(rec
.qkey
);
560 static void cma_translate_ib(struct sockaddr_ib
*sib
, struct rdma_dev_addr
*dev_addr
)
562 dev_addr
->dev_type
= ARPHRD_INFINIBAND
;
563 rdma_addr_set_sgid(dev_addr
, (union ib_gid
*) &sib
->sib_addr
);
564 ib_addr_set_pkey(dev_addr
, ntohs(sib
->sib_pkey
));
567 static int cma_translate_addr(struct sockaddr
*addr
, struct rdma_dev_addr
*dev_addr
)
571 if (addr
->sa_family
!= AF_IB
) {
572 ret
= rdma_translate_ip(addr
, dev_addr
);
574 cma_translate_ib((struct sockaddr_ib
*) addr
, dev_addr
);
581 static const struct ib_gid_attr
*
582 cma_validate_port(struct ib_device
*device
, u8 port
,
583 enum ib_gid_type gid_type
,
585 struct rdma_id_private
*id_priv
)
587 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
588 int bound_if_index
= dev_addr
->bound_dev_if
;
589 const struct ib_gid_attr
*sgid_attr
;
590 int dev_type
= dev_addr
->dev_type
;
591 struct net_device
*ndev
= NULL
;
593 if (!rdma_dev_access_netns(device
, id_priv
->id
.route
.addr
.dev_addr
.net
))
594 return ERR_PTR(-ENODEV
);
596 if ((dev_type
== ARPHRD_INFINIBAND
) && !rdma_protocol_ib(device
, port
))
597 return ERR_PTR(-ENODEV
);
599 if ((dev_type
!= ARPHRD_INFINIBAND
) && rdma_protocol_ib(device
, port
))
600 return ERR_PTR(-ENODEV
);
602 if (dev_type
== ARPHRD_ETHER
&& rdma_protocol_roce(device
, port
)) {
603 ndev
= dev_get_by_index(dev_addr
->net
, bound_if_index
);
605 return ERR_PTR(-ENODEV
);
607 gid_type
= IB_GID_TYPE_IB
;
610 sgid_attr
= rdma_find_gid_by_port(device
, gid
, gid_type
, port
, ndev
);
616 static void cma_bind_sgid_attr(struct rdma_id_private
*id_priv
,
617 const struct ib_gid_attr
*sgid_attr
)
619 WARN_ON(id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
);
620 id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
= sgid_attr
;
624 * cma_acquire_dev_by_src_ip - Acquire cma device, port, gid attribute
625 * based on source ip address.
626 * @id_priv: cm_id which should be bound to cma device
628 * cma_acquire_dev_by_src_ip() binds cm id to cma device, port and GID attribute
629 * based on source IP address. It returns 0 on success or error code otherwise.
630 * It is applicable to active and passive side cm_id.
632 static int cma_acquire_dev_by_src_ip(struct rdma_id_private
*id_priv
)
634 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
635 const struct ib_gid_attr
*sgid_attr
;
636 union ib_gid gid
, iboe_gid
, *gidp
;
637 struct cma_device
*cma_dev
;
638 enum ib_gid_type gid_type
;
642 if (dev_addr
->dev_type
!= ARPHRD_INFINIBAND
&&
643 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
646 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
649 memcpy(&gid
, dev_addr
->src_dev_addr
+
650 rdma_addr_gid_offset(dev_addr
), sizeof(gid
));
653 list_for_each_entry(cma_dev
, &dev_list
, list
) {
654 rdma_for_each_port (cma_dev
->device
, port
) {
655 gidp
= rdma_protocol_roce(cma_dev
->device
, port
) ?
657 gid_type
= cma_dev
->default_gid_type
[port
- 1];
658 sgid_attr
= cma_validate_port(cma_dev
->device
, port
,
659 gid_type
, gidp
, id_priv
);
660 if (!IS_ERR(sgid_attr
)) {
661 id_priv
->id
.port_num
= port
;
662 cma_bind_sgid_attr(id_priv
, sgid_attr
);
663 cma_attach_to_dev(id_priv
, cma_dev
);
675 * cma_ib_acquire_dev - Acquire cma device, port and SGID attribute
676 * @id_priv: cm id to bind to cma device
677 * @listen_id_priv: listener cm id to match against
678 * @req: Pointer to req structure containaining incoming
679 * request information
680 * cma_ib_acquire_dev() acquires cma device, port and SGID attribute when
681 * rdma device matches for listen_id and incoming request. It also verifies
682 * that a GID table entry is present for the source address.
683 * Returns 0 on success, or returns error code otherwise.
685 static int cma_ib_acquire_dev(struct rdma_id_private
*id_priv
,
686 const struct rdma_id_private
*listen_id_priv
,
687 struct cma_req_info
*req
)
689 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
690 const struct ib_gid_attr
*sgid_attr
;
691 enum ib_gid_type gid_type
;
694 if (dev_addr
->dev_type
!= ARPHRD_INFINIBAND
&&
695 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
698 if (rdma_protocol_roce(req
->device
, req
->port
))
699 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
702 memcpy(&gid
, dev_addr
->src_dev_addr
+
703 rdma_addr_gid_offset(dev_addr
), sizeof(gid
));
705 gid_type
= listen_id_priv
->cma_dev
->default_gid_type
[req
->port
- 1];
706 sgid_attr
= cma_validate_port(req
->device
, req
->port
,
707 gid_type
, &gid
, id_priv
);
708 if (IS_ERR(sgid_attr
))
709 return PTR_ERR(sgid_attr
);
711 id_priv
->id
.port_num
= req
->port
;
712 cma_bind_sgid_attr(id_priv
, sgid_attr
);
713 /* Need to acquire lock to protect against reader
714 * of cma_dev->id_list such as cma_netdev_callback() and
715 * cma_process_remove().
718 cma_attach_to_dev(id_priv
, listen_id_priv
->cma_dev
);
723 static int cma_iw_acquire_dev(struct rdma_id_private
*id_priv
,
724 const struct rdma_id_private
*listen_id_priv
)
726 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
727 const struct ib_gid_attr
*sgid_attr
;
728 struct cma_device
*cma_dev
;
729 enum ib_gid_type gid_type
;
734 if (dev_addr
->dev_type
!= ARPHRD_INFINIBAND
&&
735 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
738 memcpy(&gid
, dev_addr
->src_dev_addr
+
739 rdma_addr_gid_offset(dev_addr
), sizeof(gid
));
743 cma_dev
= listen_id_priv
->cma_dev
;
744 port
= listen_id_priv
->id
.port_num
;
745 gid_type
= listen_id_priv
->gid_type
;
746 sgid_attr
= cma_validate_port(cma_dev
->device
, port
,
747 gid_type
, &gid
, id_priv
);
748 if (!IS_ERR(sgid_attr
)) {
749 id_priv
->id
.port_num
= port
;
750 cma_bind_sgid_attr(id_priv
, sgid_attr
);
755 list_for_each_entry(cma_dev
, &dev_list
, list
) {
756 rdma_for_each_port (cma_dev
->device
, port
) {
757 if (listen_id_priv
->cma_dev
== cma_dev
&&
758 listen_id_priv
->id
.port_num
== port
)
761 gid_type
= cma_dev
->default_gid_type
[port
- 1];
762 sgid_attr
= cma_validate_port(cma_dev
->device
, port
,
763 gid_type
, &gid
, id_priv
);
764 if (!IS_ERR(sgid_attr
)) {
765 id_priv
->id
.port_num
= port
;
766 cma_bind_sgid_attr(id_priv
, sgid_attr
);
775 cma_attach_to_dev(id_priv
, cma_dev
);
782 * Select the source IB device and address to reach the destination IB address.
784 static int cma_resolve_ib_dev(struct rdma_id_private
*id_priv
)
786 struct cma_device
*cma_dev
, *cur_dev
;
787 struct sockaddr_ib
*addr
;
788 union ib_gid gid
, sgid
, *dgid
;
791 enum ib_port_state port_state
;
795 addr
= (struct sockaddr_ib
*) cma_dst_addr(id_priv
);
796 dgid
= (union ib_gid
*) &addr
->sib_addr
;
797 pkey
= ntohs(addr
->sib_pkey
);
800 list_for_each_entry(cur_dev
, &dev_list
, list
) {
801 rdma_for_each_port (cur_dev
->device
, p
) {
802 if (!rdma_cap_af_ib(cur_dev
->device
, p
))
805 if (ib_find_cached_pkey(cur_dev
->device
, p
, pkey
, &index
))
808 if (ib_get_cached_port_state(cur_dev
->device
, p
, &port_state
))
810 for (i
= 0; !rdma_query_gid(cur_dev
->device
,
813 if (!memcmp(&gid
, dgid
, sizeof(gid
))) {
816 id_priv
->id
.port_num
= p
;
820 if (!cma_dev
&& (gid
.global
.subnet_prefix
==
821 dgid
->global
.subnet_prefix
) &&
822 port_state
== IB_PORT_ACTIVE
) {
825 id_priv
->id
.port_num
= p
;
835 cma_attach_to_dev(id_priv
, cma_dev
);
837 addr
= (struct sockaddr_ib
*)cma_src_addr(id_priv
);
838 memcpy(&addr
->sib_addr
, &sgid
, sizeof(sgid
));
839 cma_translate_ib(addr
, &id_priv
->id
.route
.addr
.dev_addr
);
843 static void cma_id_get(struct rdma_id_private
*id_priv
)
845 refcount_inc(&id_priv
->refcount
);
848 static void cma_id_put(struct rdma_id_private
*id_priv
)
850 if (refcount_dec_and_test(&id_priv
->refcount
))
851 complete(&id_priv
->comp
);
854 struct rdma_cm_id
*__rdma_create_id(struct net
*net
,
855 rdma_cm_event_handler event_handler
,
856 void *context
, enum rdma_ucm_port_space ps
,
857 enum ib_qp_type qp_type
, const char *caller
)
859 struct rdma_id_private
*id_priv
;
861 id_priv
= kzalloc(sizeof *id_priv
, GFP_KERNEL
);
863 return ERR_PTR(-ENOMEM
);
865 rdma_restrack_set_task(&id_priv
->res
, caller
);
866 id_priv
->res
.type
= RDMA_RESTRACK_CM_ID
;
867 id_priv
->state
= RDMA_CM_IDLE
;
868 id_priv
->id
.context
= context
;
869 id_priv
->id
.event_handler
= event_handler
;
871 id_priv
->id
.qp_type
= qp_type
;
872 id_priv
->tos_set
= false;
873 id_priv
->timeout_set
= false;
874 id_priv
->gid_type
= IB_GID_TYPE_IB
;
875 spin_lock_init(&id_priv
->lock
);
876 mutex_init(&id_priv
->qp_mutex
);
877 init_completion(&id_priv
->comp
);
878 refcount_set(&id_priv
->refcount
, 1);
879 mutex_init(&id_priv
->handler_mutex
);
880 INIT_LIST_HEAD(&id_priv
->listen_list
);
881 INIT_LIST_HEAD(&id_priv
->mc_list
);
882 get_random_bytes(&id_priv
->seq_num
, sizeof id_priv
->seq_num
);
883 id_priv
->id
.route
.addr
.dev_addr
.net
= get_net(net
);
884 id_priv
->seq_num
&= 0x00ffffff;
886 trace_cm_id_create(id_priv
);
889 EXPORT_SYMBOL(__rdma_create_id
);
891 static int cma_init_ud_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
893 struct ib_qp_attr qp_attr
;
894 int qp_attr_mask
, ret
;
896 qp_attr
.qp_state
= IB_QPS_INIT
;
897 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
901 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
905 qp_attr
.qp_state
= IB_QPS_RTR
;
906 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
910 qp_attr
.qp_state
= IB_QPS_RTS
;
912 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
917 static int cma_init_conn_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
919 struct ib_qp_attr qp_attr
;
920 int qp_attr_mask
, ret
;
922 qp_attr
.qp_state
= IB_QPS_INIT
;
923 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
927 return ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
930 int rdma_create_qp(struct rdma_cm_id
*id
, struct ib_pd
*pd
,
931 struct ib_qp_init_attr
*qp_init_attr
)
933 struct rdma_id_private
*id_priv
;
937 id_priv
= container_of(id
, struct rdma_id_private
, id
);
938 if (id
->device
!= pd
->device
) {
943 qp_init_attr
->port_num
= id
->port_num
;
944 qp
= ib_create_qp(pd
, qp_init_attr
);
950 if (id
->qp_type
== IB_QPT_UD
)
951 ret
= cma_init_ud_qp(id_priv
, qp
);
953 ret
= cma_init_conn_qp(id_priv
, qp
);
958 id_priv
->qp_num
= qp
->qp_num
;
959 id_priv
->srq
= (qp
->srq
!= NULL
);
960 trace_cm_qp_create(id_priv
, pd
, qp_init_attr
, 0);
965 trace_cm_qp_create(id_priv
, pd
, qp_init_attr
, ret
);
968 EXPORT_SYMBOL(rdma_create_qp
);
970 void rdma_destroy_qp(struct rdma_cm_id
*id
)
972 struct rdma_id_private
*id_priv
;
974 id_priv
= container_of(id
, struct rdma_id_private
, id
);
975 trace_cm_qp_destroy(id_priv
);
976 mutex_lock(&id_priv
->qp_mutex
);
977 ib_destroy_qp(id_priv
->id
.qp
);
978 id_priv
->id
.qp
= NULL
;
979 mutex_unlock(&id_priv
->qp_mutex
);
981 EXPORT_SYMBOL(rdma_destroy_qp
);
983 static int cma_modify_qp_rtr(struct rdma_id_private
*id_priv
,
984 struct rdma_conn_param
*conn_param
)
986 struct ib_qp_attr qp_attr
;
987 int qp_attr_mask
, ret
;
989 mutex_lock(&id_priv
->qp_mutex
);
990 if (!id_priv
->id
.qp
) {
995 /* Need to update QP attributes from default values. */
996 qp_attr
.qp_state
= IB_QPS_INIT
;
997 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
1001 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
1005 qp_attr
.qp_state
= IB_QPS_RTR
;
1006 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
1010 BUG_ON(id_priv
->cma_dev
->device
!= id_priv
->id
.device
);
1013 qp_attr
.max_dest_rd_atomic
= conn_param
->responder_resources
;
1014 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
1016 mutex_unlock(&id_priv
->qp_mutex
);
1020 static int cma_modify_qp_rts(struct rdma_id_private
*id_priv
,
1021 struct rdma_conn_param
*conn_param
)
1023 struct ib_qp_attr qp_attr
;
1024 int qp_attr_mask
, ret
;
1026 mutex_lock(&id_priv
->qp_mutex
);
1027 if (!id_priv
->id
.qp
) {
1032 qp_attr
.qp_state
= IB_QPS_RTS
;
1033 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
1038 qp_attr
.max_rd_atomic
= conn_param
->initiator_depth
;
1039 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
1041 mutex_unlock(&id_priv
->qp_mutex
);
1045 static int cma_modify_qp_err(struct rdma_id_private
*id_priv
)
1047 struct ib_qp_attr qp_attr
;
1050 mutex_lock(&id_priv
->qp_mutex
);
1051 if (!id_priv
->id
.qp
) {
1056 qp_attr
.qp_state
= IB_QPS_ERR
;
1057 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, IB_QP_STATE
);
1059 mutex_unlock(&id_priv
->qp_mutex
);
1063 static int cma_ib_init_qp_attr(struct rdma_id_private
*id_priv
,
1064 struct ib_qp_attr
*qp_attr
, int *qp_attr_mask
)
1066 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
1070 if (rdma_cap_eth_ah(id_priv
->id
.device
, id_priv
->id
.port_num
))
1073 pkey
= ib_addr_get_pkey(dev_addr
);
1075 ret
= ib_find_cached_pkey(id_priv
->id
.device
, id_priv
->id
.port_num
,
1076 pkey
, &qp_attr
->pkey_index
);
1080 qp_attr
->port_num
= id_priv
->id
.port_num
;
1081 *qp_attr_mask
= IB_QP_STATE
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
1083 if (id_priv
->id
.qp_type
== IB_QPT_UD
) {
1084 ret
= cma_set_qkey(id_priv
, 0);
1088 qp_attr
->qkey
= id_priv
->qkey
;
1089 *qp_attr_mask
|= IB_QP_QKEY
;
1091 qp_attr
->qp_access_flags
= 0;
1092 *qp_attr_mask
|= IB_QP_ACCESS_FLAGS
;
1097 int rdma_init_qp_attr(struct rdma_cm_id
*id
, struct ib_qp_attr
*qp_attr
,
1100 struct rdma_id_private
*id_priv
;
1103 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1104 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
1105 if (!id_priv
->cm_id
.ib
|| (id_priv
->id
.qp_type
== IB_QPT_UD
))
1106 ret
= cma_ib_init_qp_attr(id_priv
, qp_attr
, qp_attr_mask
);
1108 ret
= ib_cm_init_qp_attr(id_priv
->cm_id
.ib
, qp_attr
,
1111 if (qp_attr
->qp_state
== IB_QPS_RTR
)
1112 qp_attr
->rq_psn
= id_priv
->seq_num
;
1113 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
1114 if (!id_priv
->cm_id
.iw
) {
1115 qp_attr
->qp_access_flags
= 0;
1116 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
1118 ret
= iw_cm_init_qp_attr(id_priv
->cm_id
.iw
, qp_attr
,
1120 qp_attr
->port_num
= id_priv
->id
.port_num
;
1121 *qp_attr_mask
|= IB_QP_PORT
;
1125 if ((*qp_attr_mask
& IB_QP_TIMEOUT
) && id_priv
->timeout_set
)
1126 qp_attr
->timeout
= id_priv
->timeout
;
1130 EXPORT_SYMBOL(rdma_init_qp_attr
);
1132 static inline bool cma_zero_addr(const struct sockaddr
*addr
)
1134 switch (addr
->sa_family
) {
1136 return ipv4_is_zeronet(((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
1138 return ipv6_addr_any(&((struct sockaddr_in6
*)addr
)->sin6_addr
);
1140 return ib_addr_any(&((struct sockaddr_ib
*)addr
)->sib_addr
);
1146 static inline bool cma_loopback_addr(const struct sockaddr
*addr
)
1148 switch (addr
->sa_family
) {
1150 return ipv4_is_loopback(
1151 ((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
1153 return ipv6_addr_loopback(
1154 &((struct sockaddr_in6
*)addr
)->sin6_addr
);
1156 return ib_addr_loopback(
1157 &((struct sockaddr_ib
*)addr
)->sib_addr
);
1163 static inline bool cma_any_addr(const struct sockaddr
*addr
)
1165 return cma_zero_addr(addr
) || cma_loopback_addr(addr
);
1168 static int cma_addr_cmp(const struct sockaddr
*src
, const struct sockaddr
*dst
)
1170 if (src
->sa_family
!= dst
->sa_family
)
1173 switch (src
->sa_family
) {
1175 return ((struct sockaddr_in
*)src
)->sin_addr
.s_addr
!=
1176 ((struct sockaddr_in
*)dst
)->sin_addr
.s_addr
;
1178 struct sockaddr_in6
*src_addr6
= (struct sockaddr_in6
*)src
;
1179 struct sockaddr_in6
*dst_addr6
= (struct sockaddr_in6
*)dst
;
1182 if (ipv6_addr_cmp(&src_addr6
->sin6_addr
,
1183 &dst_addr6
->sin6_addr
))
1185 link_local
= ipv6_addr_type(&dst_addr6
->sin6_addr
) &
1186 IPV6_ADDR_LINKLOCAL
;
1187 /* Link local must match their scope_ids */
1188 return link_local
? (src_addr6
->sin6_scope_id
!=
1189 dst_addr6
->sin6_scope_id
) :
1194 return ib_addr_cmp(&((struct sockaddr_ib
*) src
)->sib_addr
,
1195 &((struct sockaddr_ib
*) dst
)->sib_addr
);
1199 static __be16
cma_port(const struct sockaddr
*addr
)
1201 struct sockaddr_ib
*sib
;
1203 switch (addr
->sa_family
) {
1205 return ((struct sockaddr_in
*) addr
)->sin_port
;
1207 return ((struct sockaddr_in6
*) addr
)->sin6_port
;
1209 sib
= (struct sockaddr_ib
*) addr
;
1210 return htons((u16
) (be64_to_cpu(sib
->sib_sid
) &
1211 be64_to_cpu(sib
->sib_sid_mask
)));
1217 static inline int cma_any_port(const struct sockaddr
*addr
)
1219 return !cma_port(addr
);
1222 static void cma_save_ib_info(struct sockaddr
*src_addr
,
1223 struct sockaddr
*dst_addr
,
1224 const struct rdma_cm_id
*listen_id
,
1225 const struct sa_path_rec
*path
)
1227 struct sockaddr_ib
*listen_ib
, *ib
;
1229 listen_ib
= (struct sockaddr_ib
*) &listen_id
->route
.addr
.src_addr
;
1231 ib
= (struct sockaddr_ib
*)src_addr
;
1232 ib
->sib_family
= AF_IB
;
1234 ib
->sib_pkey
= path
->pkey
;
1235 ib
->sib_flowinfo
= path
->flow_label
;
1236 memcpy(&ib
->sib_addr
, &path
->sgid
, 16);
1237 ib
->sib_sid
= path
->service_id
;
1238 ib
->sib_scope_id
= 0;
1240 ib
->sib_pkey
= listen_ib
->sib_pkey
;
1241 ib
->sib_flowinfo
= listen_ib
->sib_flowinfo
;
1242 ib
->sib_addr
= listen_ib
->sib_addr
;
1243 ib
->sib_sid
= listen_ib
->sib_sid
;
1244 ib
->sib_scope_id
= listen_ib
->sib_scope_id
;
1246 ib
->sib_sid_mask
= cpu_to_be64(0xffffffffffffffffULL
);
1249 ib
= (struct sockaddr_ib
*)dst_addr
;
1250 ib
->sib_family
= AF_IB
;
1252 ib
->sib_pkey
= path
->pkey
;
1253 ib
->sib_flowinfo
= path
->flow_label
;
1254 memcpy(&ib
->sib_addr
, &path
->dgid
, 16);
1259 static void cma_save_ip4_info(struct sockaddr_in
*src_addr
,
1260 struct sockaddr_in
*dst_addr
,
1261 struct cma_hdr
*hdr
,
1265 *src_addr
= (struct sockaddr_in
) {
1266 .sin_family
= AF_INET
,
1267 .sin_addr
.s_addr
= hdr
->dst_addr
.ip4
.addr
,
1268 .sin_port
= local_port
,
1273 *dst_addr
= (struct sockaddr_in
) {
1274 .sin_family
= AF_INET
,
1275 .sin_addr
.s_addr
= hdr
->src_addr
.ip4
.addr
,
1276 .sin_port
= hdr
->port
,
1281 static void cma_save_ip6_info(struct sockaddr_in6
*src_addr
,
1282 struct sockaddr_in6
*dst_addr
,
1283 struct cma_hdr
*hdr
,
1287 *src_addr
= (struct sockaddr_in6
) {
1288 .sin6_family
= AF_INET6
,
1289 .sin6_addr
= hdr
->dst_addr
.ip6
,
1290 .sin6_port
= local_port
,
1295 *dst_addr
= (struct sockaddr_in6
) {
1296 .sin6_family
= AF_INET6
,
1297 .sin6_addr
= hdr
->src_addr
.ip6
,
1298 .sin6_port
= hdr
->port
,
1303 static u16
cma_port_from_service_id(__be64 service_id
)
1305 return (u16
)be64_to_cpu(service_id
);
1308 static int cma_save_ip_info(struct sockaddr
*src_addr
,
1309 struct sockaddr
*dst_addr
,
1310 const struct ib_cm_event
*ib_event
,
1313 struct cma_hdr
*hdr
;
1316 hdr
= ib_event
->private_data
;
1317 if (hdr
->cma_version
!= CMA_VERSION
)
1320 port
= htons(cma_port_from_service_id(service_id
));
1322 switch (cma_get_ip_ver(hdr
)) {
1324 cma_save_ip4_info((struct sockaddr_in
*)src_addr
,
1325 (struct sockaddr_in
*)dst_addr
, hdr
, port
);
1328 cma_save_ip6_info((struct sockaddr_in6
*)src_addr
,
1329 (struct sockaddr_in6
*)dst_addr
, hdr
, port
);
1332 return -EAFNOSUPPORT
;
1338 static int cma_save_net_info(struct sockaddr
*src_addr
,
1339 struct sockaddr
*dst_addr
,
1340 const struct rdma_cm_id
*listen_id
,
1341 const struct ib_cm_event
*ib_event
,
1342 sa_family_t sa_family
, __be64 service_id
)
1344 if (sa_family
== AF_IB
) {
1345 if (ib_event
->event
== IB_CM_REQ_RECEIVED
)
1346 cma_save_ib_info(src_addr
, dst_addr
, listen_id
,
1347 ib_event
->param
.req_rcvd
.primary_path
);
1348 else if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
)
1349 cma_save_ib_info(src_addr
, dst_addr
, listen_id
, NULL
);
1353 return cma_save_ip_info(src_addr
, dst_addr
, ib_event
, service_id
);
1356 static int cma_save_req_info(const struct ib_cm_event
*ib_event
,
1357 struct cma_req_info
*req
)
1359 const struct ib_cm_req_event_param
*req_param
=
1360 &ib_event
->param
.req_rcvd
;
1361 const struct ib_cm_sidr_req_event_param
*sidr_param
=
1362 &ib_event
->param
.sidr_req_rcvd
;
1364 switch (ib_event
->event
) {
1365 case IB_CM_REQ_RECEIVED
:
1366 req
->device
= req_param
->listen_id
->device
;
1367 req
->port
= req_param
->port
;
1368 memcpy(&req
->local_gid
, &req_param
->primary_path
->sgid
,
1369 sizeof(req
->local_gid
));
1370 req
->has_gid
= true;
1371 req
->service_id
= req_param
->primary_path
->service_id
;
1372 req
->pkey
= be16_to_cpu(req_param
->primary_path
->pkey
);
1373 if (req
->pkey
!= req_param
->bth_pkey
)
1374 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1375 "RDMA CMA: in the future this may cause the request to be dropped\n",
1376 req_param
->bth_pkey
, req
->pkey
);
1378 case IB_CM_SIDR_REQ_RECEIVED
:
1379 req
->device
= sidr_param
->listen_id
->device
;
1380 req
->port
= sidr_param
->port
;
1381 req
->has_gid
= false;
1382 req
->service_id
= sidr_param
->service_id
;
1383 req
->pkey
= sidr_param
->pkey
;
1384 if (req
->pkey
!= sidr_param
->bth_pkey
)
1385 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1386 "RDMA CMA: in the future this may cause the request to be dropped\n",
1387 sidr_param
->bth_pkey
, req
->pkey
);
1396 static bool validate_ipv4_net_dev(struct net_device
*net_dev
,
1397 const struct sockaddr_in
*dst_addr
,
1398 const struct sockaddr_in
*src_addr
)
1400 __be32 daddr
= dst_addr
->sin_addr
.s_addr
,
1401 saddr
= src_addr
->sin_addr
.s_addr
;
1402 struct fib_result res
;
1407 if (ipv4_is_multicast(saddr
) || ipv4_is_lbcast(saddr
) ||
1408 ipv4_is_lbcast(daddr
) || ipv4_is_zeronet(saddr
) ||
1409 ipv4_is_zeronet(daddr
) || ipv4_is_loopback(daddr
) ||
1410 ipv4_is_loopback(saddr
))
1413 memset(&fl4
, 0, sizeof(fl4
));
1414 fl4
.flowi4_iif
= net_dev
->ifindex
;
1419 err
= fib_lookup(dev_net(net_dev
), &fl4
, &res
, 0);
1420 ret
= err
== 0 && FIB_RES_DEV(res
) == net_dev
;
1426 static bool validate_ipv6_net_dev(struct net_device
*net_dev
,
1427 const struct sockaddr_in6
*dst_addr
,
1428 const struct sockaddr_in6
*src_addr
)
1430 #if IS_ENABLED(CONFIG_IPV6)
1431 const int strict
= ipv6_addr_type(&dst_addr
->sin6_addr
) &
1432 IPV6_ADDR_LINKLOCAL
;
1433 struct rt6_info
*rt
= rt6_lookup(dev_net(net_dev
), &dst_addr
->sin6_addr
,
1434 &src_addr
->sin6_addr
, net_dev
->ifindex
,
1441 ret
= rt
->rt6i_idev
->dev
== net_dev
;
1450 static bool validate_net_dev(struct net_device
*net_dev
,
1451 const struct sockaddr
*daddr
,
1452 const struct sockaddr
*saddr
)
1454 const struct sockaddr_in
*daddr4
= (const struct sockaddr_in
*)daddr
;
1455 const struct sockaddr_in
*saddr4
= (const struct sockaddr_in
*)saddr
;
1456 const struct sockaddr_in6
*daddr6
= (const struct sockaddr_in6
*)daddr
;
1457 const struct sockaddr_in6
*saddr6
= (const struct sockaddr_in6
*)saddr
;
1459 switch (daddr
->sa_family
) {
1461 return saddr
->sa_family
== AF_INET
&&
1462 validate_ipv4_net_dev(net_dev
, daddr4
, saddr4
);
1465 return saddr
->sa_family
== AF_INET6
&&
1466 validate_ipv6_net_dev(net_dev
, daddr6
, saddr6
);
1473 static struct net_device
*
1474 roce_get_net_dev_by_cm_event(const struct ib_cm_event
*ib_event
)
1476 const struct ib_gid_attr
*sgid_attr
= NULL
;
1477 struct net_device
*ndev
;
1479 if (ib_event
->event
== IB_CM_REQ_RECEIVED
)
1480 sgid_attr
= ib_event
->param
.req_rcvd
.ppath_sgid_attr
;
1481 else if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
)
1482 sgid_attr
= ib_event
->param
.sidr_req_rcvd
.sgid_attr
;
1488 ndev
= rdma_read_gid_attr_ndev_rcu(sgid_attr
);
1497 static struct net_device
*cma_get_net_dev(const struct ib_cm_event
*ib_event
,
1498 struct cma_req_info
*req
)
1500 struct sockaddr
*listen_addr
=
1501 (struct sockaddr
*)&req
->listen_addr_storage
;
1502 struct sockaddr
*src_addr
= (struct sockaddr
*)&req
->src_addr_storage
;
1503 struct net_device
*net_dev
;
1504 const union ib_gid
*gid
= req
->has_gid
? &req
->local_gid
: NULL
;
1507 err
= cma_save_ip_info(listen_addr
, src_addr
, ib_event
,
1510 return ERR_PTR(err
);
1512 if (rdma_protocol_roce(req
->device
, req
->port
))
1513 net_dev
= roce_get_net_dev_by_cm_event(ib_event
);
1515 net_dev
= ib_get_net_dev_by_params(req
->device
, req
->port
,
1519 return ERR_PTR(-ENODEV
);
1524 static enum rdma_ucm_port_space
rdma_ps_from_service_id(__be64 service_id
)
1526 return (be64_to_cpu(service_id
) >> 16) & 0xffff;
1529 static bool cma_match_private_data(struct rdma_id_private
*id_priv
,
1530 const struct cma_hdr
*hdr
)
1532 struct sockaddr
*addr
= cma_src_addr(id_priv
);
1534 struct in6_addr ip6_addr
;
1536 if (cma_any_addr(addr
) && !id_priv
->afonly
)
1539 switch (addr
->sa_family
) {
1541 ip4_addr
= ((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
;
1542 if (cma_get_ip_ver(hdr
) != 4)
1544 if (!cma_any_addr(addr
) &&
1545 hdr
->dst_addr
.ip4
.addr
!= ip4_addr
)
1549 ip6_addr
= ((struct sockaddr_in6
*)addr
)->sin6_addr
;
1550 if (cma_get_ip_ver(hdr
) != 6)
1552 if (!cma_any_addr(addr
) &&
1553 memcmp(&hdr
->dst_addr
.ip6
, &ip6_addr
, sizeof(ip6_addr
)))
1565 static bool cma_protocol_roce(const struct rdma_cm_id
*id
)
1567 struct ib_device
*device
= id
->device
;
1568 const int port_num
= id
->port_num
?: rdma_start_port(device
);
1570 return rdma_protocol_roce(device
, port_num
);
1573 static bool cma_is_req_ipv6_ll(const struct cma_req_info
*req
)
1575 const struct sockaddr
*daddr
=
1576 (const struct sockaddr
*)&req
->listen_addr_storage
;
1577 const struct sockaddr_in6
*daddr6
= (const struct sockaddr_in6
*)daddr
;
1579 /* Returns true if the req is for IPv6 link local */
1580 return (daddr
->sa_family
== AF_INET6
&&
1581 (ipv6_addr_type(&daddr6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
));
1584 static bool cma_match_net_dev(const struct rdma_cm_id
*id
,
1585 const struct net_device
*net_dev
,
1586 const struct cma_req_info
*req
)
1588 const struct rdma_addr
*addr
= &id
->route
.addr
;
1591 /* This request is an AF_IB request */
1592 return (!id
->port_num
|| id
->port_num
== req
->port
) &&
1593 (addr
->src_addr
.ss_family
== AF_IB
);
1596 * If the request is not for IPv6 link local, allow matching
1597 * request to any netdevice of the one or multiport rdma device.
1599 if (!cma_is_req_ipv6_ll(req
))
1602 * Net namespaces must match, and if the listner is listening
1603 * on a specific netdevice than netdevice must match as well.
1605 if (net_eq(dev_net(net_dev
), addr
->dev_addr
.net
) &&
1606 (!!addr
->dev_addr
.bound_dev_if
==
1607 (addr
->dev_addr
.bound_dev_if
== net_dev
->ifindex
)))
1613 static struct rdma_id_private
*cma_find_listener(
1614 const struct rdma_bind_list
*bind_list
,
1615 const struct ib_cm_id
*cm_id
,
1616 const struct ib_cm_event
*ib_event
,
1617 const struct cma_req_info
*req
,
1618 const struct net_device
*net_dev
)
1620 struct rdma_id_private
*id_priv
, *id_priv_dev
;
1622 lockdep_assert_held(&lock
);
1625 return ERR_PTR(-EINVAL
);
1627 hlist_for_each_entry(id_priv
, &bind_list
->owners
, node
) {
1628 if (cma_match_private_data(id_priv
, ib_event
->private_data
)) {
1629 if (id_priv
->id
.device
== cm_id
->device
&&
1630 cma_match_net_dev(&id_priv
->id
, net_dev
, req
))
1632 list_for_each_entry(id_priv_dev
,
1633 &id_priv
->listen_list
,
1635 if (id_priv_dev
->id
.device
== cm_id
->device
&&
1636 cma_match_net_dev(&id_priv_dev
->id
,
1643 return ERR_PTR(-EINVAL
);
1646 static struct rdma_id_private
*
1647 cma_ib_id_from_event(struct ib_cm_id
*cm_id
,
1648 const struct ib_cm_event
*ib_event
,
1649 struct cma_req_info
*req
,
1650 struct net_device
**net_dev
)
1652 struct rdma_bind_list
*bind_list
;
1653 struct rdma_id_private
*id_priv
;
1656 err
= cma_save_req_info(ib_event
, req
);
1658 return ERR_PTR(err
);
1660 *net_dev
= cma_get_net_dev(ib_event
, req
);
1661 if (IS_ERR(*net_dev
)) {
1662 if (PTR_ERR(*net_dev
) == -EAFNOSUPPORT
) {
1663 /* Assuming the protocol is AF_IB */
1666 return ERR_CAST(*net_dev
);
1672 * Net namespace might be getting deleted while route lookup,
1673 * cm_id lookup is in progress. Therefore, perform netdevice
1674 * validation, cm_id lookup under rcu lock.
1675 * RCU lock along with netdevice state check, synchronizes with
1676 * netdevice migrating to different net namespace and also avoids
1677 * case where net namespace doesn't get deleted while lookup is in
1679 * If the device state is not IFF_UP, its properties such as ifindex
1680 * and nd_net cannot be trusted to remain valid without rcu lock.
1681 * net/core/dev.c change_net_namespace() ensures to synchronize with
1682 * ongoing operations on net device after device is closed using
1683 * synchronize_net().
1688 * If netdevice is down, it is likely that it is administratively
1689 * down or it might be migrating to different namespace.
1690 * In that case avoid further processing, as the net namespace
1691 * or ifindex may change.
1693 if (((*net_dev
)->flags
& IFF_UP
) == 0) {
1694 id_priv
= ERR_PTR(-EHOSTUNREACH
);
1698 if (!validate_net_dev(*net_dev
,
1699 (struct sockaddr
*)&req
->listen_addr_storage
,
1700 (struct sockaddr
*)&req
->src_addr_storage
)) {
1701 id_priv
= ERR_PTR(-EHOSTUNREACH
);
1706 bind_list
= cma_ps_find(*net_dev
? dev_net(*net_dev
) : &init_net
,
1707 rdma_ps_from_service_id(req
->service_id
),
1708 cma_port_from_service_id(req
->service_id
));
1709 id_priv
= cma_find_listener(bind_list
, cm_id
, ib_event
, req
, *net_dev
);
1712 mutex_unlock(&lock
);
1713 if (IS_ERR(id_priv
) && *net_dev
) {
1720 static inline u8
cma_user_data_offset(struct rdma_id_private
*id_priv
)
1722 return cma_family(id_priv
) == AF_IB
? 0 : sizeof(struct cma_hdr
);
1725 static void cma_cancel_route(struct rdma_id_private
*id_priv
)
1727 if (rdma_cap_ib_sa(id_priv
->id
.device
, id_priv
->id
.port_num
)) {
1729 ib_sa_cancel_query(id_priv
->query_id
, id_priv
->query
);
1733 static void cma_cancel_listens(struct rdma_id_private
*id_priv
)
1735 struct rdma_id_private
*dev_id_priv
;
1738 * Remove from listen_any_list to prevent added devices from spawning
1739 * additional listen requests.
1742 list_del(&id_priv
->list
);
1744 while (!list_empty(&id_priv
->listen_list
)) {
1745 dev_id_priv
= list_entry(id_priv
->listen_list
.next
,
1746 struct rdma_id_private
, listen_list
);
1747 /* sync with device removal to avoid duplicate destruction */
1748 list_del_init(&dev_id_priv
->list
);
1749 list_del(&dev_id_priv
->listen_list
);
1750 mutex_unlock(&lock
);
1752 rdma_destroy_id(&dev_id_priv
->id
);
1755 mutex_unlock(&lock
);
1758 static void cma_cancel_operation(struct rdma_id_private
*id_priv
,
1759 enum rdma_cm_state state
)
1762 case RDMA_CM_ADDR_QUERY
:
1763 rdma_addr_cancel(&id_priv
->id
.route
.addr
.dev_addr
);
1765 case RDMA_CM_ROUTE_QUERY
:
1766 cma_cancel_route(id_priv
);
1768 case RDMA_CM_LISTEN
:
1769 if (cma_any_addr(cma_src_addr(id_priv
)) && !id_priv
->cma_dev
)
1770 cma_cancel_listens(id_priv
);
1777 static void cma_release_port(struct rdma_id_private
*id_priv
)
1779 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
1780 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
1786 hlist_del(&id_priv
->node
);
1787 if (hlist_empty(&bind_list
->owners
)) {
1788 cma_ps_remove(net
, bind_list
->ps
, bind_list
->port
);
1791 mutex_unlock(&lock
);
1794 static void cma_leave_roce_mc_group(struct rdma_id_private
*id_priv
,
1795 struct cma_multicast
*mc
)
1797 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
1798 struct net_device
*ndev
= NULL
;
1800 if (dev_addr
->bound_dev_if
)
1801 ndev
= dev_get_by_index(dev_addr
->net
, dev_addr
->bound_dev_if
);
1803 cma_igmp_send(ndev
, &mc
->multicast
.ib
->rec
.mgid
, false);
1806 kref_put(&mc
->mcref
, release_mc
);
1809 static void cma_leave_mc_groups(struct rdma_id_private
*id_priv
)
1811 struct cma_multicast
*mc
;
1813 while (!list_empty(&id_priv
->mc_list
)) {
1814 mc
= container_of(id_priv
->mc_list
.next
,
1815 struct cma_multicast
, list
);
1816 list_del(&mc
->list
);
1817 if (rdma_cap_ib_mcast(id_priv
->cma_dev
->device
,
1818 id_priv
->id
.port_num
)) {
1819 ib_sa_free_multicast(mc
->multicast
.ib
);
1822 cma_leave_roce_mc_group(id_priv
, mc
);
1827 void rdma_destroy_id(struct rdma_cm_id
*id
)
1829 struct rdma_id_private
*id_priv
;
1830 enum rdma_cm_state state
;
1832 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1833 trace_cm_id_destroy(id_priv
);
1834 state
= cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1835 cma_cancel_operation(id_priv
, state
);
1838 * Wait for any active callback to finish. New callbacks will find
1839 * the id_priv state set to destroying and abort.
1841 mutex_lock(&id_priv
->handler_mutex
);
1842 mutex_unlock(&id_priv
->handler_mutex
);
1844 rdma_restrack_del(&id_priv
->res
);
1845 if (id_priv
->cma_dev
) {
1846 if (rdma_cap_ib_cm(id_priv
->id
.device
, 1)) {
1847 if (id_priv
->cm_id
.ib
)
1848 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1849 } else if (rdma_cap_iw_cm(id_priv
->id
.device
, 1)) {
1850 if (id_priv
->cm_id
.iw
)
1851 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1853 cma_leave_mc_groups(id_priv
);
1854 cma_release_dev(id_priv
);
1857 cma_release_port(id_priv
);
1858 cma_id_put(id_priv
);
1859 wait_for_completion(&id_priv
->comp
);
1861 if (id_priv
->internal_id
)
1862 cma_id_put(id_priv
->id
.context
);
1864 kfree(id_priv
->id
.route
.path_rec
);
1866 if (id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
)
1867 rdma_put_gid_attr(id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
);
1869 put_net(id_priv
->id
.route
.addr
.dev_addr
.net
);
1872 EXPORT_SYMBOL(rdma_destroy_id
);
1874 static int cma_rep_recv(struct rdma_id_private
*id_priv
)
1878 ret
= cma_modify_qp_rtr(id_priv
, NULL
);
1882 ret
= cma_modify_qp_rts(id_priv
, NULL
);
1886 trace_cm_send_rtu(id_priv
);
1887 ret
= ib_send_cm_rtu(id_priv
->cm_id
.ib
, NULL
, 0);
1893 pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret
);
1894 cma_modify_qp_err(id_priv
);
1895 trace_cm_send_rej(id_priv
);
1896 ib_send_cm_rej(id_priv
->cm_id
.ib
, IB_CM_REJ_CONSUMER_DEFINED
,
1901 static void cma_set_rep_event_data(struct rdma_cm_event
*event
,
1902 const struct ib_cm_rep_event_param
*rep_data
,
1905 event
->param
.conn
.private_data
= private_data
;
1906 event
->param
.conn
.private_data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
1907 event
->param
.conn
.responder_resources
= rep_data
->responder_resources
;
1908 event
->param
.conn
.initiator_depth
= rep_data
->initiator_depth
;
1909 event
->param
.conn
.flow_control
= rep_data
->flow_control
;
1910 event
->param
.conn
.rnr_retry_count
= rep_data
->rnr_retry_count
;
1911 event
->param
.conn
.srq
= rep_data
->srq
;
1912 event
->param
.conn
.qp_num
= rep_data
->remote_qpn
;
1915 static int cma_cm_event_handler(struct rdma_id_private
*id_priv
,
1916 struct rdma_cm_event
*event
)
1920 trace_cm_event_handler(id_priv
, event
);
1921 ret
= id_priv
->id
.event_handler(&id_priv
->id
, event
);
1922 trace_cm_event_done(id_priv
, event
, ret
);
1926 static int cma_ib_handler(struct ib_cm_id
*cm_id
,
1927 const struct ib_cm_event
*ib_event
)
1929 struct rdma_id_private
*id_priv
= cm_id
->context
;
1930 struct rdma_cm_event event
= {};
1933 mutex_lock(&id_priv
->handler_mutex
);
1934 if ((ib_event
->event
!= IB_CM_TIMEWAIT_EXIT
&&
1935 id_priv
->state
!= RDMA_CM_CONNECT
) ||
1936 (ib_event
->event
== IB_CM_TIMEWAIT_EXIT
&&
1937 id_priv
->state
!= RDMA_CM_DISCONNECT
))
1940 switch (ib_event
->event
) {
1941 case IB_CM_REQ_ERROR
:
1942 case IB_CM_REP_ERROR
:
1943 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1944 event
.status
= -ETIMEDOUT
;
1946 case IB_CM_REP_RECEIVED
:
1947 if (cma_comp(id_priv
, RDMA_CM_CONNECT
) &&
1948 (id_priv
->id
.qp_type
!= IB_QPT_UD
)) {
1949 trace_cm_send_mra(id_priv
);
1950 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
1952 if (id_priv
->id
.qp
) {
1953 event
.status
= cma_rep_recv(id_priv
);
1954 event
.event
= event
.status
? RDMA_CM_EVENT_CONNECT_ERROR
:
1955 RDMA_CM_EVENT_ESTABLISHED
;
1957 event
.event
= RDMA_CM_EVENT_CONNECT_RESPONSE
;
1959 cma_set_rep_event_data(&event
, &ib_event
->param
.rep_rcvd
,
1960 ib_event
->private_data
);
1962 case IB_CM_RTU_RECEIVED
:
1963 case IB_CM_USER_ESTABLISHED
:
1964 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1966 case IB_CM_DREQ_ERROR
:
1967 event
.status
= -ETIMEDOUT
; /* fall through */
1968 case IB_CM_DREQ_RECEIVED
:
1969 case IB_CM_DREP_RECEIVED
:
1970 if (!cma_comp_exch(id_priv
, RDMA_CM_CONNECT
,
1971 RDMA_CM_DISCONNECT
))
1973 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1975 case IB_CM_TIMEWAIT_EXIT
:
1976 event
.event
= RDMA_CM_EVENT_TIMEWAIT_EXIT
;
1978 case IB_CM_MRA_RECEIVED
:
1981 case IB_CM_REJ_RECEIVED
:
1982 pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv
->id
,
1983 ib_event
->param
.rej_rcvd
.reason
));
1984 cma_modify_qp_err(id_priv
);
1985 event
.status
= ib_event
->param
.rej_rcvd
.reason
;
1986 event
.event
= RDMA_CM_EVENT_REJECTED
;
1987 event
.param
.conn
.private_data
= ib_event
->private_data
;
1988 event
.param
.conn
.private_data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
1991 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
1996 ret
= cma_cm_event_handler(id_priv
, &event
);
1998 /* Destroy the CM ID by returning a non-zero value. */
1999 id_priv
->cm_id
.ib
= NULL
;
2000 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2001 mutex_unlock(&id_priv
->handler_mutex
);
2002 rdma_destroy_id(&id_priv
->id
);
2006 mutex_unlock(&id_priv
->handler_mutex
);
2010 static struct rdma_id_private
*
2011 cma_ib_new_conn_id(const struct rdma_cm_id
*listen_id
,
2012 const struct ib_cm_event
*ib_event
,
2013 struct net_device
*net_dev
)
2015 struct rdma_id_private
*listen_id_priv
;
2016 struct rdma_id_private
*id_priv
;
2017 struct rdma_cm_id
*id
;
2018 struct rdma_route
*rt
;
2019 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
2020 struct sa_path_rec
*path
= ib_event
->param
.req_rcvd
.primary_path
;
2021 const __be64 service_id
=
2022 ib_event
->param
.req_rcvd
.primary_path
->service_id
;
2025 listen_id_priv
= container_of(listen_id
, struct rdma_id_private
, id
);
2026 id
= __rdma_create_id(listen_id
->route
.addr
.dev_addr
.net
,
2027 listen_id
->event_handler
, listen_id
->context
,
2028 listen_id
->ps
, ib_event
->param
.req_rcvd
.qp_type
,
2029 listen_id_priv
->res
.kern_name
);
2033 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2034 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
2035 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
2036 listen_id
, ib_event
, ss_family
, service_id
))
2040 rt
->num_paths
= ib_event
->param
.req_rcvd
.alternate_path
? 2 : 1;
2041 rt
->path_rec
= kmalloc_array(rt
->num_paths
, sizeof(*rt
->path_rec
),
2046 rt
->path_rec
[0] = *path
;
2047 if (rt
->num_paths
== 2)
2048 rt
->path_rec
[1] = *ib_event
->param
.req_rcvd
.alternate_path
;
2051 rdma_copy_src_l2_addr(&rt
->addr
.dev_addr
, net_dev
);
2053 if (!cma_protocol_roce(listen_id
) &&
2054 cma_any_addr(cma_src_addr(id_priv
))) {
2055 rt
->addr
.dev_addr
.dev_type
= ARPHRD_INFINIBAND
;
2056 rdma_addr_set_sgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].sgid
);
2057 ib_addr_set_pkey(&rt
->addr
.dev_addr
, be16_to_cpu(rt
->path_rec
[0].pkey
));
2058 } else if (!cma_any_addr(cma_src_addr(id_priv
))) {
2059 ret
= cma_translate_addr(cma_src_addr(id_priv
), &rt
->addr
.dev_addr
);
2064 rdma_addr_set_dgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].dgid
);
2066 id_priv
->state
= RDMA_CM_CONNECT
;
2070 rdma_destroy_id(id
);
2074 static struct rdma_id_private
*
2075 cma_ib_new_udp_id(const struct rdma_cm_id
*listen_id
,
2076 const struct ib_cm_event
*ib_event
,
2077 struct net_device
*net_dev
)
2079 const struct rdma_id_private
*listen_id_priv
;
2080 struct rdma_id_private
*id_priv
;
2081 struct rdma_cm_id
*id
;
2082 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
2083 struct net
*net
= listen_id
->route
.addr
.dev_addr
.net
;
2086 listen_id_priv
= container_of(listen_id
, struct rdma_id_private
, id
);
2087 id
= __rdma_create_id(net
, listen_id
->event_handler
, listen_id
->context
,
2088 listen_id
->ps
, IB_QPT_UD
,
2089 listen_id_priv
->res
.kern_name
);
2093 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2094 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
2095 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
2096 listen_id
, ib_event
, ss_family
,
2097 ib_event
->param
.sidr_req_rcvd
.service_id
))
2101 rdma_copy_src_l2_addr(&id
->route
.addr
.dev_addr
, net_dev
);
2103 if (!cma_any_addr(cma_src_addr(id_priv
))) {
2104 ret
= cma_translate_addr(cma_src_addr(id_priv
),
2105 &id
->route
.addr
.dev_addr
);
2111 id_priv
->state
= RDMA_CM_CONNECT
;
2114 rdma_destroy_id(id
);
2118 static void cma_set_req_event_data(struct rdma_cm_event
*event
,
2119 const struct ib_cm_req_event_param
*req_data
,
2120 void *private_data
, int offset
)
2122 event
->param
.conn
.private_data
= private_data
+ offset
;
2123 event
->param
.conn
.private_data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
- offset
;
2124 event
->param
.conn
.responder_resources
= req_data
->responder_resources
;
2125 event
->param
.conn
.initiator_depth
= req_data
->initiator_depth
;
2126 event
->param
.conn
.flow_control
= req_data
->flow_control
;
2127 event
->param
.conn
.retry_count
= req_data
->retry_count
;
2128 event
->param
.conn
.rnr_retry_count
= req_data
->rnr_retry_count
;
2129 event
->param
.conn
.srq
= req_data
->srq
;
2130 event
->param
.conn
.qp_num
= req_data
->remote_qpn
;
2133 static int cma_ib_check_req_qp_type(const struct rdma_cm_id
*id
,
2134 const struct ib_cm_event
*ib_event
)
2136 return (((ib_event
->event
== IB_CM_REQ_RECEIVED
) &&
2137 (ib_event
->param
.req_rcvd
.qp_type
== id
->qp_type
)) ||
2138 ((ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) &&
2139 (id
->qp_type
== IB_QPT_UD
)) ||
2143 static int cma_ib_req_handler(struct ib_cm_id
*cm_id
,
2144 const struct ib_cm_event
*ib_event
)
2146 struct rdma_id_private
*listen_id
, *conn_id
= NULL
;
2147 struct rdma_cm_event event
= {};
2148 struct cma_req_info req
= {};
2149 struct net_device
*net_dev
;
2153 listen_id
= cma_ib_id_from_event(cm_id
, ib_event
, &req
, &net_dev
);
2154 if (IS_ERR(listen_id
))
2155 return PTR_ERR(listen_id
);
2157 trace_cm_req_handler(listen_id
, ib_event
->event
);
2158 if (!cma_ib_check_req_qp_type(&listen_id
->id
, ib_event
)) {
2163 mutex_lock(&listen_id
->handler_mutex
);
2164 if (listen_id
->state
!= RDMA_CM_LISTEN
) {
2165 ret
= -ECONNABORTED
;
2169 offset
= cma_user_data_offset(listen_id
);
2170 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
2171 if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) {
2172 conn_id
= cma_ib_new_udp_id(&listen_id
->id
, ib_event
, net_dev
);
2173 event
.param
.ud
.private_data
= ib_event
->private_data
+ offset
;
2174 event
.param
.ud
.private_data_len
=
2175 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
- offset
;
2177 conn_id
= cma_ib_new_conn_id(&listen_id
->id
, ib_event
, net_dev
);
2178 cma_set_req_event_data(&event
, &ib_event
->param
.req_rcvd
,
2179 ib_event
->private_data
, offset
);
2186 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
2187 ret
= cma_ib_acquire_dev(conn_id
, listen_id
, &req
);
2191 conn_id
->cm_id
.ib
= cm_id
;
2192 cm_id
->context
= conn_id
;
2193 cm_id
->cm_handler
= cma_ib_handler
;
2196 * Protect against the user destroying conn_id from another thread
2197 * until we're done accessing it.
2199 cma_id_get(conn_id
);
2200 ret
= cma_cm_event_handler(conn_id
, &event
);
2204 * Acquire mutex to prevent user executing rdma_destroy_id()
2205 * while we're accessing the cm_id.
2208 if (cma_comp(conn_id
, RDMA_CM_CONNECT
) &&
2209 (conn_id
->id
.qp_type
!= IB_QPT_UD
)) {
2210 trace_cm_send_mra(cm_id
->context
);
2211 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
2213 mutex_unlock(&lock
);
2214 mutex_unlock(&conn_id
->handler_mutex
);
2215 mutex_unlock(&listen_id
->handler_mutex
);
2216 cma_id_put(conn_id
);
2222 cma_id_put(conn_id
);
2223 /* Destroy the CM ID by returning a non-zero value. */
2224 conn_id
->cm_id
.ib
= NULL
;
2226 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
2227 mutex_unlock(&conn_id
->handler_mutex
);
2229 mutex_unlock(&listen_id
->handler_mutex
);
2231 rdma_destroy_id(&conn_id
->id
);
2240 __be64
rdma_get_service_id(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2242 if (addr
->sa_family
== AF_IB
)
2243 return ((struct sockaddr_ib
*) addr
)->sib_sid
;
2245 return cpu_to_be64(((u64
)id
->ps
<< 16) + be16_to_cpu(cma_port(addr
)));
2247 EXPORT_SYMBOL(rdma_get_service_id
);
2249 void rdma_read_gids(struct rdma_cm_id
*cm_id
, union ib_gid
*sgid
,
2252 struct rdma_addr
*addr
= &cm_id
->route
.addr
;
2254 if (!cm_id
->device
) {
2256 memset(sgid
, 0, sizeof(*sgid
));
2258 memset(dgid
, 0, sizeof(*dgid
));
2262 if (rdma_protocol_roce(cm_id
->device
, cm_id
->port_num
)) {
2264 rdma_ip2gid((struct sockaddr
*)&addr
->src_addr
, sgid
);
2266 rdma_ip2gid((struct sockaddr
*)&addr
->dst_addr
, dgid
);
2269 rdma_addr_get_sgid(&addr
->dev_addr
, sgid
);
2271 rdma_addr_get_dgid(&addr
->dev_addr
, dgid
);
2274 EXPORT_SYMBOL(rdma_read_gids
);
2276 static int cma_iw_handler(struct iw_cm_id
*iw_id
, struct iw_cm_event
*iw_event
)
2278 struct rdma_id_private
*id_priv
= iw_id
->context
;
2279 struct rdma_cm_event event
= {};
2281 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
2282 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
2284 mutex_lock(&id_priv
->handler_mutex
);
2285 if (id_priv
->state
!= RDMA_CM_CONNECT
)
2288 switch (iw_event
->event
) {
2289 case IW_CM_EVENT_CLOSE
:
2290 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
2292 case IW_CM_EVENT_CONNECT_REPLY
:
2293 memcpy(cma_src_addr(id_priv
), laddr
,
2294 rdma_addr_size(laddr
));
2295 memcpy(cma_dst_addr(id_priv
), raddr
,
2296 rdma_addr_size(raddr
));
2297 switch (iw_event
->status
) {
2299 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2300 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2301 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2305 event
.event
= RDMA_CM_EVENT_REJECTED
;
2308 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2311 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
2315 case IW_CM_EVENT_ESTABLISHED
:
2316 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2317 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2318 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2324 event
.status
= iw_event
->status
;
2325 event
.param
.conn
.private_data
= iw_event
->private_data
;
2326 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
2327 ret
= cma_cm_event_handler(id_priv
, &event
);
2329 /* Destroy the CM ID by returning a non-zero value. */
2330 id_priv
->cm_id
.iw
= NULL
;
2331 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2332 mutex_unlock(&id_priv
->handler_mutex
);
2333 rdma_destroy_id(&id_priv
->id
);
2338 mutex_unlock(&id_priv
->handler_mutex
);
2342 static int iw_conn_req_handler(struct iw_cm_id
*cm_id
,
2343 struct iw_cm_event
*iw_event
)
2345 struct rdma_cm_id
*new_cm_id
;
2346 struct rdma_id_private
*listen_id
, *conn_id
;
2347 struct rdma_cm_event event
= {};
2348 int ret
= -ECONNABORTED
;
2349 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
2350 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
2352 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
2353 event
.param
.conn
.private_data
= iw_event
->private_data
;
2354 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
2355 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2356 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2358 listen_id
= cm_id
->context
;
2360 mutex_lock(&listen_id
->handler_mutex
);
2361 if (listen_id
->state
!= RDMA_CM_LISTEN
)
2364 /* Create a new RDMA id for the new IW CM ID */
2365 new_cm_id
= __rdma_create_id(listen_id
->id
.route
.addr
.dev_addr
.net
,
2366 listen_id
->id
.event_handler
,
2367 listen_id
->id
.context
,
2368 RDMA_PS_TCP
, IB_QPT_RC
,
2369 listen_id
->res
.kern_name
);
2370 if (IS_ERR(new_cm_id
)) {
2374 conn_id
= container_of(new_cm_id
, struct rdma_id_private
, id
);
2375 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
2376 conn_id
->state
= RDMA_CM_CONNECT
;
2378 ret
= rdma_translate_ip(laddr
, &conn_id
->id
.route
.addr
.dev_addr
);
2380 mutex_unlock(&conn_id
->handler_mutex
);
2381 rdma_destroy_id(new_cm_id
);
2385 ret
= cma_iw_acquire_dev(conn_id
, listen_id
);
2387 mutex_unlock(&conn_id
->handler_mutex
);
2388 rdma_destroy_id(new_cm_id
);
2392 conn_id
->cm_id
.iw
= cm_id
;
2393 cm_id
->context
= conn_id
;
2394 cm_id
->cm_handler
= cma_iw_handler
;
2396 memcpy(cma_src_addr(conn_id
), laddr
, rdma_addr_size(laddr
));
2397 memcpy(cma_dst_addr(conn_id
), raddr
, rdma_addr_size(raddr
));
2400 * Protect against the user destroying conn_id from another thread
2401 * until we're done accessing it.
2403 cma_id_get(conn_id
);
2404 ret
= cma_cm_event_handler(conn_id
, &event
);
2406 /* User wants to destroy the CM ID */
2407 conn_id
->cm_id
.iw
= NULL
;
2408 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
2409 mutex_unlock(&conn_id
->handler_mutex
);
2410 mutex_unlock(&listen_id
->handler_mutex
);
2411 cma_id_put(conn_id
);
2412 rdma_destroy_id(&conn_id
->id
);
2416 mutex_unlock(&conn_id
->handler_mutex
);
2417 cma_id_put(conn_id
);
2420 mutex_unlock(&listen_id
->handler_mutex
);
2424 static int cma_ib_listen(struct rdma_id_private
*id_priv
)
2426 struct sockaddr
*addr
;
2427 struct ib_cm_id
*id
;
2430 addr
= cma_src_addr(id_priv
);
2431 svc_id
= rdma_get_service_id(&id_priv
->id
, addr
);
2432 id
= ib_cm_insert_listen(id_priv
->id
.device
,
2433 cma_ib_req_handler
, svc_id
);
2436 id_priv
->cm_id
.ib
= id
;
2441 static int cma_iw_listen(struct rdma_id_private
*id_priv
, int backlog
)
2444 struct iw_cm_id
*id
;
2446 id
= iw_create_cm_id(id_priv
->id
.device
,
2447 iw_conn_req_handler
,
2452 id
->tos
= id_priv
->tos
;
2453 id
->tos_set
= id_priv
->tos_set
;
2454 id_priv
->cm_id
.iw
= id
;
2456 memcpy(&id_priv
->cm_id
.iw
->local_addr
, cma_src_addr(id_priv
),
2457 rdma_addr_size(cma_src_addr(id_priv
)));
2459 ret
= iw_cm_listen(id_priv
->cm_id
.iw
, backlog
);
2462 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
2463 id_priv
->cm_id
.iw
= NULL
;
2469 static int cma_listen_handler(struct rdma_cm_id
*id
,
2470 struct rdma_cm_event
*event
)
2472 struct rdma_id_private
*id_priv
= id
->context
;
2474 id
->context
= id_priv
->id
.context
;
2475 id
->event_handler
= id_priv
->id
.event_handler
;
2476 trace_cm_event_handler(id_priv
, event
);
2477 return id_priv
->id
.event_handler(id
, event
);
2480 static void cma_listen_on_dev(struct rdma_id_private
*id_priv
,
2481 struct cma_device
*cma_dev
)
2483 struct rdma_id_private
*dev_id_priv
;
2484 struct rdma_cm_id
*id
;
2485 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
2488 lockdep_assert_held(&lock
);
2490 if (cma_family(id_priv
) == AF_IB
&& !rdma_cap_ib_cm(cma_dev
->device
, 1))
2493 id
= __rdma_create_id(net
, cma_listen_handler
, id_priv
, id_priv
->id
.ps
,
2494 id_priv
->id
.qp_type
, id_priv
->res
.kern_name
);
2498 dev_id_priv
= container_of(id
, struct rdma_id_private
, id
);
2500 dev_id_priv
->state
= RDMA_CM_ADDR_BOUND
;
2501 memcpy(cma_src_addr(dev_id_priv
), cma_src_addr(id_priv
),
2502 rdma_addr_size(cma_src_addr(id_priv
)));
2504 _cma_attach_to_dev(dev_id_priv
, cma_dev
);
2505 list_add_tail(&dev_id_priv
->listen_list
, &id_priv
->listen_list
);
2506 cma_id_get(id_priv
);
2507 dev_id_priv
->internal_id
= 1;
2508 dev_id_priv
->afonly
= id_priv
->afonly
;
2509 dev_id_priv
->tos_set
= id_priv
->tos_set
;
2510 dev_id_priv
->tos
= id_priv
->tos
;
2512 ret
= rdma_listen(id
, id_priv
->backlog
);
2514 dev_warn(&cma_dev
->device
->dev
,
2515 "RDMA CMA: cma_listen_on_dev, error %d\n", ret
);
2518 static void cma_listen_on_all(struct rdma_id_private
*id_priv
)
2520 struct cma_device
*cma_dev
;
2523 list_add_tail(&id_priv
->list
, &listen_any_list
);
2524 list_for_each_entry(cma_dev
, &dev_list
, list
)
2525 cma_listen_on_dev(id_priv
, cma_dev
);
2526 mutex_unlock(&lock
);
2529 void rdma_set_service_type(struct rdma_cm_id
*id
, int tos
)
2531 struct rdma_id_private
*id_priv
;
2533 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2534 id_priv
->tos
= (u8
) tos
;
2535 id_priv
->tos_set
= true;
2537 EXPORT_SYMBOL(rdma_set_service_type
);
2540 * rdma_set_ack_timeout() - Set the ack timeout of QP associated
2541 * with a connection identifier.
2542 * @id: Communication identifier to associated with service type.
2543 * @timeout: Ack timeout to set a QP, expressed as 4.096 * 2^(timeout) usec.
2545 * This function should be called before rdma_connect() on active side,
2546 * and on passive side before rdma_accept(). It is applicable to primary
2547 * path only. The timeout will affect the local side of the QP, it is not
2548 * negotiated with remote side and zero disables the timer. In case it is
2549 * set before rdma_resolve_route, the value will also be used to determine
2550 * PacketLifeTime for RoCE.
2552 * Return: 0 for success
2554 int rdma_set_ack_timeout(struct rdma_cm_id
*id
, u8 timeout
)
2556 struct rdma_id_private
*id_priv
;
2558 if (id
->qp_type
!= IB_QPT_RC
)
2561 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2562 id_priv
->timeout
= timeout
;
2563 id_priv
->timeout_set
= true;
2567 EXPORT_SYMBOL(rdma_set_ack_timeout
);
2569 static void cma_query_handler(int status
, struct sa_path_rec
*path_rec
,
2572 struct cma_work
*work
= context
;
2573 struct rdma_route
*route
;
2575 route
= &work
->id
->id
.route
;
2578 route
->num_paths
= 1;
2579 *route
->path_rec
= *path_rec
;
2581 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2582 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2583 work
->event
.event
= RDMA_CM_EVENT_ROUTE_ERROR
;
2584 work
->event
.status
= status
;
2585 pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2589 queue_work(cma_wq
, &work
->work
);
2592 static int cma_query_ib_route(struct rdma_id_private
*id_priv
,
2593 unsigned long timeout_ms
, struct cma_work
*work
)
2595 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2596 struct sa_path_rec path_rec
;
2597 ib_sa_comp_mask comp_mask
;
2598 struct sockaddr_in6
*sin6
;
2599 struct sockaddr_ib
*sib
;
2601 memset(&path_rec
, 0, sizeof path_rec
);
2603 if (rdma_cap_opa_ah(id_priv
->id
.device
, id_priv
->id
.port_num
))
2604 path_rec
.rec_type
= SA_PATH_REC_TYPE_OPA
;
2606 path_rec
.rec_type
= SA_PATH_REC_TYPE_IB
;
2607 rdma_addr_get_sgid(dev_addr
, &path_rec
.sgid
);
2608 rdma_addr_get_dgid(dev_addr
, &path_rec
.dgid
);
2609 path_rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
2610 path_rec
.numb_path
= 1;
2611 path_rec
.reversible
= 1;
2612 path_rec
.service_id
= rdma_get_service_id(&id_priv
->id
,
2613 cma_dst_addr(id_priv
));
2615 comp_mask
= IB_SA_PATH_REC_DGID
| IB_SA_PATH_REC_SGID
|
2616 IB_SA_PATH_REC_PKEY
| IB_SA_PATH_REC_NUMB_PATH
|
2617 IB_SA_PATH_REC_REVERSIBLE
| IB_SA_PATH_REC_SERVICE_ID
;
2619 switch (cma_family(id_priv
)) {
2621 path_rec
.qos_class
= cpu_to_be16((u16
) id_priv
->tos
);
2622 comp_mask
|= IB_SA_PATH_REC_QOS_CLASS
;
2625 sin6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
2626 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sin6
->sin6_flowinfo
) >> 20);
2627 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2630 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
2631 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sib
->sib_flowinfo
) >> 20);
2632 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2636 id_priv
->query_id
= ib_sa_path_rec_get(&sa_client
, id_priv
->id
.device
,
2637 id_priv
->id
.port_num
, &path_rec
,
2638 comp_mask
, timeout_ms
,
2639 GFP_KERNEL
, cma_query_handler
,
2640 work
, &id_priv
->query
);
2642 return (id_priv
->query_id
< 0) ? id_priv
->query_id
: 0;
2645 static void cma_work_handler(struct work_struct
*_work
)
2647 struct cma_work
*work
= container_of(_work
, struct cma_work
, work
);
2648 struct rdma_id_private
*id_priv
= work
->id
;
2651 mutex_lock(&id_priv
->handler_mutex
);
2652 if (!cma_comp_exch(id_priv
, work
->old_state
, work
->new_state
))
2655 if (cma_cm_event_handler(id_priv
, &work
->event
)) {
2656 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2660 mutex_unlock(&id_priv
->handler_mutex
);
2661 cma_id_put(id_priv
);
2663 rdma_destroy_id(&id_priv
->id
);
2667 static void cma_ndev_work_handler(struct work_struct
*_work
)
2669 struct cma_ndev_work
*work
= container_of(_work
, struct cma_ndev_work
, work
);
2670 struct rdma_id_private
*id_priv
= work
->id
;
2673 mutex_lock(&id_priv
->handler_mutex
);
2674 if (id_priv
->state
== RDMA_CM_DESTROYING
||
2675 id_priv
->state
== RDMA_CM_DEVICE_REMOVAL
)
2678 if (cma_cm_event_handler(id_priv
, &work
->event
)) {
2679 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2684 mutex_unlock(&id_priv
->handler_mutex
);
2685 cma_id_put(id_priv
);
2687 rdma_destroy_id(&id_priv
->id
);
2691 static void cma_init_resolve_route_work(struct cma_work
*work
,
2692 struct rdma_id_private
*id_priv
)
2695 INIT_WORK(&work
->work
, cma_work_handler
);
2696 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2697 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2698 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2701 static void enqueue_resolve_addr_work(struct cma_work
*work
,
2702 struct rdma_id_private
*id_priv
)
2704 /* Balances with cma_id_put() in cma_work_handler */
2705 cma_id_get(id_priv
);
2708 INIT_WORK(&work
->work
, cma_work_handler
);
2709 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2710 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2711 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2713 queue_work(cma_wq
, &work
->work
);
2716 static int cma_resolve_ib_route(struct rdma_id_private
*id_priv
,
2717 unsigned long timeout_ms
)
2719 struct rdma_route
*route
= &id_priv
->id
.route
;
2720 struct cma_work
*work
;
2723 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2727 cma_init_resolve_route_work(work
, id_priv
);
2729 route
->path_rec
= kmalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2730 if (!route
->path_rec
) {
2735 ret
= cma_query_ib_route(id_priv
, timeout_ms
, work
);
2741 kfree(route
->path_rec
);
2742 route
->path_rec
= NULL
;
2748 static enum ib_gid_type
cma_route_gid_type(enum rdma_network_type network_type
,
2749 unsigned long supported_gids
,
2750 enum ib_gid_type default_gid
)
2752 if ((network_type
== RDMA_NETWORK_IPV4
||
2753 network_type
== RDMA_NETWORK_IPV6
) &&
2754 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP
, &supported_gids
))
2755 return IB_GID_TYPE_ROCE_UDP_ENCAP
;
2761 * cma_iboe_set_path_rec_l2_fields() is helper function which sets
2762 * path record type based on GID type.
2763 * It also sets up other L2 fields which includes destination mac address
2764 * netdev ifindex, of the path record.
2765 * It returns the netdev of the bound interface for this path record entry.
2767 static struct net_device
*
2768 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private
*id_priv
)
2770 struct rdma_route
*route
= &id_priv
->id
.route
;
2771 enum ib_gid_type gid_type
= IB_GID_TYPE_ROCE
;
2772 struct rdma_addr
*addr
= &route
->addr
;
2773 unsigned long supported_gids
;
2774 struct net_device
*ndev
;
2776 if (!addr
->dev_addr
.bound_dev_if
)
2779 ndev
= dev_get_by_index(addr
->dev_addr
.net
,
2780 addr
->dev_addr
.bound_dev_if
);
2784 supported_gids
= roce_gid_type_mask_support(id_priv
->id
.device
,
2785 id_priv
->id
.port_num
);
2786 gid_type
= cma_route_gid_type(addr
->dev_addr
.network
,
2789 /* Use the hint from IP Stack to select GID Type */
2790 if (gid_type
< ib_network_to_gid_type(addr
->dev_addr
.network
))
2791 gid_type
= ib_network_to_gid_type(addr
->dev_addr
.network
);
2792 route
->path_rec
->rec_type
= sa_conv_gid_to_pathrec_type(gid_type
);
2794 route
->path_rec
->roce
.route_resolved
= true;
2795 sa_path_set_dmac(route
->path_rec
, addr
->dev_addr
.dst_dev_addr
);
2799 int rdma_set_ib_path(struct rdma_cm_id
*id
,
2800 struct sa_path_rec
*path_rec
)
2802 struct rdma_id_private
*id_priv
;
2803 struct net_device
*ndev
;
2806 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2807 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2808 RDMA_CM_ROUTE_RESOLVED
))
2811 id
->route
.path_rec
= kmemdup(path_rec
, sizeof(*path_rec
),
2813 if (!id
->route
.path_rec
) {
2818 if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
2819 ndev
= cma_iboe_set_path_rec_l2_fields(id_priv
);
2827 id
->route
.num_paths
= 1;
2831 kfree(id
->route
.path_rec
);
2832 id
->route
.path_rec
= NULL
;
2834 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_ADDR_RESOLVED
);
2837 EXPORT_SYMBOL(rdma_set_ib_path
);
2839 static int cma_resolve_iw_route(struct rdma_id_private
*id_priv
)
2841 struct cma_work
*work
;
2843 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2847 cma_init_resolve_route_work(work
, id_priv
);
2848 queue_work(cma_wq
, &work
->work
);
2852 static int get_vlan_ndev_tc(struct net_device
*vlan_ndev
, int prio
)
2854 struct net_device
*dev
;
2856 dev
= vlan_dev_real_dev(vlan_ndev
);
2858 return netdev_get_prio_tc_map(dev
, prio
);
2860 return (vlan_dev_get_egress_qos_mask(vlan_ndev
, prio
) &
2861 VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
2864 struct iboe_prio_tc_map
{
2870 static int get_lower_vlan_dev_tc(struct net_device
*dev
, void *data
)
2872 struct iboe_prio_tc_map
*map
= data
;
2874 if (is_vlan_dev(dev
))
2875 map
->output_tc
= get_vlan_ndev_tc(dev
, map
->input_prio
);
2876 else if (dev
->num_tc
)
2877 map
->output_tc
= netdev_get_prio_tc_map(dev
, map
->input_prio
);
2880 /* We are interested only in first level VLAN device, so always
2881 * return 1 to stop iterating over next level devices.
2887 static int iboe_tos_to_sl(struct net_device
*ndev
, int tos
)
2889 struct iboe_prio_tc_map prio_tc_map
= {};
2890 int prio
= rt_tos2priority(tos
);
2892 /* If VLAN device, get it directly from the VLAN netdev */
2893 if (is_vlan_dev(ndev
))
2894 return get_vlan_ndev_tc(ndev
, prio
);
2896 prio_tc_map
.input_prio
= prio
;
2898 netdev_walk_all_lower_dev_rcu(ndev
,
2899 get_lower_vlan_dev_tc
,
2902 /* If map is found from lower device, use it; Otherwise
2903 * continue with the current netdevice to get priority to tc map.
2905 if (prio_tc_map
.found
)
2906 return prio_tc_map
.output_tc
;
2907 else if (ndev
->num_tc
)
2908 return netdev_get_prio_tc_map(ndev
, prio
);
2913 static int cma_resolve_iboe_route(struct rdma_id_private
*id_priv
)
2915 struct rdma_route
*route
= &id_priv
->id
.route
;
2916 struct rdma_addr
*addr
= &route
->addr
;
2917 struct cma_work
*work
;
2919 struct net_device
*ndev
;
2921 u8 default_roce_tos
= id_priv
->cma_dev
->default_roce_tos
[id_priv
->id
.port_num
-
2922 rdma_start_port(id_priv
->cma_dev
->device
)];
2923 u8 tos
= id_priv
->tos_set
? id_priv
->tos
: default_roce_tos
;
2926 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2930 route
->path_rec
= kzalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2931 if (!route
->path_rec
) {
2936 route
->num_paths
= 1;
2938 ndev
= cma_iboe_set_path_rec_l2_fields(id_priv
);
2944 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
2945 &route
->path_rec
->sgid
);
2946 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
,
2947 &route
->path_rec
->dgid
);
2949 if (((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
)->sa_family
!= AF_IB
)
2950 /* TODO: get the hoplimit from the inet/inet6 device */
2951 route
->path_rec
->hop_limit
= addr
->dev_addr
.hoplimit
;
2953 route
->path_rec
->hop_limit
= 1;
2954 route
->path_rec
->reversible
= 1;
2955 route
->path_rec
->pkey
= cpu_to_be16(0xffff);
2956 route
->path_rec
->mtu_selector
= IB_SA_EQ
;
2957 route
->path_rec
->sl
= iboe_tos_to_sl(ndev
, tos
);
2958 route
->path_rec
->traffic_class
= tos
;
2959 route
->path_rec
->mtu
= iboe_get_mtu(ndev
->mtu
);
2960 route
->path_rec
->rate_selector
= IB_SA_EQ
;
2961 route
->path_rec
->rate
= iboe_get_rate(ndev
);
2963 route
->path_rec
->packet_life_time_selector
= IB_SA_EQ
;
2964 /* In case ACK timeout is set, use this value to calculate
2965 * PacketLifeTime. As per IBTA 12.7.34,
2966 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay).
2967 * Assuming a negligible local ACK delay, we can use
2968 * PacketLifeTime = local ACK timeout/2
2969 * as a reasonable approximation for RoCE networks.
2971 route
->path_rec
->packet_life_time
= id_priv
->timeout_set
?
2972 id_priv
->timeout
- 1 : CMA_IBOE_PACKET_LIFETIME
;
2974 if (!route
->path_rec
->mtu
) {
2979 cma_init_resolve_route_work(work
, id_priv
);
2980 queue_work(cma_wq
, &work
->work
);
2985 kfree(route
->path_rec
);
2986 route
->path_rec
= NULL
;
2987 route
->num_paths
= 0;
2993 int rdma_resolve_route(struct rdma_cm_id
*id
, unsigned long timeout_ms
)
2995 struct rdma_id_private
*id_priv
;
2998 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2999 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
, RDMA_CM_ROUTE_QUERY
))
3002 cma_id_get(id_priv
);
3003 if (rdma_cap_ib_sa(id
->device
, id
->port_num
))
3004 ret
= cma_resolve_ib_route(id_priv
, timeout_ms
);
3005 else if (rdma_protocol_roce(id
->device
, id
->port_num
))
3006 ret
= cma_resolve_iboe_route(id_priv
);
3007 else if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
3008 ret
= cma_resolve_iw_route(id_priv
);
3017 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_QUERY
, RDMA_CM_ADDR_RESOLVED
);
3018 cma_id_put(id_priv
);
3021 EXPORT_SYMBOL(rdma_resolve_route
);
3023 static void cma_set_loopback(struct sockaddr
*addr
)
3025 switch (addr
->sa_family
) {
3027 ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
3030 ipv6_addr_set(&((struct sockaddr_in6
*) addr
)->sin6_addr
,
3034 ib_addr_set(&((struct sockaddr_ib
*) addr
)->sib_addr
,
3040 static int cma_bind_loopback(struct rdma_id_private
*id_priv
)
3042 struct cma_device
*cma_dev
, *cur_dev
;
3044 enum ib_port_state port_state
;
3051 list_for_each_entry(cur_dev
, &dev_list
, list
) {
3052 if (cma_family(id_priv
) == AF_IB
&&
3053 !rdma_cap_ib_cm(cur_dev
->device
, 1))
3059 rdma_for_each_port (cur_dev
->device
, p
) {
3060 if (!ib_get_cached_port_state(cur_dev
->device
, p
, &port_state
) &&
3061 port_state
== IB_PORT_ACTIVE
) {
3076 ret
= rdma_query_gid(cma_dev
->device
, p
, 0, &gid
);
3080 ret
= ib_get_cached_pkey(cma_dev
->device
, p
, 0, &pkey
);
3084 id_priv
->id
.route
.addr
.dev_addr
.dev_type
=
3085 (rdma_protocol_ib(cma_dev
->device
, p
)) ?
3086 ARPHRD_INFINIBAND
: ARPHRD_ETHER
;
3088 rdma_addr_set_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
3089 ib_addr_set_pkey(&id_priv
->id
.route
.addr
.dev_addr
, pkey
);
3090 id_priv
->id
.port_num
= p
;
3091 cma_attach_to_dev(id_priv
, cma_dev
);
3092 cma_set_loopback(cma_src_addr(id_priv
));
3094 mutex_unlock(&lock
);
3098 static void addr_handler(int status
, struct sockaddr
*src_addr
,
3099 struct rdma_dev_addr
*dev_addr
, void *context
)
3101 struct rdma_id_private
*id_priv
= context
;
3102 struct rdma_cm_event event
= {};
3103 struct sockaddr
*addr
;
3104 struct sockaddr_storage old_addr
;
3106 mutex_lock(&id_priv
->handler_mutex
);
3107 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
,
3108 RDMA_CM_ADDR_RESOLVED
))
3112 * Store the previous src address, so that if we fail to acquire
3113 * matching rdma device, old address can be restored back, which helps
3114 * to cancel the cma listen operation correctly.
3116 addr
= cma_src_addr(id_priv
);
3117 memcpy(&old_addr
, addr
, rdma_addr_size(addr
));
3118 memcpy(addr
, src_addr
, rdma_addr_size(src_addr
));
3119 if (!status
&& !id_priv
->cma_dev
) {
3120 status
= cma_acquire_dev_by_src_ip(id_priv
);
3122 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
3124 } else if (status
) {
3125 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status
);
3129 memcpy(addr
, &old_addr
,
3130 rdma_addr_size((struct sockaddr
*)&old_addr
));
3131 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
3132 RDMA_CM_ADDR_BOUND
))
3134 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
3135 event
.status
= status
;
3137 event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
3139 if (cma_cm_event_handler(id_priv
, &event
)) {
3140 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3141 mutex_unlock(&id_priv
->handler_mutex
);
3142 rdma_destroy_id(&id_priv
->id
);
3146 mutex_unlock(&id_priv
->handler_mutex
);
3149 static int cma_resolve_loopback(struct rdma_id_private
*id_priv
)
3151 struct cma_work
*work
;
3155 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3159 if (!id_priv
->cma_dev
) {
3160 ret
= cma_bind_loopback(id_priv
);
3165 rdma_addr_get_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
3166 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
3168 enqueue_resolve_addr_work(work
, id_priv
);
3175 static int cma_resolve_ib_addr(struct rdma_id_private
*id_priv
)
3177 struct cma_work
*work
;
3180 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3184 if (!id_priv
->cma_dev
) {
3185 ret
= cma_resolve_ib_dev(id_priv
);
3190 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, (union ib_gid
*)
3191 &(((struct sockaddr_ib
*) &id_priv
->id
.route
.addr
.dst_addr
)->sib_addr
));
3193 enqueue_resolve_addr_work(work
, id_priv
);
3200 static int cma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
3201 const struct sockaddr
*dst_addr
)
3203 if (!src_addr
|| !src_addr
->sa_family
) {
3204 src_addr
= (struct sockaddr
*) &id
->route
.addr
.src_addr
;
3205 src_addr
->sa_family
= dst_addr
->sa_family
;
3206 if (IS_ENABLED(CONFIG_IPV6
) &&
3207 dst_addr
->sa_family
== AF_INET6
) {
3208 struct sockaddr_in6
*src_addr6
= (struct sockaddr_in6
*) src_addr
;
3209 struct sockaddr_in6
*dst_addr6
= (struct sockaddr_in6
*) dst_addr
;
3210 src_addr6
->sin6_scope_id
= dst_addr6
->sin6_scope_id
;
3211 if (ipv6_addr_type(&dst_addr6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
3212 id
->route
.addr
.dev_addr
.bound_dev_if
= dst_addr6
->sin6_scope_id
;
3213 } else if (dst_addr
->sa_family
== AF_IB
) {
3214 ((struct sockaddr_ib
*) src_addr
)->sib_pkey
=
3215 ((struct sockaddr_ib
*) dst_addr
)->sib_pkey
;
3218 return rdma_bind_addr(id
, src_addr
);
3221 int rdma_resolve_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
3222 const struct sockaddr
*dst_addr
, unsigned long timeout_ms
)
3224 struct rdma_id_private
*id_priv
;
3227 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3228 memcpy(cma_dst_addr(id_priv
), dst_addr
, rdma_addr_size(dst_addr
));
3229 if (id_priv
->state
== RDMA_CM_IDLE
) {
3230 ret
= cma_bind_addr(id
, src_addr
, dst_addr
);
3232 memset(cma_dst_addr(id_priv
), 0,
3233 rdma_addr_size(dst_addr
));
3238 if (cma_family(id_priv
) != dst_addr
->sa_family
) {
3239 memset(cma_dst_addr(id_priv
), 0, rdma_addr_size(dst_addr
));
3243 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_ADDR_QUERY
)) {
3244 memset(cma_dst_addr(id_priv
), 0, rdma_addr_size(dst_addr
));
3248 if (cma_any_addr(dst_addr
)) {
3249 ret
= cma_resolve_loopback(id_priv
);
3251 if (dst_addr
->sa_family
== AF_IB
) {
3252 ret
= cma_resolve_ib_addr(id_priv
);
3254 ret
= rdma_resolve_ip(cma_src_addr(id_priv
), dst_addr
,
3255 &id
->route
.addr
.dev_addr
,
3256 timeout_ms
, addr_handler
,
3265 cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
, RDMA_CM_ADDR_BOUND
);
3268 EXPORT_SYMBOL(rdma_resolve_addr
);
3270 int rdma_set_reuseaddr(struct rdma_cm_id
*id
, int reuse
)
3272 struct rdma_id_private
*id_priv
;
3273 unsigned long flags
;
3276 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3277 spin_lock_irqsave(&id_priv
->lock
, flags
);
3278 if (reuse
|| id_priv
->state
== RDMA_CM_IDLE
) {
3279 id_priv
->reuseaddr
= reuse
;
3284 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
3287 EXPORT_SYMBOL(rdma_set_reuseaddr
);
3289 int rdma_set_afonly(struct rdma_cm_id
*id
, int afonly
)
3291 struct rdma_id_private
*id_priv
;
3292 unsigned long flags
;
3295 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3296 spin_lock_irqsave(&id_priv
->lock
, flags
);
3297 if (id_priv
->state
== RDMA_CM_IDLE
|| id_priv
->state
== RDMA_CM_ADDR_BOUND
) {
3298 id_priv
->options
|= (1 << CMA_OPTION_AFONLY
);
3299 id_priv
->afonly
= afonly
;
3304 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
3307 EXPORT_SYMBOL(rdma_set_afonly
);
3309 static void cma_bind_port(struct rdma_bind_list
*bind_list
,
3310 struct rdma_id_private
*id_priv
)
3312 struct sockaddr
*addr
;
3313 struct sockaddr_ib
*sib
;
3317 lockdep_assert_held(&lock
);
3319 addr
= cma_src_addr(id_priv
);
3320 port
= htons(bind_list
->port
);
3322 switch (addr
->sa_family
) {
3324 ((struct sockaddr_in
*) addr
)->sin_port
= port
;
3327 ((struct sockaddr_in6
*) addr
)->sin6_port
= port
;
3330 sib
= (struct sockaddr_ib
*) addr
;
3331 sid
= be64_to_cpu(sib
->sib_sid
);
3332 mask
= be64_to_cpu(sib
->sib_sid_mask
);
3333 sib
->sib_sid
= cpu_to_be64((sid
& mask
) | (u64
) ntohs(port
));
3334 sib
->sib_sid_mask
= cpu_to_be64(~0ULL);
3337 id_priv
->bind_list
= bind_list
;
3338 hlist_add_head(&id_priv
->node
, &bind_list
->owners
);
3341 static int cma_alloc_port(enum rdma_ucm_port_space ps
,
3342 struct rdma_id_private
*id_priv
, unsigned short snum
)
3344 struct rdma_bind_list
*bind_list
;
3347 lockdep_assert_held(&lock
);
3349 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
3353 ret
= cma_ps_alloc(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, bind_list
,
3359 bind_list
->port
= snum
;
3360 cma_bind_port(bind_list
, id_priv
);
3364 return ret
== -ENOSPC
? -EADDRNOTAVAIL
: ret
;
3367 static int cma_port_is_unique(struct rdma_bind_list
*bind_list
,
3368 struct rdma_id_private
*id_priv
)
3370 struct rdma_id_private
*cur_id
;
3371 struct sockaddr
*daddr
= cma_dst_addr(id_priv
);
3372 struct sockaddr
*saddr
= cma_src_addr(id_priv
);
3373 __be16 dport
= cma_port(daddr
);
3375 lockdep_assert_held(&lock
);
3377 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
3378 struct sockaddr
*cur_daddr
= cma_dst_addr(cur_id
);
3379 struct sockaddr
*cur_saddr
= cma_src_addr(cur_id
);
3380 __be16 cur_dport
= cma_port(cur_daddr
);
3382 if (id_priv
== cur_id
)
3385 /* different dest port -> unique */
3386 if (!cma_any_port(daddr
) &&
3387 !cma_any_port(cur_daddr
) &&
3388 (dport
!= cur_dport
))
3391 /* different src address -> unique */
3392 if (!cma_any_addr(saddr
) &&
3393 !cma_any_addr(cur_saddr
) &&
3394 cma_addr_cmp(saddr
, cur_saddr
))
3397 /* different dst address -> unique */
3398 if (!cma_any_addr(daddr
) &&
3399 !cma_any_addr(cur_daddr
) &&
3400 cma_addr_cmp(daddr
, cur_daddr
))
3403 return -EADDRNOTAVAIL
;
3408 static int cma_alloc_any_port(enum rdma_ucm_port_space ps
,
3409 struct rdma_id_private
*id_priv
)
3411 static unsigned int last_used_port
;
3412 int low
, high
, remaining
;
3414 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
3416 lockdep_assert_held(&lock
);
3418 inet_get_local_port_range(net
, &low
, &high
);
3419 remaining
= (high
- low
) + 1;
3420 rover
= prandom_u32() % remaining
+ low
;
3422 if (last_used_port
!= rover
) {
3423 struct rdma_bind_list
*bind_list
;
3426 bind_list
= cma_ps_find(net
, ps
, (unsigned short)rover
);
3429 ret
= cma_alloc_port(ps
, id_priv
, rover
);
3431 ret
= cma_port_is_unique(bind_list
, id_priv
);
3433 cma_bind_port(bind_list
, id_priv
);
3436 * Remember previously used port number in order to avoid
3437 * re-using same port immediately after it is closed.
3440 last_used_port
= rover
;
3441 if (ret
!= -EADDRNOTAVAIL
)
3446 if ((rover
< low
) || (rover
> high
))
3450 return -EADDRNOTAVAIL
;
3454 * Check that the requested port is available. This is called when trying to
3455 * bind to a specific port, or when trying to listen on a bound port. In
3456 * the latter case, the provided id_priv may already be on the bind_list, but
3457 * we still need to check that it's okay to start listening.
3459 static int cma_check_port(struct rdma_bind_list
*bind_list
,
3460 struct rdma_id_private
*id_priv
, uint8_t reuseaddr
)
3462 struct rdma_id_private
*cur_id
;
3463 struct sockaddr
*addr
, *cur_addr
;
3465 lockdep_assert_held(&lock
);
3467 addr
= cma_src_addr(id_priv
);
3468 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
3469 if (id_priv
== cur_id
)
3472 if ((cur_id
->state
!= RDMA_CM_LISTEN
) && reuseaddr
&&
3476 cur_addr
= cma_src_addr(cur_id
);
3477 if (id_priv
->afonly
&& cur_id
->afonly
&&
3478 (addr
->sa_family
!= cur_addr
->sa_family
))
3481 if (cma_any_addr(addr
) || cma_any_addr(cur_addr
))
3482 return -EADDRNOTAVAIL
;
3484 if (!cma_addr_cmp(addr
, cur_addr
))
3490 static int cma_use_port(enum rdma_ucm_port_space ps
,
3491 struct rdma_id_private
*id_priv
)
3493 struct rdma_bind_list
*bind_list
;
3494 unsigned short snum
;
3497 lockdep_assert_held(&lock
);
3499 snum
= ntohs(cma_port(cma_src_addr(id_priv
)));
3500 if (snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
3503 bind_list
= cma_ps_find(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, snum
);
3505 ret
= cma_alloc_port(ps
, id_priv
, snum
);
3507 ret
= cma_check_port(bind_list
, id_priv
, id_priv
->reuseaddr
);
3509 cma_bind_port(bind_list
, id_priv
);
3514 static int cma_bind_listen(struct rdma_id_private
*id_priv
)
3516 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
3520 if (bind_list
->owners
.first
->next
)
3521 ret
= cma_check_port(bind_list
, id_priv
, 0);
3522 mutex_unlock(&lock
);
3526 static enum rdma_ucm_port_space
3527 cma_select_inet_ps(struct rdma_id_private
*id_priv
)
3529 switch (id_priv
->id
.ps
) {
3534 return id_priv
->id
.ps
;
3541 static enum rdma_ucm_port_space
3542 cma_select_ib_ps(struct rdma_id_private
*id_priv
)
3544 enum rdma_ucm_port_space ps
= 0;
3545 struct sockaddr_ib
*sib
;
3546 u64 sid_ps
, mask
, sid
;
3548 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
3549 mask
= be64_to_cpu(sib
->sib_sid_mask
) & RDMA_IB_IP_PS_MASK
;
3550 sid
= be64_to_cpu(sib
->sib_sid
) & mask
;
3552 if ((id_priv
->id
.ps
== RDMA_PS_IB
) && (sid
== (RDMA_IB_IP_PS_IB
& mask
))) {
3553 sid_ps
= RDMA_IB_IP_PS_IB
;
3555 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_TCP
)) &&
3556 (sid
== (RDMA_IB_IP_PS_TCP
& mask
))) {
3557 sid_ps
= RDMA_IB_IP_PS_TCP
;
3559 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_UDP
)) &&
3560 (sid
== (RDMA_IB_IP_PS_UDP
& mask
))) {
3561 sid_ps
= RDMA_IB_IP_PS_UDP
;
3566 sib
->sib_sid
= cpu_to_be64(sid_ps
| ntohs(cma_port((struct sockaddr
*) sib
)));
3567 sib
->sib_sid_mask
= cpu_to_be64(RDMA_IB_IP_PS_MASK
|
3568 be64_to_cpu(sib
->sib_sid_mask
));
3573 static int cma_get_port(struct rdma_id_private
*id_priv
)
3575 enum rdma_ucm_port_space ps
;
3578 if (cma_family(id_priv
) != AF_IB
)
3579 ps
= cma_select_inet_ps(id_priv
);
3581 ps
= cma_select_ib_ps(id_priv
);
3583 return -EPROTONOSUPPORT
;
3586 if (cma_any_port(cma_src_addr(id_priv
)))
3587 ret
= cma_alloc_any_port(ps
, id_priv
);
3589 ret
= cma_use_port(ps
, id_priv
);
3590 mutex_unlock(&lock
);
3595 static int cma_check_linklocal(struct rdma_dev_addr
*dev_addr
,
3596 struct sockaddr
*addr
)
3598 #if IS_ENABLED(CONFIG_IPV6)
3599 struct sockaddr_in6
*sin6
;
3601 if (addr
->sa_family
!= AF_INET6
)
3604 sin6
= (struct sockaddr_in6
*) addr
;
3606 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
3609 if (!sin6
->sin6_scope_id
)
3612 dev_addr
->bound_dev_if
= sin6
->sin6_scope_id
;
3617 int rdma_listen(struct rdma_cm_id
*id
, int backlog
)
3619 struct rdma_id_private
*id_priv
;
3622 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3623 if (id_priv
->state
== RDMA_CM_IDLE
) {
3624 id
->route
.addr
.src_addr
.ss_family
= AF_INET
;
3625 ret
= rdma_bind_addr(id
, cma_src_addr(id_priv
));
3630 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_LISTEN
))
3633 if (id_priv
->reuseaddr
) {
3634 ret
= cma_bind_listen(id_priv
);
3639 id_priv
->backlog
= backlog
;
3641 if (rdma_cap_ib_cm(id
->device
, 1)) {
3642 ret
= cma_ib_listen(id_priv
);
3645 } else if (rdma_cap_iw_cm(id
->device
, 1)) {
3646 ret
= cma_iw_listen(id_priv
, backlog
);
3654 cma_listen_on_all(id_priv
);
3658 id_priv
->backlog
= 0;
3659 cma_comp_exch(id_priv
, RDMA_CM_LISTEN
, RDMA_CM_ADDR_BOUND
);
3662 EXPORT_SYMBOL(rdma_listen
);
3664 int rdma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
3666 struct rdma_id_private
*id_priv
;
3668 struct sockaddr
*daddr
;
3670 if (addr
->sa_family
!= AF_INET
&& addr
->sa_family
!= AF_INET6
&&
3671 addr
->sa_family
!= AF_IB
)
3672 return -EAFNOSUPPORT
;
3674 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3675 if (!cma_comp_exch(id_priv
, RDMA_CM_IDLE
, RDMA_CM_ADDR_BOUND
))
3678 ret
= cma_check_linklocal(&id
->route
.addr
.dev_addr
, addr
);
3682 memcpy(cma_src_addr(id_priv
), addr
, rdma_addr_size(addr
));
3683 if (!cma_any_addr(addr
)) {
3684 ret
= cma_translate_addr(addr
, &id
->route
.addr
.dev_addr
);
3688 ret
= cma_acquire_dev_by_src_ip(id_priv
);
3693 if (!(id_priv
->options
& (1 << CMA_OPTION_AFONLY
))) {
3694 if (addr
->sa_family
== AF_INET
)
3695 id_priv
->afonly
= 1;
3696 #if IS_ENABLED(CONFIG_IPV6)
3697 else if (addr
->sa_family
== AF_INET6
) {
3698 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
3700 id_priv
->afonly
= net
->ipv6
.sysctl
.bindv6only
;
3704 daddr
= cma_dst_addr(id_priv
);
3705 daddr
->sa_family
= addr
->sa_family
;
3707 ret
= cma_get_port(id_priv
);
3713 rdma_restrack_del(&id_priv
->res
);
3714 if (id_priv
->cma_dev
)
3715 cma_release_dev(id_priv
);
3717 cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_IDLE
);
3720 EXPORT_SYMBOL(rdma_bind_addr
);
3722 static int cma_format_hdr(void *hdr
, struct rdma_id_private
*id_priv
)
3724 struct cma_hdr
*cma_hdr
;
3727 cma_hdr
->cma_version
= CMA_VERSION
;
3728 if (cma_family(id_priv
) == AF_INET
) {
3729 struct sockaddr_in
*src4
, *dst4
;
3731 src4
= (struct sockaddr_in
*) cma_src_addr(id_priv
);
3732 dst4
= (struct sockaddr_in
*) cma_dst_addr(id_priv
);
3734 cma_set_ip_ver(cma_hdr
, 4);
3735 cma_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
3736 cma_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
3737 cma_hdr
->port
= src4
->sin_port
;
3738 } else if (cma_family(id_priv
) == AF_INET6
) {
3739 struct sockaddr_in6
*src6
, *dst6
;
3741 src6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
3742 dst6
= (struct sockaddr_in6
*) cma_dst_addr(id_priv
);
3744 cma_set_ip_ver(cma_hdr
, 6);
3745 cma_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
3746 cma_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
3747 cma_hdr
->port
= src6
->sin6_port
;
3752 static int cma_sidr_rep_handler(struct ib_cm_id
*cm_id
,
3753 const struct ib_cm_event
*ib_event
)
3755 struct rdma_id_private
*id_priv
= cm_id
->context
;
3756 struct rdma_cm_event event
= {};
3757 const struct ib_cm_sidr_rep_event_param
*rep
=
3758 &ib_event
->param
.sidr_rep_rcvd
;
3761 mutex_lock(&id_priv
->handler_mutex
);
3762 if (id_priv
->state
!= RDMA_CM_CONNECT
)
3765 switch (ib_event
->event
) {
3766 case IB_CM_SIDR_REQ_ERROR
:
3767 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3768 event
.status
= -ETIMEDOUT
;
3770 case IB_CM_SIDR_REP_RECEIVED
:
3771 event
.param
.ud
.private_data
= ib_event
->private_data
;
3772 event
.param
.ud
.private_data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
3773 if (rep
->status
!= IB_SIDR_SUCCESS
) {
3774 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3775 event
.status
= ib_event
->param
.sidr_rep_rcvd
.status
;
3776 pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
3780 ret
= cma_set_qkey(id_priv
, rep
->qkey
);
3782 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret
);
3783 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
3787 ib_init_ah_attr_from_path(id_priv
->id
.device
,
3788 id_priv
->id
.port_num
,
3789 id_priv
->id
.route
.path_rec
,
3790 &event
.param
.ud
.ah_attr
,
3792 event
.param
.ud
.qp_num
= rep
->qpn
;
3793 event
.param
.ud
.qkey
= rep
->qkey
;
3794 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
3798 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3803 ret
= cma_cm_event_handler(id_priv
, &event
);
3805 rdma_destroy_ah_attr(&event
.param
.ud
.ah_attr
);
3807 /* Destroy the CM ID by returning a non-zero value. */
3808 id_priv
->cm_id
.ib
= NULL
;
3809 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3810 mutex_unlock(&id_priv
->handler_mutex
);
3811 rdma_destroy_id(&id_priv
->id
);
3815 mutex_unlock(&id_priv
->handler_mutex
);
3819 static int cma_resolve_ib_udp(struct rdma_id_private
*id_priv
,
3820 struct rdma_conn_param
*conn_param
)
3822 struct ib_cm_sidr_req_param req
;
3823 struct ib_cm_id
*id
;
3828 memset(&req
, 0, sizeof req
);
3829 offset
= cma_user_data_offset(id_priv
);
3830 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3831 if (req
.private_data_len
< conn_param
->private_data_len
)
3834 if (req
.private_data_len
) {
3835 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3839 private_data
= NULL
;
3842 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3843 memcpy(private_data
+ offset
, conn_param
->private_data
,
3844 conn_param
->private_data_len
);
3847 ret
= cma_format_hdr(private_data
, id_priv
);
3850 req
.private_data
= private_data
;
3853 id
= ib_create_cm_id(id_priv
->id
.device
, cma_sidr_rep_handler
,
3859 id_priv
->cm_id
.ib
= id
;
3861 req
.path
= id_priv
->id
.route
.path_rec
;
3862 req
.sgid_attr
= id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
;
3863 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3864 req
.timeout_ms
= 1 << (CMA_CM_RESPONSE_TIMEOUT
- 8);
3865 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3867 trace_cm_send_sidr_req(id_priv
);
3868 ret
= ib_send_cm_sidr_req(id_priv
->cm_id
.ib
, &req
);
3870 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
3871 id_priv
->cm_id
.ib
= NULL
;
3874 kfree(private_data
);
3878 static int cma_connect_ib(struct rdma_id_private
*id_priv
,
3879 struct rdma_conn_param
*conn_param
)
3881 struct ib_cm_req_param req
;
3882 struct rdma_route
*route
;
3884 struct ib_cm_id
*id
;
3888 memset(&req
, 0, sizeof req
);
3889 offset
= cma_user_data_offset(id_priv
);
3890 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3891 if (req
.private_data_len
< conn_param
->private_data_len
)
3894 if (req
.private_data_len
) {
3895 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3899 private_data
= NULL
;
3902 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3903 memcpy(private_data
+ offset
, conn_param
->private_data
,
3904 conn_param
->private_data_len
);
3906 id
= ib_create_cm_id(id_priv
->id
.device
, cma_ib_handler
, id_priv
);
3911 id_priv
->cm_id
.ib
= id
;
3913 route
= &id_priv
->id
.route
;
3915 ret
= cma_format_hdr(private_data
, id_priv
);
3918 req
.private_data
= private_data
;
3921 req
.primary_path
= &route
->path_rec
[0];
3922 if (route
->num_paths
== 2)
3923 req
.alternate_path
= &route
->path_rec
[1];
3925 req
.ppath_sgid_attr
= id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
;
3926 /* Alternate path SGID attribute currently unsupported */
3927 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3928 req
.qp_num
= id_priv
->qp_num
;
3929 req
.qp_type
= id_priv
->id
.qp_type
;
3930 req
.starting_psn
= id_priv
->seq_num
;
3931 req
.responder_resources
= conn_param
->responder_resources
;
3932 req
.initiator_depth
= conn_param
->initiator_depth
;
3933 req
.flow_control
= conn_param
->flow_control
;
3934 req
.retry_count
= min_t(u8
, 7, conn_param
->retry_count
);
3935 req
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
3936 req
.remote_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3937 req
.local_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3938 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3939 req
.srq
= id_priv
->srq
? 1 : 0;
3941 trace_cm_send_req(id_priv
);
3942 ret
= ib_send_cm_req(id_priv
->cm_id
.ib
, &req
);
3944 if (ret
&& !IS_ERR(id
)) {
3945 ib_destroy_cm_id(id
);
3946 id_priv
->cm_id
.ib
= NULL
;
3949 kfree(private_data
);
3953 static int cma_connect_iw(struct rdma_id_private
*id_priv
,
3954 struct rdma_conn_param
*conn_param
)
3956 struct iw_cm_id
*cm_id
;
3958 struct iw_cm_conn_param iw_param
;
3960 cm_id
= iw_create_cm_id(id_priv
->id
.device
, cma_iw_handler
, id_priv
);
3962 return PTR_ERR(cm_id
);
3964 cm_id
->tos
= id_priv
->tos
;
3965 cm_id
->tos_set
= id_priv
->tos_set
;
3966 id_priv
->cm_id
.iw
= cm_id
;
3968 memcpy(&cm_id
->local_addr
, cma_src_addr(id_priv
),
3969 rdma_addr_size(cma_src_addr(id_priv
)));
3970 memcpy(&cm_id
->remote_addr
, cma_dst_addr(id_priv
),
3971 rdma_addr_size(cma_dst_addr(id_priv
)));
3973 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3978 iw_param
.ord
= conn_param
->initiator_depth
;
3979 iw_param
.ird
= conn_param
->responder_resources
;
3980 iw_param
.private_data
= conn_param
->private_data
;
3981 iw_param
.private_data_len
= conn_param
->private_data_len
;
3982 iw_param
.qpn
= id_priv
->id
.qp
? id_priv
->qp_num
: conn_param
->qp_num
;
3984 memset(&iw_param
, 0, sizeof iw_param
);
3985 iw_param
.qpn
= id_priv
->qp_num
;
3987 ret
= iw_cm_connect(cm_id
, &iw_param
);
3990 iw_destroy_cm_id(cm_id
);
3991 id_priv
->cm_id
.iw
= NULL
;
3996 int rdma_connect(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
3998 struct rdma_id_private
*id_priv
;
4001 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4002 if (!cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_CONNECT
))
4006 id_priv
->qp_num
= conn_param
->qp_num
;
4007 id_priv
->srq
= conn_param
->srq
;
4010 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
4011 if (id
->qp_type
== IB_QPT_UD
)
4012 ret
= cma_resolve_ib_udp(id_priv
, conn_param
);
4014 ret
= cma_connect_ib(id_priv
, conn_param
);
4015 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
4016 ret
= cma_connect_iw(id_priv
, conn_param
);
4024 cma_comp_exch(id_priv
, RDMA_CM_CONNECT
, RDMA_CM_ROUTE_RESOLVED
);
4027 EXPORT_SYMBOL(rdma_connect
);
4029 static int cma_accept_ib(struct rdma_id_private
*id_priv
,
4030 struct rdma_conn_param
*conn_param
)
4032 struct ib_cm_rep_param rep
;
4035 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
4039 ret
= cma_modify_qp_rts(id_priv
, conn_param
);
4043 memset(&rep
, 0, sizeof rep
);
4044 rep
.qp_num
= id_priv
->qp_num
;
4045 rep
.starting_psn
= id_priv
->seq_num
;
4046 rep
.private_data
= conn_param
->private_data
;
4047 rep
.private_data_len
= conn_param
->private_data_len
;
4048 rep
.responder_resources
= conn_param
->responder_resources
;
4049 rep
.initiator_depth
= conn_param
->initiator_depth
;
4050 rep
.failover_accepted
= 0;
4051 rep
.flow_control
= conn_param
->flow_control
;
4052 rep
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
4053 rep
.srq
= id_priv
->srq
? 1 : 0;
4055 trace_cm_send_rep(id_priv
);
4056 ret
= ib_send_cm_rep(id_priv
->cm_id
.ib
, &rep
);
4061 static int cma_accept_iw(struct rdma_id_private
*id_priv
,
4062 struct rdma_conn_param
*conn_param
)
4064 struct iw_cm_conn_param iw_param
;
4070 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
4074 iw_param
.ord
= conn_param
->initiator_depth
;
4075 iw_param
.ird
= conn_param
->responder_resources
;
4076 iw_param
.private_data
= conn_param
->private_data
;
4077 iw_param
.private_data_len
= conn_param
->private_data_len
;
4078 if (id_priv
->id
.qp
) {
4079 iw_param
.qpn
= id_priv
->qp_num
;
4081 iw_param
.qpn
= conn_param
->qp_num
;
4083 return iw_cm_accept(id_priv
->cm_id
.iw
, &iw_param
);
4086 static int cma_send_sidr_rep(struct rdma_id_private
*id_priv
,
4087 enum ib_cm_sidr_status status
, u32 qkey
,
4088 const void *private_data
, int private_data_len
)
4090 struct ib_cm_sidr_rep_param rep
;
4093 memset(&rep
, 0, sizeof rep
);
4094 rep
.status
= status
;
4095 if (status
== IB_SIDR_SUCCESS
) {
4096 ret
= cma_set_qkey(id_priv
, qkey
);
4099 rep
.qp_num
= id_priv
->qp_num
;
4100 rep
.qkey
= id_priv
->qkey
;
4102 rep
.private_data
= private_data
;
4103 rep
.private_data_len
= private_data_len
;
4105 trace_cm_send_sidr_rep(id_priv
);
4106 return ib_send_cm_sidr_rep(id_priv
->cm_id
.ib
, &rep
);
4109 int __rdma_accept(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
,
4112 struct rdma_id_private
*id_priv
;
4115 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4117 rdma_restrack_set_task(&id_priv
->res
, caller
);
4119 if (!cma_comp(id_priv
, RDMA_CM_CONNECT
))
4122 if (!id
->qp
&& conn_param
) {
4123 id_priv
->qp_num
= conn_param
->qp_num
;
4124 id_priv
->srq
= conn_param
->srq
;
4127 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
4128 if (id
->qp_type
== IB_QPT_UD
) {
4130 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
4132 conn_param
->private_data
,
4133 conn_param
->private_data_len
);
4135 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
4139 ret
= cma_accept_ib(id_priv
, conn_param
);
4141 ret
= cma_rep_recv(id_priv
);
4143 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
4144 ret
= cma_accept_iw(id_priv
, conn_param
);
4153 cma_modify_qp_err(id_priv
);
4154 rdma_reject(id
, NULL
, 0);
4157 EXPORT_SYMBOL(__rdma_accept
);
4159 int rdma_notify(struct rdma_cm_id
*id
, enum ib_event_type event
)
4161 struct rdma_id_private
*id_priv
;
4164 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4165 if (!id_priv
->cm_id
.ib
)
4168 switch (id
->device
->node_type
) {
4169 case RDMA_NODE_IB_CA
:
4170 ret
= ib_cm_notify(id_priv
->cm_id
.ib
, event
);
4178 EXPORT_SYMBOL(rdma_notify
);
4180 int rdma_reject(struct rdma_cm_id
*id
, const void *private_data
,
4181 u8 private_data_len
)
4183 struct rdma_id_private
*id_priv
;
4186 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4187 if (!id_priv
->cm_id
.ib
)
4190 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
4191 if (id
->qp_type
== IB_QPT_UD
) {
4192 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_REJECT
, 0,
4193 private_data
, private_data_len
);
4195 trace_cm_send_rej(id_priv
);
4196 ret
= ib_send_cm_rej(id_priv
->cm_id
.ib
,
4197 IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
4198 0, private_data
, private_data_len
);
4200 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
4201 ret
= iw_cm_reject(id_priv
->cm_id
.iw
,
4202 private_data
, private_data_len
);
4208 EXPORT_SYMBOL(rdma_reject
);
4210 int rdma_disconnect(struct rdma_cm_id
*id
)
4212 struct rdma_id_private
*id_priv
;
4215 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4216 if (!id_priv
->cm_id
.ib
)
4219 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
4220 ret
= cma_modify_qp_err(id_priv
);
4223 /* Initiate or respond to a disconnect. */
4224 trace_cm_disconnect(id_priv
);
4225 if (ib_send_cm_dreq(id_priv
->cm_id
.ib
, NULL
, 0)) {
4226 if (!ib_send_cm_drep(id_priv
->cm_id
.ib
, NULL
, 0))
4227 trace_cm_sent_drep(id_priv
);
4229 trace_cm_sent_dreq(id_priv
);
4231 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
4232 ret
= iw_cm_disconnect(id_priv
->cm_id
.iw
, 0);
4239 EXPORT_SYMBOL(rdma_disconnect
);
4241 static int cma_ib_mc_handler(int status
, struct ib_sa_multicast
*multicast
)
4243 struct rdma_id_private
*id_priv
;
4244 struct cma_multicast
*mc
= multicast
->context
;
4245 struct rdma_cm_event event
= {};
4248 id_priv
= mc
->id_priv
;
4249 mutex_lock(&id_priv
->handler_mutex
);
4250 if (id_priv
->state
!= RDMA_CM_ADDR_BOUND
&&
4251 id_priv
->state
!= RDMA_CM_ADDR_RESOLVED
)
4255 status
= cma_set_qkey(id_priv
, be32_to_cpu(multicast
->rec
.qkey
));
4257 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
4259 mutex_lock(&id_priv
->qp_mutex
);
4260 if (!status
&& id_priv
->id
.qp
) {
4261 status
= ib_attach_mcast(id_priv
->id
.qp
, &multicast
->rec
.mgid
,
4262 be16_to_cpu(multicast
->rec
.mlid
));
4264 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to attach QP. status %d\n",
4267 mutex_unlock(&id_priv
->qp_mutex
);
4269 event
.status
= status
;
4270 event
.param
.ud
.private_data
= mc
->context
;
4272 struct rdma_dev_addr
*dev_addr
=
4273 &id_priv
->id
.route
.addr
.dev_addr
;
4274 struct net_device
*ndev
=
4275 dev_get_by_index(dev_addr
->net
, dev_addr
->bound_dev_if
);
4276 enum ib_gid_type gid_type
=
4277 id_priv
->cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
4278 rdma_start_port(id_priv
->cma_dev
->device
)];
4280 event
.event
= RDMA_CM_EVENT_MULTICAST_JOIN
;
4281 ret
= ib_init_ah_from_mcmember(id_priv
->id
.device
,
4282 id_priv
->id
.port_num
,
4285 &event
.param
.ud
.ah_attr
);
4287 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
4289 event
.param
.ud
.qp_num
= 0xFFFFFF;
4290 event
.param
.ud
.qkey
= be32_to_cpu(multicast
->rec
.qkey
);
4294 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
4296 ret
= cma_cm_event_handler(id_priv
, &event
);
4298 rdma_destroy_ah_attr(&event
.param
.ud
.ah_attr
);
4300 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
4301 mutex_unlock(&id_priv
->handler_mutex
);
4302 rdma_destroy_id(&id_priv
->id
);
4307 mutex_unlock(&id_priv
->handler_mutex
);
4311 static void cma_set_mgid(struct rdma_id_private
*id_priv
,
4312 struct sockaddr
*addr
, union ib_gid
*mgid
)
4314 unsigned char mc_map
[MAX_ADDR_LEN
];
4315 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4316 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
4317 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
4319 if (cma_any_addr(addr
)) {
4320 memset(mgid
, 0, sizeof *mgid
);
4321 } else if ((addr
->sa_family
== AF_INET6
) &&
4322 ((be32_to_cpu(sin6
->sin6_addr
.s6_addr32
[0]) & 0xFFF0FFFF) ==
4324 /* IPv6 address is an SA assigned MGID. */
4325 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
4326 } else if (addr
->sa_family
== AF_IB
) {
4327 memcpy(mgid
, &((struct sockaddr_ib
*) addr
)->sib_addr
, sizeof *mgid
);
4328 } else if (addr
->sa_family
== AF_INET6
) {
4329 ipv6_ib_mc_map(&sin6
->sin6_addr
, dev_addr
->broadcast
, mc_map
);
4330 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
4331 mc_map
[7] = 0x01; /* Use RDMA CM signature */
4332 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
4334 ip_ib_mc_map(sin
->sin_addr
.s_addr
, dev_addr
->broadcast
, mc_map
);
4335 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
4336 mc_map
[7] = 0x01; /* Use RDMA CM signature */
4337 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
4341 static int cma_join_ib_multicast(struct rdma_id_private
*id_priv
,
4342 struct cma_multicast
*mc
)
4344 struct ib_sa_mcmember_rec rec
;
4345 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4346 ib_sa_comp_mask comp_mask
;
4349 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
4350 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
, id_priv
->id
.port_num
,
4355 ret
= cma_set_qkey(id_priv
, 0);
4359 cma_set_mgid(id_priv
, (struct sockaddr
*) &mc
->addr
, &rec
.mgid
);
4360 rec
.qkey
= cpu_to_be32(id_priv
->qkey
);
4361 rdma_addr_get_sgid(dev_addr
, &rec
.port_gid
);
4362 rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
4363 rec
.join_state
= mc
->join_state
;
4365 if ((rec
.join_state
== BIT(SENDONLY_FULLMEMBER_JOIN
)) &&
4366 (!ib_sa_sendonly_fullmem_support(&sa_client
,
4368 id_priv
->id
.port_num
))) {
4370 &id_priv
->id
.device
->dev
,
4371 "RDMA CM: port %u Unable to multicast join: SM doesn't support Send Only Full Member option\n",
4372 id_priv
->id
.port_num
);
4376 comp_mask
= IB_SA_MCMEMBER_REC_MGID
| IB_SA_MCMEMBER_REC_PORT_GID
|
4377 IB_SA_MCMEMBER_REC_PKEY
| IB_SA_MCMEMBER_REC_JOIN_STATE
|
4378 IB_SA_MCMEMBER_REC_QKEY
| IB_SA_MCMEMBER_REC_SL
|
4379 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
4380 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
4382 if (id_priv
->id
.ps
== RDMA_PS_IPOIB
)
4383 comp_mask
|= IB_SA_MCMEMBER_REC_RATE
|
4384 IB_SA_MCMEMBER_REC_RATE_SELECTOR
|
4385 IB_SA_MCMEMBER_REC_MTU_SELECTOR
|
4386 IB_SA_MCMEMBER_REC_MTU
|
4387 IB_SA_MCMEMBER_REC_HOP_LIMIT
;
4389 mc
->multicast
.ib
= ib_sa_join_multicast(&sa_client
, id_priv
->id
.device
,
4390 id_priv
->id
.port_num
, &rec
,
4391 comp_mask
, GFP_KERNEL
,
4392 cma_ib_mc_handler
, mc
);
4393 return PTR_ERR_OR_ZERO(mc
->multicast
.ib
);
4396 static void iboe_mcast_work_handler(struct work_struct
*work
)
4398 struct iboe_mcast_work
*mw
= container_of(work
, struct iboe_mcast_work
, work
);
4399 struct cma_multicast
*mc
= mw
->mc
;
4400 struct ib_sa_multicast
*m
= mc
->multicast
.ib
;
4402 mc
->multicast
.ib
->context
= mc
;
4403 cma_ib_mc_handler(0, m
);
4404 kref_put(&mc
->mcref
, release_mc
);
4408 static void cma_iboe_set_mgid(struct sockaddr
*addr
, union ib_gid
*mgid
,
4409 enum ib_gid_type gid_type
)
4411 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
4412 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
4414 if (cma_any_addr(addr
)) {
4415 memset(mgid
, 0, sizeof *mgid
);
4416 } else if (addr
->sa_family
== AF_INET6
) {
4417 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
4420 (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) ? 0 : 0xff;
4422 (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) ? 0 : 0x0e;
4431 mgid
->raw
[10] = 0xff;
4432 mgid
->raw
[11] = 0xff;
4433 *(__be32
*)(&mgid
->raw
[12]) = sin
->sin_addr
.s_addr
;
4437 static int cma_iboe_join_multicast(struct rdma_id_private
*id_priv
,
4438 struct cma_multicast
*mc
)
4440 struct iboe_mcast_work
*work
;
4441 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4443 struct sockaddr
*addr
= (struct sockaddr
*)&mc
->addr
;
4444 struct net_device
*ndev
= NULL
;
4445 enum ib_gid_type gid_type
;
4448 send_only
= mc
->join_state
== BIT(SENDONLY_FULLMEMBER_JOIN
);
4450 if (cma_zero_addr((struct sockaddr
*)&mc
->addr
))
4453 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
4457 mc
->multicast
.ib
= kzalloc(sizeof(struct ib_sa_multicast
), GFP_KERNEL
);
4458 if (!mc
->multicast
.ib
) {
4463 gid_type
= id_priv
->cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
4464 rdma_start_port(id_priv
->cma_dev
->device
)];
4465 cma_iboe_set_mgid(addr
, &mc
->multicast
.ib
->rec
.mgid
, gid_type
);
4467 mc
->multicast
.ib
->rec
.pkey
= cpu_to_be16(0xffff);
4468 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
4469 mc
->multicast
.ib
->rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
4471 if (dev_addr
->bound_dev_if
)
4472 ndev
= dev_get_by_index(dev_addr
->net
, dev_addr
->bound_dev_if
);
4477 mc
->multicast
.ib
->rec
.rate
= iboe_get_rate(ndev
);
4478 mc
->multicast
.ib
->rec
.hop_limit
= 1;
4479 mc
->multicast
.ib
->rec
.mtu
= iboe_get_mtu(ndev
->mtu
);
4481 if (addr
->sa_family
== AF_INET
) {
4482 if (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) {
4483 mc
->multicast
.ib
->rec
.hop_limit
= IPV6_DEFAULT_HOPLIMIT
;
4485 err
= cma_igmp_send(ndev
, &mc
->multicast
.ib
->rec
.mgid
,
4490 if (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
)
4494 if (err
|| !mc
->multicast
.ib
->rec
.mtu
) {
4499 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
4500 &mc
->multicast
.ib
->rec
.port_gid
);
4503 INIT_WORK(&work
->work
, iboe_mcast_work_handler
);
4504 kref_get(&mc
->mcref
);
4505 queue_work(cma_wq
, &work
->work
);
4510 kfree(mc
->multicast
.ib
);
4516 int rdma_join_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
,
4517 u8 join_state
, void *context
)
4519 struct rdma_id_private
*id_priv
;
4520 struct cma_multicast
*mc
;
4526 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4527 if (!cma_comp(id_priv
, RDMA_CM_ADDR_BOUND
) &&
4528 !cma_comp(id_priv
, RDMA_CM_ADDR_RESOLVED
))
4531 mc
= kmalloc(sizeof *mc
, GFP_KERNEL
);
4535 memcpy(&mc
->addr
, addr
, rdma_addr_size(addr
));
4536 mc
->context
= context
;
4537 mc
->id_priv
= id_priv
;
4538 mc
->join_state
= join_state
;
4540 if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
4541 kref_init(&mc
->mcref
);
4542 ret
= cma_iboe_join_multicast(id_priv
, mc
);
4545 } else if (rdma_cap_ib_mcast(id
->device
, id
->port_num
)) {
4546 ret
= cma_join_ib_multicast(id_priv
, mc
);
4554 spin_lock(&id_priv
->lock
);
4555 list_add(&mc
->list
, &id_priv
->mc_list
);
4556 spin_unlock(&id_priv
->lock
);
4563 EXPORT_SYMBOL(rdma_join_multicast
);
4565 void rdma_leave_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
4567 struct rdma_id_private
*id_priv
;
4568 struct cma_multicast
*mc
;
4570 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4571 spin_lock_irq(&id_priv
->lock
);
4572 list_for_each_entry(mc
, &id_priv
->mc_list
, list
) {
4573 if (!memcmp(&mc
->addr
, addr
, rdma_addr_size(addr
))) {
4574 list_del(&mc
->list
);
4575 spin_unlock_irq(&id_priv
->lock
);
4578 ib_detach_mcast(id
->qp
,
4579 &mc
->multicast
.ib
->rec
.mgid
,
4580 be16_to_cpu(mc
->multicast
.ib
->rec
.mlid
));
4582 BUG_ON(id_priv
->cma_dev
->device
!= id
->device
);
4584 if (rdma_cap_ib_mcast(id
->device
, id
->port_num
)) {
4585 ib_sa_free_multicast(mc
->multicast
.ib
);
4587 } else if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
4588 cma_leave_roce_mc_group(id_priv
, mc
);
4593 spin_unlock_irq(&id_priv
->lock
);
4595 EXPORT_SYMBOL(rdma_leave_multicast
);
4597 static int cma_netdev_change(struct net_device
*ndev
, struct rdma_id_private
*id_priv
)
4599 struct rdma_dev_addr
*dev_addr
;
4600 struct cma_ndev_work
*work
;
4602 dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4604 if ((dev_addr
->bound_dev_if
== ndev
->ifindex
) &&
4605 (net_eq(dev_net(ndev
), dev_addr
->net
)) &&
4606 memcmp(dev_addr
->src_dev_addr
, ndev
->dev_addr
, ndev
->addr_len
)) {
4607 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4608 ndev
->name
, &id_priv
->id
);
4609 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
4613 INIT_WORK(&work
->work
, cma_ndev_work_handler
);
4615 work
->event
.event
= RDMA_CM_EVENT_ADDR_CHANGE
;
4616 cma_id_get(id_priv
);
4617 queue_work(cma_wq
, &work
->work
);
4623 static int cma_netdev_callback(struct notifier_block
*self
, unsigned long event
,
4626 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
4627 struct cma_device
*cma_dev
;
4628 struct rdma_id_private
*id_priv
;
4629 int ret
= NOTIFY_DONE
;
4631 if (event
!= NETDEV_BONDING_FAILOVER
)
4634 if (!netif_is_bond_master(ndev
))
4638 list_for_each_entry(cma_dev
, &dev_list
, list
)
4639 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
4640 ret
= cma_netdev_change(ndev
, id_priv
);
4646 mutex_unlock(&lock
);
4650 static struct notifier_block cma_nb
= {
4651 .notifier_call
= cma_netdev_callback
4654 static void cma_add_one(struct ib_device
*device
)
4656 struct cma_device
*cma_dev
;
4657 struct rdma_id_private
*id_priv
;
4659 unsigned long supported_gids
= 0;
4661 cma_dev
= kmalloc(sizeof *cma_dev
, GFP_KERNEL
);
4665 cma_dev
->device
= device
;
4666 cma_dev
->default_gid_type
= kcalloc(device
->phys_port_cnt
,
4667 sizeof(*cma_dev
->default_gid_type
),
4669 if (!cma_dev
->default_gid_type
)
4672 cma_dev
->default_roce_tos
= kcalloc(device
->phys_port_cnt
,
4673 sizeof(*cma_dev
->default_roce_tos
),
4675 if (!cma_dev
->default_roce_tos
)
4678 rdma_for_each_port (device
, i
) {
4679 supported_gids
= roce_gid_type_mask_support(device
, i
);
4680 WARN_ON(!supported_gids
);
4681 if (supported_gids
& (1 << CMA_PREFERRED_ROCE_GID_TYPE
))
4682 cma_dev
->default_gid_type
[i
- rdma_start_port(device
)] =
4683 CMA_PREFERRED_ROCE_GID_TYPE
;
4685 cma_dev
->default_gid_type
[i
- rdma_start_port(device
)] =
4686 find_first_bit(&supported_gids
, BITS_PER_LONG
);
4687 cma_dev
->default_roce_tos
[i
- rdma_start_port(device
)] = 0;
4690 init_completion(&cma_dev
->comp
);
4691 refcount_set(&cma_dev
->refcount
, 1);
4692 INIT_LIST_HEAD(&cma_dev
->id_list
);
4693 ib_set_client_data(device
, &cma_client
, cma_dev
);
4696 list_add_tail(&cma_dev
->list
, &dev_list
);
4697 list_for_each_entry(id_priv
, &listen_any_list
, list
)
4698 cma_listen_on_dev(id_priv
, cma_dev
);
4699 mutex_unlock(&lock
);
4701 trace_cm_add_one(device
);
4705 kfree(cma_dev
->default_gid_type
);
4713 static int cma_remove_id_dev(struct rdma_id_private
*id_priv
)
4715 struct rdma_cm_event event
= {};
4716 enum rdma_cm_state state
;
4719 /* Record that we want to remove the device */
4720 state
= cma_exch(id_priv
, RDMA_CM_DEVICE_REMOVAL
);
4721 if (state
== RDMA_CM_DESTROYING
)
4724 cma_cancel_operation(id_priv
, state
);
4725 mutex_lock(&id_priv
->handler_mutex
);
4727 /* Check for destruction from another callback. */
4728 if (!cma_comp(id_priv
, RDMA_CM_DEVICE_REMOVAL
))
4731 event
.event
= RDMA_CM_EVENT_DEVICE_REMOVAL
;
4732 ret
= cma_cm_event_handler(id_priv
, &event
);
4734 mutex_unlock(&id_priv
->handler_mutex
);
4738 static void cma_process_remove(struct cma_device
*cma_dev
)
4740 struct rdma_id_private
*id_priv
;
4744 while (!list_empty(&cma_dev
->id_list
)) {
4745 id_priv
= list_entry(cma_dev
->id_list
.next
,
4746 struct rdma_id_private
, list
);
4748 list_del(&id_priv
->listen_list
);
4749 list_del_init(&id_priv
->list
);
4750 cma_id_get(id_priv
);
4751 mutex_unlock(&lock
);
4753 ret
= id_priv
->internal_id
? 1 : cma_remove_id_dev(id_priv
);
4754 cma_id_put(id_priv
);
4756 rdma_destroy_id(&id_priv
->id
);
4760 mutex_unlock(&lock
);
4762 cma_dev_put(cma_dev
);
4763 wait_for_completion(&cma_dev
->comp
);
4766 static void cma_remove_one(struct ib_device
*device
, void *client_data
)
4768 struct cma_device
*cma_dev
= client_data
;
4770 trace_cm_remove_one(device
);
4776 list_del(&cma_dev
->list
);
4777 mutex_unlock(&lock
);
4779 cma_process_remove(cma_dev
);
4780 kfree(cma_dev
->default_roce_tos
);
4781 kfree(cma_dev
->default_gid_type
);
4785 static int cma_init_net(struct net
*net
)
4787 struct cma_pernet
*pernet
= cma_pernet(net
);
4789 xa_init(&pernet
->tcp_ps
);
4790 xa_init(&pernet
->udp_ps
);
4791 xa_init(&pernet
->ipoib_ps
);
4792 xa_init(&pernet
->ib_ps
);
4797 static void cma_exit_net(struct net
*net
)
4799 struct cma_pernet
*pernet
= cma_pernet(net
);
4801 WARN_ON(!xa_empty(&pernet
->tcp_ps
));
4802 WARN_ON(!xa_empty(&pernet
->udp_ps
));
4803 WARN_ON(!xa_empty(&pernet
->ipoib_ps
));
4804 WARN_ON(!xa_empty(&pernet
->ib_ps
));
4807 static struct pernet_operations cma_pernet_operations
= {
4808 .init
= cma_init_net
,
4809 .exit
= cma_exit_net
,
4810 .id
= &cma_pernet_id
,
4811 .size
= sizeof(struct cma_pernet
),
4814 static int __init
cma_init(void)
4819 * There is a rare lock ordering dependency in cma_netdev_callback()
4820 * that only happens when bonding is enabled. Teach lockdep that rtnl
4821 * must never be nested under lock so it can find these without having
4822 * to test with bonding.
4824 if (IS_ENABLED(CONFIG_LOCKDEP
)) {
4827 mutex_unlock(&lock
);
4831 cma_wq
= alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM
);
4835 ret
= register_pernet_subsys(&cma_pernet_operations
);
4839 ib_sa_register_client(&sa_client
);
4840 register_netdevice_notifier(&cma_nb
);
4842 ret
= ib_register_client(&cma_client
);
4846 ret
= cma_configfs_init();
4853 ib_unregister_client(&cma_client
);
4855 unregister_netdevice_notifier(&cma_nb
);
4856 ib_sa_unregister_client(&sa_client
);
4857 unregister_pernet_subsys(&cma_pernet_operations
);
4859 destroy_workqueue(cma_wq
);
4863 static void __exit
cma_cleanup(void)
4865 cma_configfs_exit();
4866 ib_unregister_client(&cma_client
);
4867 unregister_netdevice_notifier(&cma_nb
);
4868 ib_sa_unregister_client(&sa_client
);
4869 unregister_pernet_subsys(&cma_pernet_operations
);
4870 destroy_workqueue(cma_wq
);
4873 module_init(cma_init
);
4874 module_exit(cma_cleanup
);