1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
4 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
5 * Copyright (c) 1999-2019, Mellanox Technologies, Inc. All rights reserved.
6 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
9 #include <linux/completion.h>
11 #include <linux/in6.h>
12 #include <linux/mutex.h>
13 #include <linux/random.h>
14 #include <linux/igmp.h>
15 #include <linux/xarray.h>
16 #include <linux/inetdevice.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <net/route.h>
21 #include <net/net_namespace.h>
22 #include <net/netns/generic.h>
25 #include <net/ip_fib.h>
26 #include <net/ip6_route.h>
28 #include <rdma/rdma_cm.h>
29 #include <rdma/rdma_cm_ib.h>
30 #include <rdma/rdma_netlink.h>
32 #include <rdma/ib_cache.h>
33 #include <rdma/ib_cm.h>
34 #include <rdma/ib_sa.h>
35 #include <rdma/iw_cm.h>
37 #include "core_priv.h"
39 #include "cma_trace.h"
41 MODULE_AUTHOR("Sean Hefty");
42 MODULE_DESCRIPTION("Generic RDMA CM Agent");
43 MODULE_LICENSE("Dual BSD/GPL");
45 #define CMA_CM_RESPONSE_TIMEOUT 20
46 #define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000
47 #define CMA_MAX_CM_RETRIES 15
48 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
49 #define CMA_IBOE_PACKET_LIFETIME 18
50 #define CMA_PREFERRED_ROCE_GID_TYPE IB_GID_TYPE_ROCE_UDP_ENCAP
52 static const char * const cma_events[] = {
53 [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved",
54 [RDMA_CM_EVENT_ADDR_ERROR] = "address error",
55 [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ",
56 [RDMA_CM_EVENT_ROUTE_ERROR] = "route error",
57 [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request",
58 [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
59 [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error",
60 [RDMA_CM_EVENT_UNREACHABLE] = "unreachable",
61 [RDMA_CM_EVENT_REJECTED] = "rejected",
62 [RDMA_CM_EVENT_ESTABLISHED] = "established",
63 [RDMA_CM_EVENT_DISCONNECTED] = "disconnected",
64 [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal",
65 [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join",
66 [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error",
67 [RDMA_CM_EVENT_ADDR_CHANGE] = "address change",
68 [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit",
71 static void cma_set_mgid(struct rdma_id_private *id_priv, struct sockaddr *addr,
74 const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
78 return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
79 cma_events[index] : "unrecognized event";
81 EXPORT_SYMBOL(rdma_event_msg);
83 const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,
86 if (rdma_ib_or_roce(id->device, id->port_num))
87 return ibcm_reject_msg(reason);
89 if (rdma_protocol_iwarp(id->device, id->port_num))
90 return iwcm_reject_msg(reason);
93 return "unrecognized transport";
95 EXPORT_SYMBOL(rdma_reject_msg);
98 * rdma_is_consumer_reject - return true if the consumer rejected the connect
100 * @id: Communication identifier that received the REJECT event.
101 * @reason: Value returned in the REJECT event status field.
103 static bool rdma_is_consumer_reject(struct rdma_cm_id *id, int reason)
105 if (rdma_ib_or_roce(id->device, id->port_num))
106 return reason == IB_CM_REJ_CONSUMER_DEFINED;
108 if (rdma_protocol_iwarp(id->device, id->port_num))
109 return reason == -ECONNREFUSED;
115 const void *rdma_consumer_reject_data(struct rdma_cm_id *id,
116 struct rdma_cm_event *ev, u8 *data_len)
120 if (rdma_is_consumer_reject(id, ev->status)) {
121 *data_len = ev->param.conn.private_data_len;
122 p = ev->param.conn.private_data;
129 EXPORT_SYMBOL(rdma_consumer_reject_data);
132 * rdma_iw_cm_id() - return the iw_cm_id pointer for this cm_id.
133 * @id: Communication Identifier
135 struct iw_cm_id *rdma_iw_cm_id(struct rdma_cm_id *id)
137 struct rdma_id_private *id_priv;
139 id_priv = container_of(id, struct rdma_id_private, id);
140 if (id->device->node_type == RDMA_NODE_RNIC)
141 return id_priv->cm_id.iw;
144 EXPORT_SYMBOL(rdma_iw_cm_id);
147 * rdma_res_to_id() - return the rdma_cm_id pointer for this restrack.
148 * @res: rdma resource tracking entry pointer
150 struct rdma_cm_id *rdma_res_to_id(struct rdma_restrack_entry *res)
152 struct rdma_id_private *id_priv =
153 container_of(res, struct rdma_id_private, res);
157 EXPORT_SYMBOL(rdma_res_to_id);
159 static int cma_add_one(struct ib_device *device);
160 static void cma_remove_one(struct ib_device *device, void *client_data);
162 static struct ib_client cma_client = {
165 .remove = cma_remove_one
168 static struct ib_sa_client sa_client;
169 static LIST_HEAD(dev_list);
170 static LIST_HEAD(listen_any_list);
171 static DEFINE_MUTEX(lock);
172 static struct workqueue_struct *cma_wq;
173 static unsigned int cma_pernet_id;
176 struct xarray tcp_ps;
177 struct xarray udp_ps;
178 struct xarray ipoib_ps;
182 static struct cma_pernet *cma_pernet(struct net *net)
184 return net_generic(net, cma_pernet_id);
188 struct xarray *cma_pernet_xa(struct net *net, enum rdma_ucm_port_space ps)
190 struct cma_pernet *pernet = cma_pernet(net);
194 return &pernet->tcp_ps;
196 return &pernet->udp_ps;
198 return &pernet->ipoib_ps;
200 return &pernet->ib_ps;
207 struct list_head list;
208 struct ib_device *device;
209 struct completion comp;
211 struct list_head id_list;
212 enum ib_gid_type *default_gid_type;
213 u8 *default_roce_tos;
216 struct rdma_bind_list {
217 enum rdma_ucm_port_space ps;
218 struct hlist_head owners;
222 struct class_port_info_context {
223 struct ib_class_port_info *class_port_info;
224 struct ib_device *device;
225 struct completion done;
226 struct ib_sa_query *sa_query;
230 static int cma_ps_alloc(struct net *net, enum rdma_ucm_port_space ps,
231 struct rdma_bind_list *bind_list, int snum)
233 struct xarray *xa = cma_pernet_xa(net, ps);
235 return xa_insert(xa, snum, bind_list, GFP_KERNEL);
238 static struct rdma_bind_list *cma_ps_find(struct net *net,
239 enum rdma_ucm_port_space ps, int snum)
241 struct xarray *xa = cma_pernet_xa(net, ps);
243 return xa_load(xa, snum);
246 static void cma_ps_remove(struct net *net, enum rdma_ucm_port_space ps,
249 struct xarray *xa = cma_pernet_xa(net, ps);
258 void cma_dev_get(struct cma_device *cma_dev)
260 refcount_inc(&cma_dev->refcount);
263 void cma_dev_put(struct cma_device *cma_dev)
265 if (refcount_dec_and_test(&cma_dev->refcount))
266 complete(&cma_dev->comp);
269 struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter filter,
272 struct cma_device *cma_dev;
273 struct cma_device *found_cma_dev = NULL;
277 list_for_each_entry(cma_dev, &dev_list, list)
278 if (filter(cma_dev->device, cookie)) {
279 found_cma_dev = cma_dev;
284 cma_dev_get(found_cma_dev);
286 return found_cma_dev;
289 int cma_get_default_gid_type(struct cma_device *cma_dev,
292 if (!rdma_is_port_valid(cma_dev->device, port))
295 return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
298 int cma_set_default_gid_type(struct cma_device *cma_dev,
300 enum ib_gid_type default_gid_type)
302 unsigned long supported_gids;
304 if (!rdma_is_port_valid(cma_dev->device, port))
307 if (default_gid_type == IB_GID_TYPE_IB &&
308 rdma_protocol_roce_eth_encap(cma_dev->device, port))
309 default_gid_type = IB_GID_TYPE_ROCE;
311 supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
313 if (!(supported_gids & 1 << default_gid_type))
316 cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
322 int cma_get_default_roce_tos(struct cma_device *cma_dev, unsigned int port)
324 if (!rdma_is_port_valid(cma_dev->device, port))
327 return cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)];
330 int cma_set_default_roce_tos(struct cma_device *cma_dev, unsigned int port,
333 if (!rdma_is_port_valid(cma_dev->device, port))
336 cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)] =
341 struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
343 return cma_dev->device;
347 * Device removal can occur at anytime, so we need extra handling to
348 * serialize notifying the user of device removal with other callbacks.
349 * We do this by disabling removal notification while a callback is in process,
350 * and reporting it after the callback completes.
353 struct cma_multicast {
354 struct rdma_id_private *id_priv;
355 struct ib_sa_multicast *sa_mc;
356 struct list_head list;
358 struct sockaddr_storage addr;
363 struct work_struct work;
364 struct rdma_id_private *id;
365 enum rdma_cm_state old_state;
366 enum rdma_cm_state new_state;
367 struct rdma_cm_event event;
380 u8 ip_version; /* IP version: 7:4 */
382 union cma_ip_addr src_addr;
383 union cma_ip_addr dst_addr;
386 #define CMA_VERSION 0x00
388 struct cma_req_info {
389 struct sockaddr_storage listen_addr_storage;
390 struct sockaddr_storage src_addr_storage;
391 struct ib_device *device;
392 union ib_gid local_gid;
399 static int cma_comp_exch(struct rdma_id_private *id_priv,
400 enum rdma_cm_state comp, enum rdma_cm_state exch)
406 * The FSM uses a funny double locking where state is protected by both
407 * the handler_mutex and the spinlock. State is not allowed to change
408 * away from a handler_mutex protected value without also holding
411 if (comp == RDMA_CM_CONNECT)
412 lockdep_assert_held(&id_priv->handler_mutex);
414 spin_lock_irqsave(&id_priv->lock, flags);
415 if ((ret = (id_priv->state == comp)))
416 id_priv->state = exch;
417 spin_unlock_irqrestore(&id_priv->lock, flags);
421 static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
423 return hdr->ip_version >> 4;
426 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
428 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
431 static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
433 struct in_device *in_dev = NULL;
437 in_dev = __in_dev_get_rtnl(ndev);
440 ip_mc_inc_group(in_dev,
441 *(__be32 *)(mgid->raw + 12));
443 ip_mc_dec_group(in_dev,
444 *(__be32 *)(mgid->raw + 12));
448 return (in_dev) ? 0 : -ENODEV;
451 static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
452 struct cma_device *cma_dev)
454 cma_dev_get(cma_dev);
455 id_priv->cma_dev = cma_dev;
456 id_priv->id.device = cma_dev->device;
457 id_priv->id.route.addr.dev_addr.transport =
458 rdma_node_get_transport(cma_dev->device->node_type);
459 list_add_tail(&id_priv->list, &cma_dev->id_list);
460 rdma_restrack_add(&id_priv->res);
462 trace_cm_id_attach(id_priv, cma_dev->device);
465 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
466 struct cma_device *cma_dev)
468 _cma_attach_to_dev(id_priv, cma_dev);
470 cma_dev->default_gid_type[id_priv->id.port_num -
471 rdma_start_port(cma_dev->device)];
474 static void cma_release_dev(struct rdma_id_private *id_priv)
477 list_del(&id_priv->list);
478 cma_dev_put(id_priv->cma_dev);
479 id_priv->cma_dev = NULL;
483 static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
485 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
488 static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
490 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
493 static inline unsigned short cma_family(struct rdma_id_private *id_priv)
495 return id_priv->id.route.addr.src_addr.ss_family;
498 static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
500 struct ib_sa_mcmember_rec rec;
504 if (qkey && id_priv->qkey != qkey)
510 id_priv->qkey = qkey;
514 switch (id_priv->id.ps) {
517 id_priv->qkey = RDMA_UDP_QKEY;
520 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
521 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
522 id_priv->id.port_num, &rec.mgid,
525 id_priv->qkey = be32_to_cpu(rec.qkey);
533 static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
535 dev_addr->dev_type = ARPHRD_INFINIBAND;
536 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
537 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
540 static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
544 if (addr->sa_family != AF_IB) {
545 ret = rdma_translate_ip(addr, dev_addr);
547 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
554 static const struct ib_gid_attr *
555 cma_validate_port(struct ib_device *device, u8 port,
556 enum ib_gid_type gid_type,
558 struct rdma_id_private *id_priv)
560 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
561 int bound_if_index = dev_addr->bound_dev_if;
562 const struct ib_gid_attr *sgid_attr;
563 int dev_type = dev_addr->dev_type;
564 struct net_device *ndev = NULL;
566 if (!rdma_dev_access_netns(device, id_priv->id.route.addr.dev_addr.net))
567 return ERR_PTR(-ENODEV);
569 if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
570 return ERR_PTR(-ENODEV);
572 if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
573 return ERR_PTR(-ENODEV);
575 if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
576 ndev = dev_get_by_index(dev_addr->net, bound_if_index);
578 return ERR_PTR(-ENODEV);
580 gid_type = IB_GID_TYPE_IB;
583 sgid_attr = rdma_find_gid_by_port(device, gid, gid_type, port, ndev);
589 static void cma_bind_sgid_attr(struct rdma_id_private *id_priv,
590 const struct ib_gid_attr *sgid_attr)
592 WARN_ON(id_priv->id.route.addr.dev_addr.sgid_attr);
593 id_priv->id.route.addr.dev_addr.sgid_attr = sgid_attr;
597 * cma_acquire_dev_by_src_ip - Acquire cma device, port, gid attribute
598 * based on source ip address.
599 * @id_priv: cm_id which should be bound to cma device
601 * cma_acquire_dev_by_src_ip() binds cm id to cma device, port and GID attribute
602 * based on source IP address. It returns 0 on success or error code otherwise.
603 * It is applicable to active and passive side cm_id.
605 static int cma_acquire_dev_by_src_ip(struct rdma_id_private *id_priv)
607 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
608 const struct ib_gid_attr *sgid_attr;
609 union ib_gid gid, iboe_gid, *gidp;
610 struct cma_device *cma_dev;
611 enum ib_gid_type gid_type;
615 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
616 id_priv->id.ps == RDMA_PS_IPOIB)
619 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
622 memcpy(&gid, dev_addr->src_dev_addr +
623 rdma_addr_gid_offset(dev_addr), sizeof(gid));
626 list_for_each_entry(cma_dev, &dev_list, list) {
627 rdma_for_each_port (cma_dev->device, port) {
628 gidp = rdma_protocol_roce(cma_dev->device, port) ?
630 gid_type = cma_dev->default_gid_type[port - 1];
631 sgid_attr = cma_validate_port(cma_dev->device, port,
632 gid_type, gidp, id_priv);
633 if (!IS_ERR(sgid_attr)) {
634 id_priv->id.port_num = port;
635 cma_bind_sgid_attr(id_priv, sgid_attr);
636 cma_attach_to_dev(id_priv, cma_dev);
648 * cma_ib_acquire_dev - Acquire cma device, port and SGID attribute
649 * @id_priv: cm id to bind to cma device
650 * @listen_id_priv: listener cm id to match against
651 * @req: Pointer to req structure containaining incoming
652 * request information
653 * cma_ib_acquire_dev() acquires cma device, port and SGID attribute when
654 * rdma device matches for listen_id and incoming request. It also verifies
655 * that a GID table entry is present for the source address.
656 * Returns 0 on success, or returns error code otherwise.
658 static int cma_ib_acquire_dev(struct rdma_id_private *id_priv,
659 const struct rdma_id_private *listen_id_priv,
660 struct cma_req_info *req)
662 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
663 const struct ib_gid_attr *sgid_attr;
664 enum ib_gid_type gid_type;
667 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
668 id_priv->id.ps == RDMA_PS_IPOIB)
671 if (rdma_protocol_roce(req->device, req->port))
672 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
675 memcpy(&gid, dev_addr->src_dev_addr +
676 rdma_addr_gid_offset(dev_addr), sizeof(gid));
678 gid_type = listen_id_priv->cma_dev->default_gid_type[req->port - 1];
679 sgid_attr = cma_validate_port(req->device, req->port,
680 gid_type, &gid, id_priv);
681 if (IS_ERR(sgid_attr))
682 return PTR_ERR(sgid_attr);
684 id_priv->id.port_num = req->port;
685 cma_bind_sgid_attr(id_priv, sgid_attr);
686 /* Need to acquire lock to protect against reader
687 * of cma_dev->id_list such as cma_netdev_callback() and
688 * cma_process_remove().
691 cma_attach_to_dev(id_priv, listen_id_priv->cma_dev);
696 static int cma_iw_acquire_dev(struct rdma_id_private *id_priv,
697 const struct rdma_id_private *listen_id_priv)
699 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
700 const struct ib_gid_attr *sgid_attr;
701 struct cma_device *cma_dev;
702 enum ib_gid_type gid_type;
707 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
708 id_priv->id.ps == RDMA_PS_IPOIB)
711 memcpy(&gid, dev_addr->src_dev_addr +
712 rdma_addr_gid_offset(dev_addr), sizeof(gid));
716 cma_dev = listen_id_priv->cma_dev;
717 port = listen_id_priv->id.port_num;
718 gid_type = listen_id_priv->gid_type;
719 sgid_attr = cma_validate_port(cma_dev->device, port,
720 gid_type, &gid, id_priv);
721 if (!IS_ERR(sgid_attr)) {
722 id_priv->id.port_num = port;
723 cma_bind_sgid_attr(id_priv, sgid_attr);
728 list_for_each_entry(cma_dev, &dev_list, list) {
729 rdma_for_each_port (cma_dev->device, port) {
730 if (listen_id_priv->cma_dev == cma_dev &&
731 listen_id_priv->id.port_num == port)
734 gid_type = cma_dev->default_gid_type[port - 1];
735 sgid_attr = cma_validate_port(cma_dev->device, port,
736 gid_type, &gid, id_priv);
737 if (!IS_ERR(sgid_attr)) {
738 id_priv->id.port_num = port;
739 cma_bind_sgid_attr(id_priv, sgid_attr);
748 cma_attach_to_dev(id_priv, cma_dev);
755 * Select the source IB device and address to reach the destination IB address.
757 static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
759 struct cma_device *cma_dev, *cur_dev;
760 struct sockaddr_ib *addr;
761 union ib_gid gid, sgid, *dgid;
764 enum ib_port_state port_state;
768 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
769 dgid = (union ib_gid *) &addr->sib_addr;
770 pkey = ntohs(addr->sib_pkey);
773 list_for_each_entry(cur_dev, &dev_list, list) {
774 rdma_for_each_port (cur_dev->device, p) {
775 if (!rdma_cap_af_ib(cur_dev->device, p))
778 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
781 if (ib_get_cached_port_state(cur_dev->device, p, &port_state))
783 for (i = 0; !rdma_query_gid(cur_dev->device,
786 if (!memcmp(&gid, dgid, sizeof(gid))) {
789 id_priv->id.port_num = p;
793 if (!cma_dev && (gid.global.subnet_prefix ==
794 dgid->global.subnet_prefix) &&
795 port_state == IB_PORT_ACTIVE) {
798 id_priv->id.port_num = p;
808 cma_attach_to_dev(id_priv, cma_dev);
810 addr = (struct sockaddr_ib *)cma_src_addr(id_priv);
811 memcpy(&addr->sib_addr, &sgid, sizeof(sgid));
812 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
816 static void cma_id_get(struct rdma_id_private *id_priv)
818 refcount_inc(&id_priv->refcount);
821 static void cma_id_put(struct rdma_id_private *id_priv)
823 if (refcount_dec_and_test(&id_priv->refcount))
824 complete(&id_priv->comp);
827 static struct rdma_id_private *
828 __rdma_create_id(struct net *net, rdma_cm_event_handler event_handler,
829 void *context, enum rdma_ucm_port_space ps,
830 enum ib_qp_type qp_type, const struct rdma_id_private *parent)
832 struct rdma_id_private *id_priv;
834 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
836 return ERR_PTR(-ENOMEM);
838 id_priv->state = RDMA_CM_IDLE;
839 id_priv->id.context = context;
840 id_priv->id.event_handler = event_handler;
842 id_priv->id.qp_type = qp_type;
843 id_priv->tos_set = false;
844 id_priv->timeout_set = false;
845 id_priv->gid_type = IB_GID_TYPE_IB;
846 spin_lock_init(&id_priv->lock);
847 mutex_init(&id_priv->qp_mutex);
848 init_completion(&id_priv->comp);
849 refcount_set(&id_priv->refcount, 1);
850 mutex_init(&id_priv->handler_mutex);
851 INIT_LIST_HEAD(&id_priv->listen_list);
852 INIT_LIST_HEAD(&id_priv->mc_list);
853 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
854 id_priv->id.route.addr.dev_addr.net = get_net(net);
855 id_priv->seq_num &= 0x00ffffff;
857 rdma_restrack_new(&id_priv->res, RDMA_RESTRACK_CM_ID);
859 rdma_restrack_parent_name(&id_priv->res, &parent->res);
865 __rdma_create_kernel_id(struct net *net, rdma_cm_event_handler event_handler,
866 void *context, enum rdma_ucm_port_space ps,
867 enum ib_qp_type qp_type, const char *caller)
869 struct rdma_id_private *ret;
871 ret = __rdma_create_id(net, event_handler, context, ps, qp_type, NULL);
873 return ERR_CAST(ret);
875 rdma_restrack_set_name(&ret->res, caller);
878 EXPORT_SYMBOL(__rdma_create_kernel_id);
880 struct rdma_cm_id *rdma_create_user_id(rdma_cm_event_handler event_handler,
882 enum rdma_ucm_port_space ps,
883 enum ib_qp_type qp_type)
885 struct rdma_id_private *ret;
887 ret = __rdma_create_id(current->nsproxy->net_ns, event_handler, context,
890 return ERR_CAST(ret);
892 rdma_restrack_set_name(&ret->res, NULL);
895 EXPORT_SYMBOL(rdma_create_user_id);
897 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
899 struct ib_qp_attr qp_attr;
900 int qp_attr_mask, ret;
902 qp_attr.qp_state = IB_QPS_INIT;
903 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
907 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
911 qp_attr.qp_state = IB_QPS_RTR;
912 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
916 qp_attr.qp_state = IB_QPS_RTS;
918 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
923 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
925 struct ib_qp_attr qp_attr;
926 int qp_attr_mask, ret;
928 qp_attr.qp_state = IB_QPS_INIT;
929 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
933 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
936 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
937 struct ib_qp_init_attr *qp_init_attr)
939 struct rdma_id_private *id_priv;
943 id_priv = container_of(id, struct rdma_id_private, id);
944 if (id->device != pd->device) {
949 qp_init_attr->port_num = id->port_num;
950 qp = ib_create_qp(pd, qp_init_attr);
956 if (id->qp_type == IB_QPT_UD)
957 ret = cma_init_ud_qp(id_priv, qp);
959 ret = cma_init_conn_qp(id_priv, qp);
964 id_priv->qp_num = qp->qp_num;
965 id_priv->srq = (qp->srq != NULL);
966 trace_cm_qp_create(id_priv, pd, qp_init_attr, 0);
971 trace_cm_qp_create(id_priv, pd, qp_init_attr, ret);
974 EXPORT_SYMBOL(rdma_create_qp);
976 void rdma_destroy_qp(struct rdma_cm_id *id)
978 struct rdma_id_private *id_priv;
980 id_priv = container_of(id, struct rdma_id_private, id);
981 trace_cm_qp_destroy(id_priv);
982 mutex_lock(&id_priv->qp_mutex);
983 ib_destroy_qp(id_priv->id.qp);
984 id_priv->id.qp = NULL;
985 mutex_unlock(&id_priv->qp_mutex);
987 EXPORT_SYMBOL(rdma_destroy_qp);
989 static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
990 struct rdma_conn_param *conn_param)
992 struct ib_qp_attr qp_attr;
993 int qp_attr_mask, ret;
995 mutex_lock(&id_priv->qp_mutex);
996 if (!id_priv->id.qp) {
1001 /* Need to update QP attributes from default values. */
1002 qp_attr.qp_state = IB_QPS_INIT;
1003 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1007 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1011 qp_attr.qp_state = IB_QPS_RTR;
1012 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1016 BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
1019 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
1020 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1022 mutex_unlock(&id_priv->qp_mutex);
1026 static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
1027 struct rdma_conn_param *conn_param)
1029 struct ib_qp_attr qp_attr;
1030 int qp_attr_mask, ret;
1032 mutex_lock(&id_priv->qp_mutex);
1033 if (!id_priv->id.qp) {
1038 qp_attr.qp_state = IB_QPS_RTS;
1039 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1044 qp_attr.max_rd_atomic = conn_param->initiator_depth;
1045 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1047 mutex_unlock(&id_priv->qp_mutex);
1051 static int cma_modify_qp_err(struct rdma_id_private *id_priv)
1053 struct ib_qp_attr qp_attr;
1056 mutex_lock(&id_priv->qp_mutex);
1057 if (!id_priv->id.qp) {
1062 qp_attr.qp_state = IB_QPS_ERR;
1063 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
1065 mutex_unlock(&id_priv->qp_mutex);
1069 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
1070 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
1072 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
1076 if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
1079 pkey = ib_addr_get_pkey(dev_addr);
1081 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
1082 pkey, &qp_attr->pkey_index);
1086 qp_attr->port_num = id_priv->id.port_num;
1087 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
1089 if (id_priv->id.qp_type == IB_QPT_UD) {
1090 ret = cma_set_qkey(id_priv, 0);
1094 qp_attr->qkey = id_priv->qkey;
1095 *qp_attr_mask |= IB_QP_QKEY;
1097 qp_attr->qp_access_flags = 0;
1098 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
1103 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
1106 struct rdma_id_private *id_priv;
1109 id_priv = container_of(id, struct rdma_id_private, id);
1110 if (rdma_cap_ib_cm(id->device, id->port_num)) {
1111 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
1112 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
1114 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
1117 if (qp_attr->qp_state == IB_QPS_RTR)
1118 qp_attr->rq_psn = id_priv->seq_num;
1119 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
1120 if (!id_priv->cm_id.iw) {
1121 qp_attr->qp_access_flags = 0;
1122 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1124 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
1126 qp_attr->port_num = id_priv->id.port_num;
1127 *qp_attr_mask |= IB_QP_PORT;
1131 if ((*qp_attr_mask & IB_QP_TIMEOUT) && id_priv->timeout_set)
1132 qp_attr->timeout = id_priv->timeout;
1136 EXPORT_SYMBOL(rdma_init_qp_attr);
1138 static inline bool cma_zero_addr(const struct sockaddr *addr)
1140 switch (addr->sa_family) {
1142 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
1144 return ipv6_addr_any(&((struct sockaddr_in6 *)addr)->sin6_addr);
1146 return ib_addr_any(&((struct sockaddr_ib *)addr)->sib_addr);
1152 static inline bool cma_loopback_addr(const struct sockaddr *addr)
1154 switch (addr->sa_family) {
1156 return ipv4_is_loopback(
1157 ((struct sockaddr_in *)addr)->sin_addr.s_addr);
1159 return ipv6_addr_loopback(
1160 &((struct sockaddr_in6 *)addr)->sin6_addr);
1162 return ib_addr_loopback(
1163 &((struct sockaddr_ib *)addr)->sib_addr);
1169 static inline bool cma_any_addr(const struct sockaddr *addr)
1171 return cma_zero_addr(addr) || cma_loopback_addr(addr);
1174 static int cma_addr_cmp(const struct sockaddr *src, const struct sockaddr *dst)
1176 if (src->sa_family != dst->sa_family)
1179 switch (src->sa_family) {
1181 return ((struct sockaddr_in *)src)->sin_addr.s_addr !=
1182 ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1184 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *)src;
1185 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *)dst;
1188 if (ipv6_addr_cmp(&src_addr6->sin6_addr,
1189 &dst_addr6->sin6_addr))
1191 link_local = ipv6_addr_type(&dst_addr6->sin6_addr) &
1192 IPV6_ADDR_LINKLOCAL;
1193 /* Link local must match their scope_ids */
1194 return link_local ? (src_addr6->sin6_scope_id !=
1195 dst_addr6->sin6_scope_id) :
1200 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
1201 &((struct sockaddr_ib *) dst)->sib_addr);
1205 static __be16 cma_port(const struct sockaddr *addr)
1207 struct sockaddr_ib *sib;
1209 switch (addr->sa_family) {
1211 return ((struct sockaddr_in *) addr)->sin_port;
1213 return ((struct sockaddr_in6 *) addr)->sin6_port;
1215 sib = (struct sockaddr_ib *) addr;
1216 return htons((u16) (be64_to_cpu(sib->sib_sid) &
1217 be64_to_cpu(sib->sib_sid_mask)));
1223 static inline int cma_any_port(const struct sockaddr *addr)
1225 return !cma_port(addr);
1228 static void cma_save_ib_info(struct sockaddr *src_addr,
1229 struct sockaddr *dst_addr,
1230 const struct rdma_cm_id *listen_id,
1231 const struct sa_path_rec *path)
1233 struct sockaddr_ib *listen_ib, *ib;
1235 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
1237 ib = (struct sockaddr_ib *)src_addr;
1238 ib->sib_family = AF_IB;
1240 ib->sib_pkey = path->pkey;
1241 ib->sib_flowinfo = path->flow_label;
1242 memcpy(&ib->sib_addr, &path->sgid, 16);
1243 ib->sib_sid = path->service_id;
1244 ib->sib_scope_id = 0;
1246 ib->sib_pkey = listen_ib->sib_pkey;
1247 ib->sib_flowinfo = listen_ib->sib_flowinfo;
1248 ib->sib_addr = listen_ib->sib_addr;
1249 ib->sib_sid = listen_ib->sib_sid;
1250 ib->sib_scope_id = listen_ib->sib_scope_id;
1252 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
1255 ib = (struct sockaddr_ib *)dst_addr;
1256 ib->sib_family = AF_IB;
1258 ib->sib_pkey = path->pkey;
1259 ib->sib_flowinfo = path->flow_label;
1260 memcpy(&ib->sib_addr, &path->dgid, 16);
1265 static void cma_save_ip4_info(struct sockaddr_in *src_addr,
1266 struct sockaddr_in *dst_addr,
1267 struct cma_hdr *hdr,
1271 *src_addr = (struct sockaddr_in) {
1272 .sin_family = AF_INET,
1273 .sin_addr.s_addr = hdr->dst_addr.ip4.addr,
1274 .sin_port = local_port,
1279 *dst_addr = (struct sockaddr_in) {
1280 .sin_family = AF_INET,
1281 .sin_addr.s_addr = hdr->src_addr.ip4.addr,
1282 .sin_port = hdr->port,
1287 static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
1288 struct sockaddr_in6 *dst_addr,
1289 struct cma_hdr *hdr,
1293 *src_addr = (struct sockaddr_in6) {
1294 .sin6_family = AF_INET6,
1295 .sin6_addr = hdr->dst_addr.ip6,
1296 .sin6_port = local_port,
1301 *dst_addr = (struct sockaddr_in6) {
1302 .sin6_family = AF_INET6,
1303 .sin6_addr = hdr->src_addr.ip6,
1304 .sin6_port = hdr->port,
1309 static u16 cma_port_from_service_id(__be64 service_id)
1311 return (u16)be64_to_cpu(service_id);
1314 static int cma_save_ip_info(struct sockaddr *src_addr,
1315 struct sockaddr *dst_addr,
1316 const struct ib_cm_event *ib_event,
1319 struct cma_hdr *hdr;
1322 hdr = ib_event->private_data;
1323 if (hdr->cma_version != CMA_VERSION)
1326 port = htons(cma_port_from_service_id(service_id));
1328 switch (cma_get_ip_ver(hdr)) {
1330 cma_save_ip4_info((struct sockaddr_in *)src_addr,
1331 (struct sockaddr_in *)dst_addr, hdr, port);
1334 cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1335 (struct sockaddr_in6 *)dst_addr, hdr, port);
1338 return -EAFNOSUPPORT;
1344 static int cma_save_net_info(struct sockaddr *src_addr,
1345 struct sockaddr *dst_addr,
1346 const struct rdma_cm_id *listen_id,
1347 const struct ib_cm_event *ib_event,
1348 sa_family_t sa_family, __be64 service_id)
1350 if (sa_family == AF_IB) {
1351 if (ib_event->event == IB_CM_REQ_RECEIVED)
1352 cma_save_ib_info(src_addr, dst_addr, listen_id,
1353 ib_event->param.req_rcvd.primary_path);
1354 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1355 cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1359 return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1362 static int cma_save_req_info(const struct ib_cm_event *ib_event,
1363 struct cma_req_info *req)
1365 const struct ib_cm_req_event_param *req_param =
1366 &ib_event->param.req_rcvd;
1367 const struct ib_cm_sidr_req_event_param *sidr_param =
1368 &ib_event->param.sidr_req_rcvd;
1370 switch (ib_event->event) {
1371 case IB_CM_REQ_RECEIVED:
1372 req->device = req_param->listen_id->device;
1373 req->port = req_param->port;
1374 memcpy(&req->local_gid, &req_param->primary_path->sgid,
1375 sizeof(req->local_gid));
1376 req->has_gid = true;
1377 req->service_id = req_param->primary_path->service_id;
1378 req->pkey = be16_to_cpu(req_param->primary_path->pkey);
1379 if (req->pkey != req_param->bth_pkey)
1380 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1381 "RDMA CMA: in the future this may cause the request to be dropped\n",
1382 req_param->bth_pkey, req->pkey);
1384 case IB_CM_SIDR_REQ_RECEIVED:
1385 req->device = sidr_param->listen_id->device;
1386 req->port = sidr_param->port;
1387 req->has_gid = false;
1388 req->service_id = sidr_param->service_id;
1389 req->pkey = sidr_param->pkey;
1390 if (req->pkey != sidr_param->bth_pkey)
1391 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1392 "RDMA CMA: in the future this may cause the request to be dropped\n",
1393 sidr_param->bth_pkey, req->pkey);
1402 static bool validate_ipv4_net_dev(struct net_device *net_dev,
1403 const struct sockaddr_in *dst_addr,
1404 const struct sockaddr_in *src_addr)
1406 __be32 daddr = dst_addr->sin_addr.s_addr,
1407 saddr = src_addr->sin_addr.s_addr;
1408 struct fib_result res;
1413 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1414 ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1415 ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1416 ipv4_is_loopback(saddr))
1419 memset(&fl4, 0, sizeof(fl4));
1420 fl4.flowi4_iif = net_dev->ifindex;
1425 err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
1426 ret = err == 0 && FIB_RES_DEV(res) == net_dev;
1432 static bool validate_ipv6_net_dev(struct net_device *net_dev,
1433 const struct sockaddr_in6 *dst_addr,
1434 const struct sockaddr_in6 *src_addr)
1436 #if IS_ENABLED(CONFIG_IPV6)
1437 const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1438 IPV6_ADDR_LINKLOCAL;
1439 struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1440 &src_addr->sin6_addr, net_dev->ifindex,
1447 ret = rt->rt6i_idev->dev == net_dev;
1456 static bool validate_net_dev(struct net_device *net_dev,
1457 const struct sockaddr *daddr,
1458 const struct sockaddr *saddr)
1460 const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1461 const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1462 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1463 const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1465 switch (daddr->sa_family) {
1467 return saddr->sa_family == AF_INET &&
1468 validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1471 return saddr->sa_family == AF_INET6 &&
1472 validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1479 static struct net_device *
1480 roce_get_net_dev_by_cm_event(const struct ib_cm_event *ib_event)
1482 const struct ib_gid_attr *sgid_attr = NULL;
1483 struct net_device *ndev;
1485 if (ib_event->event == IB_CM_REQ_RECEIVED)
1486 sgid_attr = ib_event->param.req_rcvd.ppath_sgid_attr;
1487 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1488 sgid_attr = ib_event->param.sidr_req_rcvd.sgid_attr;
1494 ndev = rdma_read_gid_attr_ndev_rcu(sgid_attr);
1503 static struct net_device *cma_get_net_dev(const struct ib_cm_event *ib_event,
1504 struct cma_req_info *req)
1506 struct sockaddr *listen_addr =
1507 (struct sockaddr *)&req->listen_addr_storage;
1508 struct sockaddr *src_addr = (struct sockaddr *)&req->src_addr_storage;
1509 struct net_device *net_dev;
1510 const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1513 err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1516 return ERR_PTR(err);
1518 if (rdma_protocol_roce(req->device, req->port))
1519 net_dev = roce_get_net_dev_by_cm_event(ib_event);
1521 net_dev = ib_get_net_dev_by_params(req->device, req->port,
1525 return ERR_PTR(-ENODEV);
1530 static enum rdma_ucm_port_space rdma_ps_from_service_id(__be64 service_id)
1532 return (be64_to_cpu(service_id) >> 16) & 0xffff;
1535 static bool cma_match_private_data(struct rdma_id_private *id_priv,
1536 const struct cma_hdr *hdr)
1538 struct sockaddr *addr = cma_src_addr(id_priv);
1540 struct in6_addr ip6_addr;
1542 if (cma_any_addr(addr) && !id_priv->afonly)
1545 switch (addr->sa_family) {
1547 ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1548 if (cma_get_ip_ver(hdr) != 4)
1550 if (!cma_any_addr(addr) &&
1551 hdr->dst_addr.ip4.addr != ip4_addr)
1555 ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1556 if (cma_get_ip_ver(hdr) != 6)
1558 if (!cma_any_addr(addr) &&
1559 memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1571 static bool cma_protocol_roce(const struct rdma_cm_id *id)
1573 struct ib_device *device = id->device;
1574 const int port_num = id->port_num ?: rdma_start_port(device);
1576 return rdma_protocol_roce(device, port_num);
1579 static bool cma_is_req_ipv6_ll(const struct cma_req_info *req)
1581 const struct sockaddr *daddr =
1582 (const struct sockaddr *)&req->listen_addr_storage;
1583 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1585 /* Returns true if the req is for IPv6 link local */
1586 return (daddr->sa_family == AF_INET6 &&
1587 (ipv6_addr_type(&daddr6->sin6_addr) & IPV6_ADDR_LINKLOCAL));
1590 static bool cma_match_net_dev(const struct rdma_cm_id *id,
1591 const struct net_device *net_dev,
1592 const struct cma_req_info *req)
1594 const struct rdma_addr *addr = &id->route.addr;
1597 /* This request is an AF_IB request */
1598 return (!id->port_num || id->port_num == req->port) &&
1599 (addr->src_addr.ss_family == AF_IB);
1602 * If the request is not for IPv6 link local, allow matching
1603 * request to any netdevice of the one or multiport rdma device.
1605 if (!cma_is_req_ipv6_ll(req))
1608 * Net namespaces must match, and if the listner is listening
1609 * on a specific netdevice than netdevice must match as well.
1611 if (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1612 (!!addr->dev_addr.bound_dev_if ==
1613 (addr->dev_addr.bound_dev_if == net_dev->ifindex)))
1619 static struct rdma_id_private *cma_find_listener(
1620 const struct rdma_bind_list *bind_list,
1621 const struct ib_cm_id *cm_id,
1622 const struct ib_cm_event *ib_event,
1623 const struct cma_req_info *req,
1624 const struct net_device *net_dev)
1626 struct rdma_id_private *id_priv, *id_priv_dev;
1628 lockdep_assert_held(&lock);
1631 return ERR_PTR(-EINVAL);
1633 hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1634 if (cma_match_private_data(id_priv, ib_event->private_data)) {
1635 if (id_priv->id.device == cm_id->device &&
1636 cma_match_net_dev(&id_priv->id, net_dev, req))
1638 list_for_each_entry(id_priv_dev,
1639 &id_priv->listen_list,
1641 if (id_priv_dev->id.device == cm_id->device &&
1642 cma_match_net_dev(&id_priv_dev->id,
1649 return ERR_PTR(-EINVAL);
1652 static struct rdma_id_private *
1653 cma_ib_id_from_event(struct ib_cm_id *cm_id,
1654 const struct ib_cm_event *ib_event,
1655 struct cma_req_info *req,
1656 struct net_device **net_dev)
1658 struct rdma_bind_list *bind_list;
1659 struct rdma_id_private *id_priv;
1662 err = cma_save_req_info(ib_event, req);
1664 return ERR_PTR(err);
1666 *net_dev = cma_get_net_dev(ib_event, req);
1667 if (IS_ERR(*net_dev)) {
1668 if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
1669 /* Assuming the protocol is AF_IB */
1672 return ERR_CAST(*net_dev);
1678 * Net namespace might be getting deleted while route lookup,
1679 * cm_id lookup is in progress. Therefore, perform netdevice
1680 * validation, cm_id lookup under rcu lock.
1681 * RCU lock along with netdevice state check, synchronizes with
1682 * netdevice migrating to different net namespace and also avoids
1683 * case where net namespace doesn't get deleted while lookup is in
1685 * If the device state is not IFF_UP, its properties such as ifindex
1686 * and nd_net cannot be trusted to remain valid without rcu lock.
1687 * net/core/dev.c change_net_namespace() ensures to synchronize with
1688 * ongoing operations on net device after device is closed using
1689 * synchronize_net().
1694 * If netdevice is down, it is likely that it is administratively
1695 * down or it might be migrating to different namespace.
1696 * In that case avoid further processing, as the net namespace
1697 * or ifindex may change.
1699 if (((*net_dev)->flags & IFF_UP) == 0) {
1700 id_priv = ERR_PTR(-EHOSTUNREACH);
1704 if (!validate_net_dev(*net_dev,
1705 (struct sockaddr *)&req->listen_addr_storage,
1706 (struct sockaddr *)&req->src_addr_storage)) {
1707 id_priv = ERR_PTR(-EHOSTUNREACH);
1712 bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
1713 rdma_ps_from_service_id(req->service_id),
1714 cma_port_from_service_id(req->service_id));
1715 id_priv = cma_find_listener(bind_list, cm_id, ib_event, req, *net_dev);
1718 mutex_unlock(&lock);
1719 if (IS_ERR(id_priv) && *net_dev) {
1726 static inline u8 cma_user_data_offset(struct rdma_id_private *id_priv)
1728 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
1731 static void cma_cancel_route(struct rdma_id_private *id_priv)
1733 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
1735 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
1739 static void cma_cancel_listens(struct rdma_id_private *id_priv)
1741 struct rdma_id_private *dev_id_priv;
1744 * Remove from listen_any_list to prevent added devices from spawning
1745 * additional listen requests.
1748 list_del(&id_priv->list);
1750 while (!list_empty(&id_priv->listen_list)) {
1751 dev_id_priv = list_entry(id_priv->listen_list.next,
1752 struct rdma_id_private, listen_list);
1753 /* sync with device removal to avoid duplicate destruction */
1754 list_del_init(&dev_id_priv->list);
1755 list_del(&dev_id_priv->listen_list);
1756 mutex_unlock(&lock);
1758 rdma_destroy_id(&dev_id_priv->id);
1761 mutex_unlock(&lock);
1764 static void cma_cancel_operation(struct rdma_id_private *id_priv,
1765 enum rdma_cm_state state)
1768 case RDMA_CM_ADDR_QUERY:
1769 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1771 case RDMA_CM_ROUTE_QUERY:
1772 cma_cancel_route(id_priv);
1774 case RDMA_CM_LISTEN:
1775 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
1776 cma_cancel_listens(id_priv);
1783 static void cma_release_port(struct rdma_id_private *id_priv)
1785 struct rdma_bind_list *bind_list = id_priv->bind_list;
1786 struct net *net = id_priv->id.route.addr.dev_addr.net;
1792 hlist_del(&id_priv->node);
1793 if (hlist_empty(&bind_list->owners)) {
1794 cma_ps_remove(net, bind_list->ps, bind_list->port);
1797 mutex_unlock(&lock);
1800 static void destroy_mc(struct rdma_id_private *id_priv,
1801 struct cma_multicast *mc)
1803 if (rdma_cap_ib_mcast(id_priv->id.device, id_priv->id.port_num))
1804 ib_sa_free_multicast(mc->sa_mc);
1806 if (rdma_protocol_roce(id_priv->id.device, id_priv->id.port_num)) {
1807 struct rdma_dev_addr *dev_addr =
1808 &id_priv->id.route.addr.dev_addr;
1809 struct net_device *ndev = NULL;
1811 if (dev_addr->bound_dev_if)
1812 ndev = dev_get_by_index(dev_addr->net,
1813 dev_addr->bound_dev_if);
1817 cma_set_mgid(id_priv, (struct sockaddr *)&mc->addr,
1819 cma_igmp_send(ndev, &mgid, false);
1826 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1828 struct cma_multicast *mc;
1830 while (!list_empty(&id_priv->mc_list)) {
1831 mc = list_first_entry(&id_priv->mc_list, struct cma_multicast,
1833 list_del(&mc->list);
1834 destroy_mc(id_priv, mc);
1838 static void _destroy_id(struct rdma_id_private *id_priv,
1839 enum rdma_cm_state state)
1841 cma_cancel_operation(id_priv, state);
1843 if (id_priv->cma_dev) {
1844 if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
1845 if (id_priv->cm_id.ib)
1846 ib_destroy_cm_id(id_priv->cm_id.ib);
1847 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
1848 if (id_priv->cm_id.iw)
1849 iw_destroy_cm_id(id_priv->cm_id.iw);
1851 cma_leave_mc_groups(id_priv);
1852 cma_release_dev(id_priv);
1855 cma_release_port(id_priv);
1856 cma_id_put(id_priv);
1857 wait_for_completion(&id_priv->comp);
1859 if (id_priv->internal_id)
1860 cma_id_put(id_priv->id.context);
1862 kfree(id_priv->id.route.path_rec);
1864 if (id_priv->id.route.addr.dev_addr.sgid_attr)
1865 rdma_put_gid_attr(id_priv->id.route.addr.dev_addr.sgid_attr);
1867 put_net(id_priv->id.route.addr.dev_addr.net);
1868 rdma_restrack_del(&id_priv->res);
1873 * destroy an ID from within the handler_mutex. This ensures that no other
1874 * handlers can start running concurrently.
1876 static void destroy_id_handler_unlock(struct rdma_id_private *id_priv)
1877 __releases(&idprv->handler_mutex)
1879 enum rdma_cm_state state;
1880 unsigned long flags;
1882 trace_cm_id_destroy(id_priv);
1885 * Setting the state to destroyed under the handler mutex provides a
1886 * fence against calling handler callbacks. If this is invoked due to
1887 * the failure of a handler callback then it guarentees that no future
1888 * handlers will be called.
1890 lockdep_assert_held(&id_priv->handler_mutex);
1891 spin_lock_irqsave(&id_priv->lock, flags);
1892 state = id_priv->state;
1893 id_priv->state = RDMA_CM_DESTROYING;
1894 spin_unlock_irqrestore(&id_priv->lock, flags);
1895 mutex_unlock(&id_priv->handler_mutex);
1896 _destroy_id(id_priv, state);
1899 void rdma_destroy_id(struct rdma_cm_id *id)
1901 struct rdma_id_private *id_priv =
1902 container_of(id, struct rdma_id_private, id);
1904 mutex_lock(&id_priv->handler_mutex);
1905 destroy_id_handler_unlock(id_priv);
1907 EXPORT_SYMBOL(rdma_destroy_id);
1909 static int cma_rep_recv(struct rdma_id_private *id_priv)
1913 ret = cma_modify_qp_rtr(id_priv, NULL);
1917 ret = cma_modify_qp_rts(id_priv, NULL);
1921 trace_cm_send_rtu(id_priv);
1922 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1928 pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret);
1929 cma_modify_qp_err(id_priv);
1930 trace_cm_send_rej(id_priv);
1931 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1936 static void cma_set_rep_event_data(struct rdma_cm_event *event,
1937 const struct ib_cm_rep_event_param *rep_data,
1940 event->param.conn.private_data = private_data;
1941 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1942 event->param.conn.responder_resources = rep_data->responder_resources;
1943 event->param.conn.initiator_depth = rep_data->initiator_depth;
1944 event->param.conn.flow_control = rep_data->flow_control;
1945 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1946 event->param.conn.srq = rep_data->srq;
1947 event->param.conn.qp_num = rep_data->remote_qpn;
1949 event->ece.vendor_id = rep_data->ece.vendor_id;
1950 event->ece.attr_mod = rep_data->ece.attr_mod;
1953 static int cma_cm_event_handler(struct rdma_id_private *id_priv,
1954 struct rdma_cm_event *event)
1958 lockdep_assert_held(&id_priv->handler_mutex);
1960 trace_cm_event_handler(id_priv, event);
1961 ret = id_priv->id.event_handler(&id_priv->id, event);
1962 trace_cm_event_done(id_priv, event, ret);
1966 static int cma_ib_handler(struct ib_cm_id *cm_id,
1967 const struct ib_cm_event *ib_event)
1969 struct rdma_id_private *id_priv = cm_id->context;
1970 struct rdma_cm_event event = {};
1971 enum rdma_cm_state state;
1974 mutex_lock(&id_priv->handler_mutex);
1975 state = READ_ONCE(id_priv->state);
1976 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
1977 state != RDMA_CM_CONNECT) ||
1978 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
1979 state != RDMA_CM_DISCONNECT))
1982 switch (ib_event->event) {
1983 case IB_CM_REQ_ERROR:
1984 case IB_CM_REP_ERROR:
1985 event.event = RDMA_CM_EVENT_UNREACHABLE;
1986 event.status = -ETIMEDOUT;
1988 case IB_CM_REP_RECEIVED:
1989 if (state == RDMA_CM_CONNECT &&
1990 (id_priv->id.qp_type != IB_QPT_UD)) {
1991 trace_cm_send_mra(id_priv);
1992 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1994 if (id_priv->id.qp) {
1995 event.status = cma_rep_recv(id_priv);
1996 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1997 RDMA_CM_EVENT_ESTABLISHED;
1999 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
2001 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
2002 ib_event->private_data);
2004 case IB_CM_RTU_RECEIVED:
2005 case IB_CM_USER_ESTABLISHED:
2006 event.event = RDMA_CM_EVENT_ESTABLISHED;
2008 case IB_CM_DREQ_ERROR:
2009 event.status = -ETIMEDOUT;
2011 case IB_CM_DREQ_RECEIVED:
2012 case IB_CM_DREP_RECEIVED:
2013 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
2014 RDMA_CM_DISCONNECT))
2016 event.event = RDMA_CM_EVENT_DISCONNECTED;
2018 case IB_CM_TIMEWAIT_EXIT:
2019 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
2021 case IB_CM_MRA_RECEIVED:
2024 case IB_CM_REJ_RECEIVED:
2025 pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv->id,
2026 ib_event->param.rej_rcvd.reason));
2027 cma_modify_qp_err(id_priv);
2028 event.status = ib_event->param.rej_rcvd.reason;
2029 event.event = RDMA_CM_EVENT_REJECTED;
2030 event.param.conn.private_data = ib_event->private_data;
2031 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
2034 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
2039 ret = cma_cm_event_handler(id_priv, &event);
2041 /* Destroy the CM ID by returning a non-zero value. */
2042 id_priv->cm_id.ib = NULL;
2043 destroy_id_handler_unlock(id_priv);
2047 mutex_unlock(&id_priv->handler_mutex);
2051 static struct rdma_id_private *
2052 cma_ib_new_conn_id(const struct rdma_cm_id *listen_id,
2053 const struct ib_cm_event *ib_event,
2054 struct net_device *net_dev)
2056 struct rdma_id_private *listen_id_priv;
2057 struct rdma_id_private *id_priv;
2058 struct rdma_cm_id *id;
2059 struct rdma_route *rt;
2060 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2061 struct sa_path_rec *path = ib_event->param.req_rcvd.primary_path;
2062 const __be64 service_id =
2063 ib_event->param.req_rcvd.primary_path->service_id;
2066 listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2067 id_priv = __rdma_create_id(listen_id->route.addr.dev_addr.net,
2068 listen_id->event_handler, listen_id->context,
2070 ib_event->param.req_rcvd.qp_type,
2072 if (IS_ERR(id_priv))
2076 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2077 (struct sockaddr *)&id->route.addr.dst_addr,
2078 listen_id, ib_event, ss_family, service_id))
2082 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
2083 rt->path_rec = kmalloc_array(rt->num_paths, sizeof(*rt->path_rec),
2088 rt->path_rec[0] = *path;
2089 if (rt->num_paths == 2)
2090 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
2093 rdma_copy_src_l2_addr(&rt->addr.dev_addr, net_dev);
2095 if (!cma_protocol_roce(listen_id) &&
2096 cma_any_addr(cma_src_addr(id_priv))) {
2097 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
2098 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
2099 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
2100 } else if (!cma_any_addr(cma_src_addr(id_priv))) {
2101 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
2106 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
2108 id_priv->state = RDMA_CM_CONNECT;
2112 rdma_destroy_id(id);
2116 static struct rdma_id_private *
2117 cma_ib_new_udp_id(const struct rdma_cm_id *listen_id,
2118 const struct ib_cm_event *ib_event,
2119 struct net_device *net_dev)
2121 const struct rdma_id_private *listen_id_priv;
2122 struct rdma_id_private *id_priv;
2123 struct rdma_cm_id *id;
2124 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2125 struct net *net = listen_id->route.addr.dev_addr.net;
2128 listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2129 id_priv = __rdma_create_id(net, listen_id->event_handler,
2130 listen_id->context, listen_id->ps, IB_QPT_UD,
2132 if (IS_ERR(id_priv))
2136 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2137 (struct sockaddr *)&id->route.addr.dst_addr,
2138 listen_id, ib_event, ss_family,
2139 ib_event->param.sidr_req_rcvd.service_id))
2143 rdma_copy_src_l2_addr(&id->route.addr.dev_addr, net_dev);
2145 if (!cma_any_addr(cma_src_addr(id_priv))) {
2146 ret = cma_translate_addr(cma_src_addr(id_priv),
2147 &id->route.addr.dev_addr);
2153 id_priv->state = RDMA_CM_CONNECT;
2156 rdma_destroy_id(id);
2160 static void cma_set_req_event_data(struct rdma_cm_event *event,
2161 const struct ib_cm_req_event_param *req_data,
2162 void *private_data, int offset)
2164 event->param.conn.private_data = private_data + offset;
2165 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
2166 event->param.conn.responder_resources = req_data->responder_resources;
2167 event->param.conn.initiator_depth = req_data->initiator_depth;
2168 event->param.conn.flow_control = req_data->flow_control;
2169 event->param.conn.retry_count = req_data->retry_count;
2170 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
2171 event->param.conn.srq = req_data->srq;
2172 event->param.conn.qp_num = req_data->remote_qpn;
2174 event->ece.vendor_id = req_data->ece.vendor_id;
2175 event->ece.attr_mod = req_data->ece.attr_mod;
2178 static int cma_ib_check_req_qp_type(const struct rdma_cm_id *id,
2179 const struct ib_cm_event *ib_event)
2181 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
2182 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
2183 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
2184 (id->qp_type == IB_QPT_UD)) ||
2188 static int cma_ib_req_handler(struct ib_cm_id *cm_id,
2189 const struct ib_cm_event *ib_event)
2191 struct rdma_id_private *listen_id, *conn_id = NULL;
2192 struct rdma_cm_event event = {};
2193 struct cma_req_info req = {};
2194 struct net_device *net_dev;
2198 listen_id = cma_ib_id_from_event(cm_id, ib_event, &req, &net_dev);
2199 if (IS_ERR(listen_id))
2200 return PTR_ERR(listen_id);
2202 trace_cm_req_handler(listen_id, ib_event->event);
2203 if (!cma_ib_check_req_qp_type(&listen_id->id, ib_event)) {
2208 mutex_lock(&listen_id->handler_mutex);
2209 if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN) {
2210 ret = -ECONNABORTED;
2214 offset = cma_user_data_offset(listen_id);
2215 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2216 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
2217 conn_id = cma_ib_new_udp_id(&listen_id->id, ib_event, net_dev);
2218 event.param.ud.private_data = ib_event->private_data + offset;
2219 event.param.ud.private_data_len =
2220 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
2222 conn_id = cma_ib_new_conn_id(&listen_id->id, ib_event, net_dev);
2223 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
2224 ib_event->private_data, offset);
2231 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2232 ret = cma_ib_acquire_dev(conn_id, listen_id, &req);
2234 destroy_id_handler_unlock(conn_id);
2238 conn_id->cm_id.ib = cm_id;
2239 cm_id->context = conn_id;
2240 cm_id->cm_handler = cma_ib_handler;
2242 ret = cma_cm_event_handler(conn_id, &event);
2244 /* Destroy the CM ID by returning a non-zero value. */
2245 conn_id->cm_id.ib = NULL;
2246 mutex_unlock(&listen_id->handler_mutex);
2247 destroy_id_handler_unlock(conn_id);
2251 if (READ_ONCE(conn_id->state) == RDMA_CM_CONNECT &&
2252 conn_id->id.qp_type != IB_QPT_UD) {
2253 trace_cm_send_mra(cm_id->context);
2254 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2256 mutex_unlock(&conn_id->handler_mutex);
2259 mutex_unlock(&listen_id->handler_mutex);
2268 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
2270 if (addr->sa_family == AF_IB)
2271 return ((struct sockaddr_ib *) addr)->sib_sid;
2273 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
2275 EXPORT_SYMBOL(rdma_get_service_id);
2277 void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid,
2280 struct rdma_addr *addr = &cm_id->route.addr;
2282 if (!cm_id->device) {
2284 memset(sgid, 0, sizeof(*sgid));
2286 memset(dgid, 0, sizeof(*dgid));
2290 if (rdma_protocol_roce(cm_id->device, cm_id->port_num)) {
2292 rdma_ip2gid((struct sockaddr *)&addr->src_addr, sgid);
2294 rdma_ip2gid((struct sockaddr *)&addr->dst_addr, dgid);
2297 rdma_addr_get_sgid(&addr->dev_addr, sgid);
2299 rdma_addr_get_dgid(&addr->dev_addr, dgid);
2302 EXPORT_SYMBOL(rdma_read_gids);
2304 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
2306 struct rdma_id_private *id_priv = iw_id->context;
2307 struct rdma_cm_event event = {};
2309 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2310 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2312 mutex_lock(&id_priv->handler_mutex);
2313 if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
2316 switch (iw_event->event) {
2317 case IW_CM_EVENT_CLOSE:
2318 event.event = RDMA_CM_EVENT_DISCONNECTED;
2320 case IW_CM_EVENT_CONNECT_REPLY:
2321 memcpy(cma_src_addr(id_priv), laddr,
2322 rdma_addr_size(laddr));
2323 memcpy(cma_dst_addr(id_priv), raddr,
2324 rdma_addr_size(raddr));
2325 switch (iw_event->status) {
2327 event.event = RDMA_CM_EVENT_ESTABLISHED;
2328 event.param.conn.initiator_depth = iw_event->ird;
2329 event.param.conn.responder_resources = iw_event->ord;
2333 event.event = RDMA_CM_EVENT_REJECTED;
2336 event.event = RDMA_CM_EVENT_UNREACHABLE;
2339 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2343 case IW_CM_EVENT_ESTABLISHED:
2344 event.event = RDMA_CM_EVENT_ESTABLISHED;
2345 event.param.conn.initiator_depth = iw_event->ird;
2346 event.param.conn.responder_resources = iw_event->ord;
2352 event.status = iw_event->status;
2353 event.param.conn.private_data = iw_event->private_data;
2354 event.param.conn.private_data_len = iw_event->private_data_len;
2355 ret = cma_cm_event_handler(id_priv, &event);
2357 /* Destroy the CM ID by returning a non-zero value. */
2358 id_priv->cm_id.iw = NULL;
2359 destroy_id_handler_unlock(id_priv);
2364 mutex_unlock(&id_priv->handler_mutex);
2368 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2369 struct iw_cm_event *iw_event)
2371 struct rdma_id_private *listen_id, *conn_id;
2372 struct rdma_cm_event event = {};
2373 int ret = -ECONNABORTED;
2374 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2375 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2377 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2378 event.param.conn.private_data = iw_event->private_data;
2379 event.param.conn.private_data_len = iw_event->private_data_len;
2380 event.param.conn.initiator_depth = iw_event->ird;
2381 event.param.conn.responder_resources = iw_event->ord;
2383 listen_id = cm_id->context;
2385 mutex_lock(&listen_id->handler_mutex);
2386 if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN)
2389 /* Create a new RDMA id for the new IW CM ID */
2390 conn_id = __rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2391 listen_id->id.event_handler,
2392 listen_id->id.context, RDMA_PS_TCP,
2393 IB_QPT_RC, listen_id);
2394 if (IS_ERR(conn_id)) {
2398 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2399 conn_id->state = RDMA_CM_CONNECT;
2401 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr);
2403 mutex_unlock(&listen_id->handler_mutex);
2404 destroy_id_handler_unlock(conn_id);
2408 ret = cma_iw_acquire_dev(conn_id, listen_id);
2410 mutex_unlock(&listen_id->handler_mutex);
2411 destroy_id_handler_unlock(conn_id);
2415 conn_id->cm_id.iw = cm_id;
2416 cm_id->context = conn_id;
2417 cm_id->cm_handler = cma_iw_handler;
2419 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2420 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
2422 ret = cma_cm_event_handler(conn_id, &event);
2424 /* User wants to destroy the CM ID */
2425 conn_id->cm_id.iw = NULL;
2426 mutex_unlock(&listen_id->handler_mutex);
2427 destroy_id_handler_unlock(conn_id);
2431 mutex_unlock(&conn_id->handler_mutex);
2434 mutex_unlock(&listen_id->handler_mutex);
2438 static int cma_ib_listen(struct rdma_id_private *id_priv)
2440 struct sockaddr *addr;
2441 struct ib_cm_id *id;
2444 addr = cma_src_addr(id_priv);
2445 svc_id = rdma_get_service_id(&id_priv->id, addr);
2446 id = ib_cm_insert_listen(id_priv->id.device,
2447 cma_ib_req_handler, svc_id);
2450 id_priv->cm_id.ib = id;
2455 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2458 struct iw_cm_id *id;
2460 id = iw_create_cm_id(id_priv->id.device,
2461 iw_conn_req_handler,
2466 id->tos = id_priv->tos;
2467 id->tos_set = id_priv->tos_set;
2468 id_priv->cm_id.iw = id;
2470 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2471 rdma_addr_size(cma_src_addr(id_priv)));
2473 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2476 iw_destroy_cm_id(id_priv->cm_id.iw);
2477 id_priv->cm_id.iw = NULL;
2483 static int cma_listen_handler(struct rdma_cm_id *id,
2484 struct rdma_cm_event *event)
2486 struct rdma_id_private *id_priv = id->context;
2488 /* Listening IDs are always destroyed on removal */
2489 if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
2492 id->context = id_priv->id.context;
2493 id->event_handler = id_priv->id.event_handler;
2494 trace_cm_event_handler(id_priv, event);
2495 return id_priv->id.event_handler(id, event);
2498 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2499 struct cma_device *cma_dev)
2501 struct rdma_id_private *dev_id_priv;
2502 struct net *net = id_priv->id.route.addr.dev_addr.net;
2505 lockdep_assert_held(&lock);
2507 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2511 __rdma_create_id(net, cma_listen_handler, id_priv,
2512 id_priv->id.ps, id_priv->id.qp_type, id_priv);
2513 if (IS_ERR(dev_id_priv))
2516 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2517 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2518 rdma_addr_size(cma_src_addr(id_priv)));
2520 _cma_attach_to_dev(dev_id_priv, cma_dev);
2521 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
2522 cma_id_get(id_priv);
2523 dev_id_priv->internal_id = 1;
2524 dev_id_priv->afonly = id_priv->afonly;
2525 dev_id_priv->tos_set = id_priv->tos_set;
2526 dev_id_priv->tos = id_priv->tos;
2528 ret = rdma_listen(&dev_id_priv->id, id_priv->backlog);
2530 dev_warn(&cma_dev->device->dev,
2531 "RDMA CMA: cma_listen_on_dev, error %d\n", ret);
2534 static void cma_listen_on_all(struct rdma_id_private *id_priv)
2536 struct cma_device *cma_dev;
2539 list_add_tail(&id_priv->list, &listen_any_list);
2540 list_for_each_entry(cma_dev, &dev_list, list)
2541 cma_listen_on_dev(id_priv, cma_dev);
2542 mutex_unlock(&lock);
2545 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2547 struct rdma_id_private *id_priv;
2549 id_priv = container_of(id, struct rdma_id_private, id);
2550 id_priv->tos = (u8) tos;
2551 id_priv->tos_set = true;
2553 EXPORT_SYMBOL(rdma_set_service_type);
2556 * rdma_set_ack_timeout() - Set the ack timeout of QP associated
2557 * with a connection identifier.
2558 * @id: Communication identifier to associated with service type.
2559 * @timeout: Ack timeout to set a QP, expressed as 4.096 * 2^(timeout) usec.
2561 * This function should be called before rdma_connect() on active side,
2562 * and on passive side before rdma_accept(). It is applicable to primary
2563 * path only. The timeout will affect the local side of the QP, it is not
2564 * negotiated with remote side and zero disables the timer. In case it is
2565 * set before rdma_resolve_route, the value will also be used to determine
2566 * PacketLifeTime for RoCE.
2568 * Return: 0 for success
2570 int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout)
2572 struct rdma_id_private *id_priv;
2574 if (id->qp_type != IB_QPT_RC)
2577 id_priv = container_of(id, struct rdma_id_private, id);
2578 id_priv->timeout = timeout;
2579 id_priv->timeout_set = true;
2583 EXPORT_SYMBOL(rdma_set_ack_timeout);
2585 static void cma_query_handler(int status, struct sa_path_rec *path_rec,
2588 struct cma_work *work = context;
2589 struct rdma_route *route;
2591 route = &work->id->id.route;
2594 route->num_paths = 1;
2595 *route->path_rec = *path_rec;
2597 work->old_state = RDMA_CM_ROUTE_QUERY;
2598 work->new_state = RDMA_CM_ADDR_RESOLVED;
2599 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2600 work->event.status = status;
2601 pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2605 queue_work(cma_wq, &work->work);
2608 static int cma_query_ib_route(struct rdma_id_private *id_priv,
2609 unsigned long timeout_ms, struct cma_work *work)
2611 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2612 struct sa_path_rec path_rec;
2613 ib_sa_comp_mask comp_mask;
2614 struct sockaddr_in6 *sin6;
2615 struct sockaddr_ib *sib;
2617 memset(&path_rec, 0, sizeof path_rec);
2619 if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num))
2620 path_rec.rec_type = SA_PATH_REC_TYPE_OPA;
2622 path_rec.rec_type = SA_PATH_REC_TYPE_IB;
2623 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2624 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2625 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2626 path_rec.numb_path = 1;
2627 path_rec.reversible = 1;
2628 path_rec.service_id = rdma_get_service_id(&id_priv->id,
2629 cma_dst_addr(id_priv));
2631 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2632 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2633 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2635 switch (cma_family(id_priv)) {
2637 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2638 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2641 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2642 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2643 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2646 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2647 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2648 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2652 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2653 id_priv->id.port_num, &path_rec,
2654 comp_mask, timeout_ms,
2655 GFP_KERNEL, cma_query_handler,
2656 work, &id_priv->query);
2658 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2661 static void cma_work_handler(struct work_struct *_work)
2663 struct cma_work *work = container_of(_work, struct cma_work, work);
2664 struct rdma_id_private *id_priv = work->id;
2666 mutex_lock(&id_priv->handler_mutex);
2667 if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2668 READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2670 if (work->old_state != 0 || work->new_state != 0) {
2671 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2675 if (cma_cm_event_handler(id_priv, &work->event)) {
2676 cma_id_put(id_priv);
2677 destroy_id_handler_unlock(id_priv);
2682 mutex_unlock(&id_priv->handler_mutex);
2683 cma_id_put(id_priv);
2685 if (work->event.event == RDMA_CM_EVENT_MULTICAST_JOIN)
2686 rdma_destroy_ah_attr(&work->event.param.ud.ah_attr);
2690 static void cma_init_resolve_route_work(struct cma_work *work,
2691 struct rdma_id_private *id_priv)
2694 INIT_WORK(&work->work, cma_work_handler);
2695 work->old_state = RDMA_CM_ROUTE_QUERY;
2696 work->new_state = RDMA_CM_ROUTE_RESOLVED;
2697 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2700 static void enqueue_resolve_addr_work(struct cma_work *work,
2701 struct rdma_id_private *id_priv)
2703 /* Balances with cma_id_put() in cma_work_handler */
2704 cma_id_get(id_priv);
2707 INIT_WORK(&work->work, cma_work_handler);
2708 work->old_state = RDMA_CM_ADDR_QUERY;
2709 work->new_state = RDMA_CM_ADDR_RESOLVED;
2710 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2712 queue_work(cma_wq, &work->work);
2715 static int cma_resolve_ib_route(struct rdma_id_private *id_priv,
2716 unsigned long timeout_ms)
2718 struct rdma_route *route = &id_priv->id.route;
2719 struct cma_work *work;
2722 work = kzalloc(sizeof *work, GFP_KERNEL);
2726 cma_init_resolve_route_work(work, id_priv);
2728 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2729 if (!route->path_rec) {
2734 ret = cma_query_ib_route(id_priv, timeout_ms, work);
2740 kfree(route->path_rec);
2741 route->path_rec = NULL;
2747 static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2748 unsigned long supported_gids,
2749 enum ib_gid_type default_gid)
2751 if ((network_type == RDMA_NETWORK_IPV4 ||
2752 network_type == RDMA_NETWORK_IPV6) &&
2753 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2754 return IB_GID_TYPE_ROCE_UDP_ENCAP;
2760 * cma_iboe_set_path_rec_l2_fields() is helper function which sets
2761 * path record type based on GID type.
2762 * It also sets up other L2 fields which includes destination mac address
2763 * netdev ifindex, of the path record.
2764 * It returns the netdev of the bound interface for this path record entry.
2766 static struct net_device *
2767 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private *id_priv)
2769 struct rdma_route *route = &id_priv->id.route;
2770 enum ib_gid_type gid_type = IB_GID_TYPE_ROCE;
2771 struct rdma_addr *addr = &route->addr;
2772 unsigned long supported_gids;
2773 struct net_device *ndev;
2775 if (!addr->dev_addr.bound_dev_if)
2778 ndev = dev_get_by_index(addr->dev_addr.net,
2779 addr->dev_addr.bound_dev_if);
2783 supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2784 id_priv->id.port_num);
2785 gid_type = cma_route_gid_type(addr->dev_addr.network,
2788 /* Use the hint from IP Stack to select GID Type */
2789 if (gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2790 gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2791 route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type);
2793 route->path_rec->roce.route_resolved = true;
2794 sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr);
2798 int rdma_set_ib_path(struct rdma_cm_id *id,
2799 struct sa_path_rec *path_rec)
2801 struct rdma_id_private *id_priv;
2802 struct net_device *ndev;
2805 id_priv = container_of(id, struct rdma_id_private, id);
2806 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2807 RDMA_CM_ROUTE_RESOLVED))
2810 id->route.path_rec = kmemdup(path_rec, sizeof(*path_rec),
2812 if (!id->route.path_rec) {
2817 if (rdma_protocol_roce(id->device, id->port_num)) {
2818 ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
2826 id->route.num_paths = 1;
2830 kfree(id->route.path_rec);
2831 id->route.path_rec = NULL;
2833 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
2836 EXPORT_SYMBOL(rdma_set_ib_path);
2838 static int cma_resolve_iw_route(struct rdma_id_private *id_priv)
2840 struct cma_work *work;
2842 work = kzalloc(sizeof *work, GFP_KERNEL);
2846 cma_init_resolve_route_work(work, id_priv);
2847 queue_work(cma_wq, &work->work);
2851 static int get_vlan_ndev_tc(struct net_device *vlan_ndev, int prio)
2853 struct net_device *dev;
2855 dev = vlan_dev_real_dev(vlan_ndev);
2857 return netdev_get_prio_tc_map(dev, prio);
2859 return (vlan_dev_get_egress_qos_mask(vlan_ndev, prio) &
2860 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2863 struct iboe_prio_tc_map {
2869 static int get_lower_vlan_dev_tc(struct net_device *dev,
2870 struct netdev_nested_priv *priv)
2872 struct iboe_prio_tc_map *map = (struct iboe_prio_tc_map *)priv->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);
2891 struct netdev_nested_priv priv;
2893 /* If VLAN device, get it directly from the VLAN netdev */
2894 if (is_vlan_dev(ndev))
2895 return get_vlan_ndev_tc(ndev, prio);
2897 prio_tc_map.input_prio = prio;
2898 priv.data = (void *)&prio_tc_map;
2900 netdev_walk_all_lower_dev_rcu(ndev,
2901 get_lower_vlan_dev_tc,
2904 /* If map is found from lower device, use it; Otherwise
2905 * continue with the current netdevice to get priority to tc map.
2907 if (prio_tc_map.found)
2908 return prio_tc_map.output_tc;
2909 else if (ndev->num_tc)
2910 return netdev_get_prio_tc_map(ndev, prio);
2915 static __be32 cma_get_roce_udp_flow_label(struct rdma_id_private *id_priv)
2917 struct sockaddr_in6 *addr6;
2921 addr6 = (struct sockaddr_in6 *)cma_src_addr(id_priv);
2922 fl = be32_to_cpu(addr6->sin6_flowinfo) & IB_GRH_FLOWLABEL_MASK;
2923 if ((cma_family(id_priv) != AF_INET6) || !fl) {
2924 dport = be16_to_cpu(cma_port(cma_dst_addr(id_priv)));
2925 sport = be16_to_cpu(cma_port(cma_src_addr(id_priv)));
2926 hash = (u32)sport * 31 + dport;
2927 fl = hash & IB_GRH_FLOWLABEL_MASK;
2930 return cpu_to_be32(fl);
2933 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2935 struct rdma_route *route = &id_priv->id.route;
2936 struct rdma_addr *addr = &route->addr;
2937 struct cma_work *work;
2939 struct net_device *ndev;
2941 u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num -
2942 rdma_start_port(id_priv->cma_dev->device)];
2943 u8 tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
2946 work = kzalloc(sizeof *work, GFP_KERNEL);
2950 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2951 if (!route->path_rec) {
2956 route->num_paths = 1;
2958 ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
2964 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2965 &route->path_rec->sgid);
2966 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2967 &route->path_rec->dgid);
2969 if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
2970 /* TODO: get the hoplimit from the inet/inet6 device */
2971 route->path_rec->hop_limit = addr->dev_addr.hoplimit;
2973 route->path_rec->hop_limit = 1;
2974 route->path_rec->reversible = 1;
2975 route->path_rec->pkey = cpu_to_be16(0xffff);
2976 route->path_rec->mtu_selector = IB_SA_EQ;
2977 route->path_rec->sl = iboe_tos_to_sl(ndev, tos);
2978 route->path_rec->traffic_class = tos;
2979 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
2980 route->path_rec->rate_selector = IB_SA_EQ;
2981 route->path_rec->rate = iboe_get_rate(ndev);
2983 route->path_rec->packet_life_time_selector = IB_SA_EQ;
2984 /* In case ACK timeout is set, use this value to calculate
2985 * PacketLifeTime. As per IBTA 12.7.34,
2986 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay).
2987 * Assuming a negligible local ACK delay, we can use
2988 * PacketLifeTime = local ACK timeout/2
2989 * as a reasonable approximation for RoCE networks.
2991 route->path_rec->packet_life_time = id_priv->timeout_set ?
2992 id_priv->timeout - 1 : CMA_IBOE_PACKET_LIFETIME;
2994 if (!route->path_rec->mtu) {
2999 if (rdma_protocol_roce_udp_encap(id_priv->id.device,
3000 id_priv->id.port_num))
3001 route->path_rec->flow_label =
3002 cma_get_roce_udp_flow_label(id_priv);
3004 cma_init_resolve_route_work(work, id_priv);
3005 queue_work(cma_wq, &work->work);
3010 kfree(route->path_rec);
3011 route->path_rec = NULL;
3012 route->num_paths = 0;
3018 int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms)
3020 struct rdma_id_private *id_priv;
3023 id_priv = container_of(id, struct rdma_id_private, id);
3024 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
3027 cma_id_get(id_priv);
3028 if (rdma_cap_ib_sa(id->device, id->port_num))
3029 ret = cma_resolve_ib_route(id_priv, timeout_ms);
3030 else if (rdma_protocol_roce(id->device, id->port_num))
3031 ret = cma_resolve_iboe_route(id_priv);
3032 else if (rdma_protocol_iwarp(id->device, id->port_num))
3033 ret = cma_resolve_iw_route(id_priv);
3042 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
3043 cma_id_put(id_priv);
3046 EXPORT_SYMBOL(rdma_resolve_route);
3048 static void cma_set_loopback(struct sockaddr *addr)
3050 switch (addr->sa_family) {
3052 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3055 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
3059 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
3065 static int cma_bind_loopback(struct rdma_id_private *id_priv)
3067 struct cma_device *cma_dev, *cur_dev;
3069 enum ib_port_state port_state;
3076 list_for_each_entry(cur_dev, &dev_list, list) {
3077 if (cma_family(id_priv) == AF_IB &&
3078 !rdma_cap_ib_cm(cur_dev->device, 1))
3084 rdma_for_each_port (cur_dev->device, p) {
3085 if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) &&
3086 port_state == IB_PORT_ACTIVE) {
3101 ret = rdma_query_gid(cma_dev->device, p, 0, &gid);
3105 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
3109 id_priv->id.route.addr.dev_addr.dev_type =
3110 (rdma_protocol_ib(cma_dev->device, p)) ?
3111 ARPHRD_INFINIBAND : ARPHRD_ETHER;
3113 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3114 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
3115 id_priv->id.port_num = p;
3116 cma_attach_to_dev(id_priv, cma_dev);
3117 cma_set_loopback(cma_src_addr(id_priv));
3119 mutex_unlock(&lock);
3123 static void addr_handler(int status, struct sockaddr *src_addr,
3124 struct rdma_dev_addr *dev_addr, void *context)
3126 struct rdma_id_private *id_priv = context;
3127 struct rdma_cm_event event = {};
3128 struct sockaddr *addr;
3129 struct sockaddr_storage old_addr;
3131 mutex_lock(&id_priv->handler_mutex);
3132 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
3133 RDMA_CM_ADDR_RESOLVED))
3137 * Store the previous src address, so that if we fail to acquire
3138 * matching rdma device, old address can be restored back, which helps
3139 * to cancel the cma listen operation correctly.
3141 addr = cma_src_addr(id_priv);
3142 memcpy(&old_addr, addr, rdma_addr_size(addr));
3143 memcpy(addr, src_addr, rdma_addr_size(src_addr));
3144 if (!status && !id_priv->cma_dev) {
3145 status = cma_acquire_dev_by_src_ip(id_priv);
3147 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
3149 } else if (status) {
3150 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status);
3154 memcpy(addr, &old_addr,
3155 rdma_addr_size((struct sockaddr *)&old_addr));
3156 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3157 RDMA_CM_ADDR_BOUND))
3159 event.event = RDMA_CM_EVENT_ADDR_ERROR;
3160 event.status = status;
3162 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3164 if (cma_cm_event_handler(id_priv, &event)) {
3165 destroy_id_handler_unlock(id_priv);
3169 mutex_unlock(&id_priv->handler_mutex);
3172 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
3174 struct cma_work *work;
3178 work = kzalloc(sizeof *work, GFP_KERNEL);
3182 if (!id_priv->cma_dev) {
3183 ret = cma_bind_loopback(id_priv);
3188 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3189 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
3191 enqueue_resolve_addr_work(work, id_priv);
3198 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
3200 struct cma_work *work;
3203 work = kzalloc(sizeof *work, GFP_KERNEL);
3207 if (!id_priv->cma_dev) {
3208 ret = cma_resolve_ib_dev(id_priv);
3213 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
3214 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
3216 enqueue_resolve_addr_work(work, id_priv);
3223 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3224 const struct sockaddr *dst_addr)
3226 if (!src_addr || !src_addr->sa_family) {
3227 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
3228 src_addr->sa_family = dst_addr->sa_family;
3229 if (IS_ENABLED(CONFIG_IPV6) &&
3230 dst_addr->sa_family == AF_INET6) {
3231 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
3232 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
3233 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
3234 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
3235 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
3236 } else if (dst_addr->sa_family == AF_IB) {
3237 ((struct sockaddr_ib *) src_addr)->sib_pkey =
3238 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
3241 return rdma_bind_addr(id, src_addr);
3245 * If required, resolve the source address for bind and leave the id_priv in
3246 * state RDMA_CM_ADDR_BOUND. This oddly uses the state to determine the prior
3247 * calls made by ULP, a previously bound ID will not be re-bound and src_addr is
3250 static int resolve_prepare_src(struct rdma_id_private *id_priv,
3251 struct sockaddr *src_addr,
3252 const struct sockaddr *dst_addr)
3256 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
3257 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
3258 /* For a well behaved ULP state will be RDMA_CM_IDLE */
3259 ret = cma_bind_addr(&id_priv->id, src_addr, dst_addr);
3262 if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3263 RDMA_CM_ADDR_QUERY))) {
3269 if (cma_family(id_priv) != dst_addr->sa_family) {
3276 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3278 memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
3282 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3283 const struct sockaddr *dst_addr, unsigned long timeout_ms)
3285 struct rdma_id_private *id_priv =
3286 container_of(id, struct rdma_id_private, id);
3289 ret = resolve_prepare_src(id_priv, src_addr, dst_addr);
3293 if (cma_any_addr(dst_addr)) {
3294 ret = cma_resolve_loopback(id_priv);
3296 if (dst_addr->sa_family == AF_IB) {
3297 ret = cma_resolve_ib_addr(id_priv);
3299 ret = rdma_resolve_ip(cma_src_addr(id_priv), dst_addr,
3300 &id->route.addr.dev_addr,
3301 timeout_ms, addr_handler,
3310 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3313 EXPORT_SYMBOL(rdma_resolve_addr);
3315 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
3317 struct rdma_id_private *id_priv;
3318 unsigned long flags;
3321 id_priv = container_of(id, struct rdma_id_private, id);
3322 spin_lock_irqsave(&id_priv->lock, flags);
3323 if ((reuse && id_priv->state != RDMA_CM_LISTEN) ||
3324 id_priv->state == RDMA_CM_IDLE) {
3325 id_priv->reuseaddr = reuse;
3330 spin_unlock_irqrestore(&id_priv->lock, flags);
3333 EXPORT_SYMBOL(rdma_set_reuseaddr);
3335 int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
3337 struct rdma_id_private *id_priv;
3338 unsigned long flags;
3341 id_priv = container_of(id, struct rdma_id_private, id);
3342 spin_lock_irqsave(&id_priv->lock, flags);
3343 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
3344 id_priv->options |= (1 << CMA_OPTION_AFONLY);
3345 id_priv->afonly = afonly;
3350 spin_unlock_irqrestore(&id_priv->lock, flags);
3353 EXPORT_SYMBOL(rdma_set_afonly);
3355 static void cma_bind_port(struct rdma_bind_list *bind_list,
3356 struct rdma_id_private *id_priv)
3358 struct sockaddr *addr;
3359 struct sockaddr_ib *sib;
3363 lockdep_assert_held(&lock);
3365 addr = cma_src_addr(id_priv);
3366 port = htons(bind_list->port);
3368 switch (addr->sa_family) {
3370 ((struct sockaddr_in *) addr)->sin_port = port;
3373 ((struct sockaddr_in6 *) addr)->sin6_port = port;
3376 sib = (struct sockaddr_ib *) addr;
3377 sid = be64_to_cpu(sib->sib_sid);
3378 mask = be64_to_cpu(sib->sib_sid_mask);
3379 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
3380 sib->sib_sid_mask = cpu_to_be64(~0ULL);
3383 id_priv->bind_list = bind_list;
3384 hlist_add_head(&id_priv->node, &bind_list->owners);
3387 static int cma_alloc_port(enum rdma_ucm_port_space ps,
3388 struct rdma_id_private *id_priv, unsigned short snum)
3390 struct rdma_bind_list *bind_list;
3393 lockdep_assert_held(&lock);
3395 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
3399 ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
3405 bind_list->port = snum;
3406 cma_bind_port(bind_list, id_priv);
3410 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
3413 static int cma_port_is_unique(struct rdma_bind_list *bind_list,
3414 struct rdma_id_private *id_priv)
3416 struct rdma_id_private *cur_id;
3417 struct sockaddr *daddr = cma_dst_addr(id_priv);
3418 struct sockaddr *saddr = cma_src_addr(id_priv);
3419 __be16 dport = cma_port(daddr);
3421 lockdep_assert_held(&lock);
3423 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3424 struct sockaddr *cur_daddr = cma_dst_addr(cur_id);
3425 struct sockaddr *cur_saddr = cma_src_addr(cur_id);
3426 __be16 cur_dport = cma_port(cur_daddr);
3428 if (id_priv == cur_id)
3431 /* different dest port -> unique */
3432 if (!cma_any_port(daddr) &&
3433 !cma_any_port(cur_daddr) &&
3434 (dport != cur_dport))
3437 /* different src address -> unique */
3438 if (!cma_any_addr(saddr) &&
3439 !cma_any_addr(cur_saddr) &&
3440 cma_addr_cmp(saddr, cur_saddr))
3443 /* different dst address -> unique */
3444 if (!cma_any_addr(daddr) &&
3445 !cma_any_addr(cur_daddr) &&
3446 cma_addr_cmp(daddr, cur_daddr))
3449 return -EADDRNOTAVAIL;
3454 static int cma_alloc_any_port(enum rdma_ucm_port_space ps,
3455 struct rdma_id_private *id_priv)
3457 static unsigned int last_used_port;
3458 int low, high, remaining;
3460 struct net *net = id_priv->id.route.addr.dev_addr.net;
3462 lockdep_assert_held(&lock);
3464 inet_get_local_port_range(net, &low, &high);
3465 remaining = (high - low) + 1;
3466 rover = prandom_u32() % remaining + low;
3468 if (last_used_port != rover) {
3469 struct rdma_bind_list *bind_list;
3472 bind_list = cma_ps_find(net, ps, (unsigned short)rover);
3475 ret = cma_alloc_port(ps, id_priv, rover);
3477 ret = cma_port_is_unique(bind_list, id_priv);
3479 cma_bind_port(bind_list, id_priv);
3482 * Remember previously used port number in order to avoid
3483 * re-using same port immediately after it is closed.
3486 last_used_port = rover;
3487 if (ret != -EADDRNOTAVAIL)
3492 if ((rover < low) || (rover > high))
3496 return -EADDRNOTAVAIL;
3500 * Check that the requested port is available. This is called when trying to
3501 * bind to a specific port, or when trying to listen on a bound port. In
3502 * the latter case, the provided id_priv may already be on the bind_list, but
3503 * we still need to check that it's okay to start listening.
3505 static int cma_check_port(struct rdma_bind_list *bind_list,
3506 struct rdma_id_private *id_priv, uint8_t reuseaddr)
3508 struct rdma_id_private *cur_id;
3509 struct sockaddr *addr, *cur_addr;
3511 lockdep_assert_held(&lock);
3513 addr = cma_src_addr(id_priv);
3514 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3515 if (id_priv == cur_id)
3518 if (reuseaddr && cur_id->reuseaddr)
3521 cur_addr = cma_src_addr(cur_id);
3522 if (id_priv->afonly && cur_id->afonly &&
3523 (addr->sa_family != cur_addr->sa_family))
3526 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3527 return -EADDRNOTAVAIL;
3529 if (!cma_addr_cmp(addr, cur_addr))
3535 static int cma_use_port(enum rdma_ucm_port_space ps,
3536 struct rdma_id_private *id_priv)
3538 struct rdma_bind_list *bind_list;
3539 unsigned short snum;
3542 lockdep_assert_held(&lock);
3544 snum = ntohs(cma_port(cma_src_addr(id_priv)));
3545 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3548 bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3550 ret = cma_alloc_port(ps, id_priv, snum);
3552 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3554 cma_bind_port(bind_list, id_priv);
3559 static enum rdma_ucm_port_space
3560 cma_select_inet_ps(struct rdma_id_private *id_priv)
3562 switch (id_priv->id.ps) {
3567 return id_priv->id.ps;
3574 static enum rdma_ucm_port_space
3575 cma_select_ib_ps(struct rdma_id_private *id_priv)
3577 enum rdma_ucm_port_space ps = 0;
3578 struct sockaddr_ib *sib;
3579 u64 sid_ps, mask, sid;
3581 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3582 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3583 sid = be64_to_cpu(sib->sib_sid) & mask;
3585 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3586 sid_ps = RDMA_IB_IP_PS_IB;
3588 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3589 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3590 sid_ps = RDMA_IB_IP_PS_TCP;
3592 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3593 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3594 sid_ps = RDMA_IB_IP_PS_UDP;
3599 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3600 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3601 be64_to_cpu(sib->sib_sid_mask));
3606 static int cma_get_port(struct rdma_id_private *id_priv)
3608 enum rdma_ucm_port_space ps;
3611 if (cma_family(id_priv) != AF_IB)
3612 ps = cma_select_inet_ps(id_priv);
3614 ps = cma_select_ib_ps(id_priv);
3616 return -EPROTONOSUPPORT;
3619 if (cma_any_port(cma_src_addr(id_priv)))
3620 ret = cma_alloc_any_port(ps, id_priv);
3622 ret = cma_use_port(ps, id_priv);
3623 mutex_unlock(&lock);
3628 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3629 struct sockaddr *addr)
3631 #if IS_ENABLED(CONFIG_IPV6)
3632 struct sockaddr_in6 *sin6;
3634 if (addr->sa_family != AF_INET6)
3637 sin6 = (struct sockaddr_in6 *) addr;
3639 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3642 if (!sin6->sin6_scope_id)
3645 dev_addr->bound_dev_if = sin6->sin6_scope_id;
3650 int rdma_listen(struct rdma_cm_id *id, int backlog)
3652 struct rdma_id_private *id_priv =
3653 container_of(id, struct rdma_id_private, id);
3656 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) {
3657 /* For a well behaved ULP state will be RDMA_CM_IDLE */
3658 id->route.addr.src_addr.ss_family = AF_INET;
3659 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
3662 if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3668 * Once the ID reaches RDMA_CM_LISTEN it is not allowed to be reusable
3669 * any more, and has to be unique in the bind list.
3671 if (id_priv->reuseaddr) {
3673 ret = cma_check_port(id_priv->bind_list, id_priv, 0);
3675 id_priv->reuseaddr = 0;
3676 mutex_unlock(&lock);
3681 id_priv->backlog = backlog;
3683 if (rdma_cap_ib_cm(id->device, 1)) {
3684 ret = cma_ib_listen(id_priv);
3687 } else if (rdma_cap_iw_cm(id->device, 1)) {
3688 ret = cma_iw_listen(id_priv, backlog);
3696 cma_listen_on_all(id_priv);
3700 id_priv->backlog = 0;
3702 * All the failure paths that lead here will not allow the req_handler's
3705 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3708 EXPORT_SYMBOL(rdma_listen);
3710 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3712 struct rdma_id_private *id_priv;
3714 struct sockaddr *daddr;
3716 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3717 addr->sa_family != AF_IB)
3718 return -EAFNOSUPPORT;
3720 id_priv = container_of(id, struct rdma_id_private, id);
3721 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3724 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3728 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
3729 if (!cma_any_addr(addr)) {
3730 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
3734 ret = cma_acquire_dev_by_src_ip(id_priv);
3739 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3740 if (addr->sa_family == AF_INET)
3741 id_priv->afonly = 1;
3742 #if IS_ENABLED(CONFIG_IPV6)
3743 else if (addr->sa_family == AF_INET6) {
3744 struct net *net = id_priv->id.route.addr.dev_addr.net;
3746 id_priv->afonly = net->ipv6.sysctl.bindv6only;
3750 daddr = cma_dst_addr(id_priv);
3751 daddr->sa_family = addr->sa_family;
3753 ret = cma_get_port(id_priv);
3759 if (id_priv->cma_dev)
3760 cma_release_dev(id_priv);
3762 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
3765 EXPORT_SYMBOL(rdma_bind_addr);
3767 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
3769 struct cma_hdr *cma_hdr;
3772 cma_hdr->cma_version = CMA_VERSION;
3773 if (cma_family(id_priv) == AF_INET) {
3774 struct sockaddr_in *src4, *dst4;
3776 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3777 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
3779 cma_set_ip_ver(cma_hdr, 4);
3780 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3781 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3782 cma_hdr->port = src4->sin_port;
3783 } else if (cma_family(id_priv) == AF_INET6) {
3784 struct sockaddr_in6 *src6, *dst6;
3786 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3787 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
3789 cma_set_ip_ver(cma_hdr, 6);
3790 cma_hdr->src_addr.ip6 = src6->sin6_addr;
3791 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3792 cma_hdr->port = src6->sin6_port;
3797 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3798 const struct ib_cm_event *ib_event)
3800 struct rdma_id_private *id_priv = cm_id->context;
3801 struct rdma_cm_event event = {};
3802 const struct ib_cm_sidr_rep_event_param *rep =
3803 &ib_event->param.sidr_rep_rcvd;
3806 mutex_lock(&id_priv->handler_mutex);
3807 if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
3810 switch (ib_event->event) {
3811 case IB_CM_SIDR_REQ_ERROR:
3812 event.event = RDMA_CM_EVENT_UNREACHABLE;
3813 event.status = -ETIMEDOUT;
3815 case IB_CM_SIDR_REP_RECEIVED:
3816 event.param.ud.private_data = ib_event->private_data;
3817 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3818 if (rep->status != IB_SIDR_SUCCESS) {
3819 event.event = RDMA_CM_EVENT_UNREACHABLE;
3820 event.status = ib_event->param.sidr_rep_rcvd.status;
3821 pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
3825 ret = cma_set_qkey(id_priv, rep->qkey);
3827 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret);
3828 event.event = RDMA_CM_EVENT_ADDR_ERROR;
3832 ib_init_ah_attr_from_path(id_priv->id.device,
3833 id_priv->id.port_num,
3834 id_priv->id.route.path_rec,
3835 &event.param.ud.ah_attr,
3837 event.param.ud.qp_num = rep->qpn;
3838 event.param.ud.qkey = rep->qkey;
3839 event.event = RDMA_CM_EVENT_ESTABLISHED;
3843 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3848 ret = cma_cm_event_handler(id_priv, &event);
3850 rdma_destroy_ah_attr(&event.param.ud.ah_attr);
3852 /* Destroy the CM ID by returning a non-zero value. */
3853 id_priv->cm_id.ib = NULL;
3854 destroy_id_handler_unlock(id_priv);
3858 mutex_unlock(&id_priv->handler_mutex);
3862 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3863 struct rdma_conn_param *conn_param)
3865 struct ib_cm_sidr_req_param req;
3866 struct ib_cm_id *id;
3871 memset(&req, 0, sizeof req);
3872 offset = cma_user_data_offset(id_priv);
3873 req.private_data_len = offset + conn_param->private_data_len;
3874 if (req.private_data_len < conn_param->private_data_len)
3877 if (req.private_data_len) {
3878 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3882 private_data = NULL;
3885 if (conn_param->private_data && conn_param->private_data_len)
3886 memcpy(private_data + offset, conn_param->private_data,
3887 conn_param->private_data_len);
3890 ret = cma_format_hdr(private_data, id_priv);
3893 req.private_data = private_data;
3896 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3902 id_priv->cm_id.ib = id;
3904 req.path = id_priv->id.route.path_rec;
3905 req.sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
3906 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3907 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3908 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3910 trace_cm_send_sidr_req(id_priv);
3911 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3913 ib_destroy_cm_id(id_priv->cm_id.ib);
3914 id_priv->cm_id.ib = NULL;
3917 kfree(private_data);
3921 static int cma_connect_ib(struct rdma_id_private *id_priv,
3922 struct rdma_conn_param *conn_param)
3924 struct ib_cm_req_param req;
3925 struct rdma_route *route;
3927 struct ib_cm_id *id;
3931 memset(&req, 0, sizeof req);
3932 offset = cma_user_data_offset(id_priv);
3933 req.private_data_len = offset + conn_param->private_data_len;
3934 if (req.private_data_len < conn_param->private_data_len)
3937 if (req.private_data_len) {
3938 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3942 private_data = NULL;
3945 if (conn_param->private_data && conn_param->private_data_len)
3946 memcpy(private_data + offset, conn_param->private_data,
3947 conn_param->private_data_len);
3949 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3954 id_priv->cm_id.ib = id;
3956 route = &id_priv->id.route;
3958 ret = cma_format_hdr(private_data, id_priv);
3961 req.private_data = private_data;
3964 req.primary_path = &route->path_rec[0];
3965 if (route->num_paths == 2)
3966 req.alternate_path = &route->path_rec[1];
3968 req.ppath_sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
3969 /* Alternate path SGID attribute currently unsupported */
3970 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3971 req.qp_num = id_priv->qp_num;
3972 req.qp_type = id_priv->id.qp_type;
3973 req.starting_psn = id_priv->seq_num;
3974 req.responder_resources = conn_param->responder_resources;
3975 req.initiator_depth = conn_param->initiator_depth;
3976 req.flow_control = conn_param->flow_control;
3977 req.retry_count = min_t(u8, 7, conn_param->retry_count);
3978 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3979 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3980 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3981 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3982 req.srq = id_priv->srq ? 1 : 0;
3983 req.ece.vendor_id = id_priv->ece.vendor_id;
3984 req.ece.attr_mod = id_priv->ece.attr_mod;
3986 trace_cm_send_req(id_priv);
3987 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3989 if (ret && !IS_ERR(id)) {
3990 ib_destroy_cm_id(id);
3991 id_priv->cm_id.ib = NULL;
3994 kfree(private_data);
3998 static int cma_connect_iw(struct rdma_id_private *id_priv,
3999 struct rdma_conn_param *conn_param)
4001 struct iw_cm_id *cm_id;
4003 struct iw_cm_conn_param iw_param;
4005 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
4007 return PTR_ERR(cm_id);
4009 cm_id->tos = id_priv->tos;
4010 cm_id->tos_set = id_priv->tos_set;
4011 id_priv->cm_id.iw = cm_id;
4013 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
4014 rdma_addr_size(cma_src_addr(id_priv)));
4015 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
4016 rdma_addr_size(cma_dst_addr(id_priv)));
4018 ret = cma_modify_qp_rtr(id_priv, conn_param);
4023 iw_param.ord = conn_param->initiator_depth;
4024 iw_param.ird = conn_param->responder_resources;
4025 iw_param.private_data = conn_param->private_data;
4026 iw_param.private_data_len = conn_param->private_data_len;
4027 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
4029 memset(&iw_param, 0, sizeof iw_param);
4030 iw_param.qpn = id_priv->qp_num;
4032 ret = iw_cm_connect(cm_id, &iw_param);
4035 iw_destroy_cm_id(cm_id);
4036 id_priv->cm_id.iw = NULL;
4041 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4043 struct rdma_id_private *id_priv =
4044 container_of(id, struct rdma_id_private, id);
4047 mutex_lock(&id_priv->handler_mutex);
4048 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT)) {
4054 id_priv->qp_num = conn_param->qp_num;
4055 id_priv->srq = conn_param->srq;
4058 if (rdma_cap_ib_cm(id->device, id->port_num)) {
4059 if (id->qp_type == IB_QPT_UD)
4060 ret = cma_resolve_ib_udp(id_priv, conn_param);
4062 ret = cma_connect_ib(id_priv, conn_param);
4063 } else if (rdma_cap_iw_cm(id->device, id->port_num))
4064 ret = cma_connect_iw(id_priv, conn_param);
4069 mutex_unlock(&id_priv->handler_mutex);
4072 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
4074 mutex_unlock(&id_priv->handler_mutex);
4077 EXPORT_SYMBOL(rdma_connect);
4080 * rdma_connect_ece - Initiate an active connection request with ECE data.
4081 * @id: Connection identifier to connect.
4082 * @conn_param: Connection information used for connected QPs.
4083 * @ece: ECE parameters
4085 * See rdma_connect() explanation.
4087 int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4088 struct rdma_ucm_ece *ece)
4090 struct rdma_id_private *id_priv =
4091 container_of(id, struct rdma_id_private, id);
4093 id_priv->ece.vendor_id = ece->vendor_id;
4094 id_priv->ece.attr_mod = ece->attr_mod;
4096 return rdma_connect(id, conn_param);
4098 EXPORT_SYMBOL(rdma_connect_ece);
4100 static int cma_accept_ib(struct rdma_id_private *id_priv,
4101 struct rdma_conn_param *conn_param)
4103 struct ib_cm_rep_param rep;
4106 ret = cma_modify_qp_rtr(id_priv, conn_param);
4110 ret = cma_modify_qp_rts(id_priv, conn_param);
4114 memset(&rep, 0, sizeof rep);
4115 rep.qp_num = id_priv->qp_num;
4116 rep.starting_psn = id_priv->seq_num;
4117 rep.private_data = conn_param->private_data;
4118 rep.private_data_len = conn_param->private_data_len;
4119 rep.responder_resources = conn_param->responder_resources;
4120 rep.initiator_depth = conn_param->initiator_depth;
4121 rep.failover_accepted = 0;
4122 rep.flow_control = conn_param->flow_control;
4123 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4124 rep.srq = id_priv->srq ? 1 : 0;
4125 rep.ece.vendor_id = id_priv->ece.vendor_id;
4126 rep.ece.attr_mod = id_priv->ece.attr_mod;
4128 trace_cm_send_rep(id_priv);
4129 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
4134 static int cma_accept_iw(struct rdma_id_private *id_priv,
4135 struct rdma_conn_param *conn_param)
4137 struct iw_cm_conn_param iw_param;
4143 ret = cma_modify_qp_rtr(id_priv, conn_param);
4147 iw_param.ord = conn_param->initiator_depth;
4148 iw_param.ird = conn_param->responder_resources;
4149 iw_param.private_data = conn_param->private_data;
4150 iw_param.private_data_len = conn_param->private_data_len;
4151 if (id_priv->id.qp) {
4152 iw_param.qpn = id_priv->qp_num;
4154 iw_param.qpn = conn_param->qp_num;
4156 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
4159 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
4160 enum ib_cm_sidr_status status, u32 qkey,
4161 const void *private_data, int private_data_len)
4163 struct ib_cm_sidr_rep_param rep;
4166 memset(&rep, 0, sizeof rep);
4167 rep.status = status;
4168 if (status == IB_SIDR_SUCCESS) {
4169 ret = cma_set_qkey(id_priv, qkey);
4172 rep.qp_num = id_priv->qp_num;
4173 rep.qkey = id_priv->qkey;
4175 rep.ece.vendor_id = id_priv->ece.vendor_id;
4176 rep.ece.attr_mod = id_priv->ece.attr_mod;
4179 rep.private_data = private_data;
4180 rep.private_data_len = private_data_len;
4182 trace_cm_send_sidr_rep(id_priv);
4183 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
4187 * rdma_accept - Called to accept a connection request or response.
4188 * @id: Connection identifier associated with the request.
4189 * @conn_param: Information needed to establish the connection. This must be
4190 * provided if accepting a connection request. If accepting a connection
4191 * response, this parameter must be NULL.
4193 * Typically, this routine is only called by the listener to accept a connection
4194 * request. It must also be called on the active side of a connection if the
4195 * user is performing their own QP transitions.
4197 * In the case of error, a reject message is sent to the remote side and the
4198 * state of the qp associated with the id is modified to error, such that any
4199 * previously posted receive buffers would be flushed.
4201 * This function is for use by kernel ULPs and must be called from under the
4204 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4206 struct rdma_id_private *id_priv =
4207 container_of(id, struct rdma_id_private, id);
4210 lockdep_assert_held(&id_priv->handler_mutex);
4212 if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
4215 if (!id->qp && conn_param) {
4216 id_priv->qp_num = conn_param->qp_num;
4217 id_priv->srq = conn_param->srq;
4220 if (rdma_cap_ib_cm(id->device, id->port_num)) {
4221 if (id->qp_type == IB_QPT_UD) {
4223 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4225 conn_param->private_data,
4226 conn_param->private_data_len);
4228 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4232 ret = cma_accept_ib(id_priv, conn_param);
4234 ret = cma_rep_recv(id_priv);
4236 } else if (rdma_cap_iw_cm(id->device, id->port_num))
4237 ret = cma_accept_iw(id_priv, conn_param);
4246 cma_modify_qp_err(id_priv);
4247 rdma_reject(id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
4250 EXPORT_SYMBOL(rdma_accept);
4252 int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4253 struct rdma_ucm_ece *ece)
4255 struct rdma_id_private *id_priv =
4256 container_of(id, struct rdma_id_private, id);
4258 id_priv->ece.vendor_id = ece->vendor_id;
4259 id_priv->ece.attr_mod = ece->attr_mod;
4261 return rdma_accept(id, conn_param);
4263 EXPORT_SYMBOL(rdma_accept_ece);
4265 void rdma_lock_handler(struct rdma_cm_id *id)
4267 struct rdma_id_private *id_priv =
4268 container_of(id, struct rdma_id_private, id);
4270 mutex_lock(&id_priv->handler_mutex);
4272 EXPORT_SYMBOL(rdma_lock_handler);
4274 void rdma_unlock_handler(struct rdma_cm_id *id)
4276 struct rdma_id_private *id_priv =
4277 container_of(id, struct rdma_id_private, id);
4279 mutex_unlock(&id_priv->handler_mutex);
4281 EXPORT_SYMBOL(rdma_unlock_handler);
4283 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
4285 struct rdma_id_private *id_priv;
4288 id_priv = container_of(id, struct rdma_id_private, id);
4289 if (!id_priv->cm_id.ib)
4292 switch (id->device->node_type) {
4293 case RDMA_NODE_IB_CA:
4294 ret = ib_cm_notify(id_priv->cm_id.ib, event);
4302 EXPORT_SYMBOL(rdma_notify);
4304 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
4305 u8 private_data_len, u8 reason)
4307 struct rdma_id_private *id_priv;
4310 id_priv = container_of(id, struct rdma_id_private, id);
4311 if (!id_priv->cm_id.ib)
4314 if (rdma_cap_ib_cm(id->device, id->port_num)) {
4315 if (id->qp_type == IB_QPT_UD) {
4316 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
4317 private_data, private_data_len);
4319 trace_cm_send_rej(id_priv);
4320 ret = ib_send_cm_rej(id_priv->cm_id.ib, reason, NULL, 0,
4321 private_data, private_data_len);
4323 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4324 ret = iw_cm_reject(id_priv->cm_id.iw,
4325 private_data, private_data_len);
4331 EXPORT_SYMBOL(rdma_reject);
4333 int rdma_disconnect(struct rdma_cm_id *id)
4335 struct rdma_id_private *id_priv;
4338 id_priv = container_of(id, struct rdma_id_private, id);
4339 if (!id_priv->cm_id.ib)
4342 if (rdma_cap_ib_cm(id->device, id->port_num)) {
4343 ret = cma_modify_qp_err(id_priv);
4346 /* Initiate or respond to a disconnect. */
4347 trace_cm_disconnect(id_priv);
4348 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) {
4349 if (!ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0))
4350 trace_cm_sent_drep(id_priv);
4352 trace_cm_sent_dreq(id_priv);
4354 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4355 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
4362 EXPORT_SYMBOL(rdma_disconnect);
4364 static void cma_make_mc_event(int status, struct rdma_id_private *id_priv,
4365 struct ib_sa_multicast *multicast,
4366 struct rdma_cm_event *event,
4367 struct cma_multicast *mc)
4369 struct rdma_dev_addr *dev_addr;
4370 enum ib_gid_type gid_type;
4371 struct net_device *ndev;
4374 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
4376 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
4379 event->status = status;
4380 event->param.ud.private_data = mc->context;
4382 event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4386 dev_addr = &id_priv->id.route.addr.dev_addr;
4387 ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4390 ->default_gid_type[id_priv->id.port_num -
4392 id_priv->cma_dev->device)];
4394 event->event = RDMA_CM_EVENT_MULTICAST_JOIN;
4395 if (ib_init_ah_from_mcmember(id_priv->id.device, id_priv->id.port_num,
4396 &multicast->rec, ndev, gid_type,
4397 &event->param.ud.ah_attr)) {
4398 event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4402 event->param.ud.qp_num = 0xFFFFFF;
4403 event->param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
4410 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
4412 struct cma_multicast *mc = multicast->context;
4413 struct rdma_id_private *id_priv = mc->id_priv;
4414 struct rdma_cm_event event = {};
4417 mutex_lock(&id_priv->handler_mutex);
4418 if (READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL ||
4419 READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING)
4422 cma_make_mc_event(status, id_priv, multicast, &event, mc);
4423 ret = cma_cm_event_handler(id_priv, &event);
4424 rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4426 destroy_id_handler_unlock(id_priv);
4431 mutex_unlock(&id_priv->handler_mutex);
4435 static void cma_set_mgid(struct rdma_id_private *id_priv,
4436 struct sockaddr *addr, union ib_gid *mgid)
4438 unsigned char mc_map[MAX_ADDR_LEN];
4439 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4440 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
4441 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
4443 if (cma_any_addr(addr)) {
4444 memset(mgid, 0, sizeof *mgid);
4445 } else if ((addr->sa_family == AF_INET6) &&
4446 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
4448 /* IPv6 address is an SA assigned MGID. */
4449 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4450 } else if (addr->sa_family == AF_IB) {
4451 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
4452 } else if (addr->sa_family == AF_INET6) {
4453 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
4454 if (id_priv->id.ps == RDMA_PS_UDP)
4455 mc_map[7] = 0x01; /* Use RDMA CM signature */
4456 *mgid = *(union ib_gid *) (mc_map + 4);
4458 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
4459 if (id_priv->id.ps == RDMA_PS_UDP)
4460 mc_map[7] = 0x01; /* Use RDMA CM signature */
4461 *mgid = *(union ib_gid *) (mc_map + 4);
4465 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
4466 struct cma_multicast *mc)
4468 struct ib_sa_mcmember_rec rec;
4469 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4470 ib_sa_comp_mask comp_mask;
4473 ib_addr_get_mgid(dev_addr, &rec.mgid);
4474 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
4479 ret = cma_set_qkey(id_priv, 0);
4483 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
4484 rec.qkey = cpu_to_be32(id_priv->qkey);
4485 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
4486 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
4487 rec.join_state = mc->join_state;
4489 if ((rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) &&
4490 (!ib_sa_sendonly_fullmem_support(&sa_client,
4492 id_priv->id.port_num))) {
4494 &id_priv->id.device->dev,
4495 "RDMA CM: port %u Unable to multicast join: SM doesn't support Send Only Full Member option\n",
4496 id_priv->id.port_num);
4500 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
4501 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
4502 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
4503 IB_SA_MCMEMBER_REC_FLOW_LABEL |
4504 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
4506 if (id_priv->id.ps == RDMA_PS_IPOIB)
4507 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
4508 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
4509 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
4510 IB_SA_MCMEMBER_REC_MTU |
4511 IB_SA_MCMEMBER_REC_HOP_LIMIT;
4513 mc->sa_mc = ib_sa_join_multicast(&sa_client, id_priv->id.device,
4514 id_priv->id.port_num, &rec, comp_mask,
4515 GFP_KERNEL, cma_ib_mc_handler, mc);
4516 return PTR_ERR_OR_ZERO(mc->sa_mc);
4519 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
4520 enum ib_gid_type gid_type)
4522 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
4523 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
4525 if (cma_any_addr(addr)) {
4526 memset(mgid, 0, sizeof *mgid);
4527 } else if (addr->sa_family == AF_INET6) {
4528 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4531 (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0xff;
4533 (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0x0e;
4542 mgid->raw[10] = 0xff;
4543 mgid->raw[11] = 0xff;
4544 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
4548 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
4549 struct cma_multicast *mc)
4551 struct cma_work *work;
4552 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4554 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
4555 struct net_device *ndev = NULL;
4556 struct ib_sa_multicast ib;
4557 enum ib_gid_type gid_type;
4560 send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
4562 if (cma_zero_addr(addr))
4565 work = kzalloc(sizeof *work, GFP_KERNEL);
4569 gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4570 rdma_start_port(id_priv->cma_dev->device)];
4571 cma_iboe_set_mgid(addr, &ib.rec.mgid, gid_type);
4573 ib.rec.pkey = cpu_to_be16(0xffff);
4574 if (id_priv->id.ps == RDMA_PS_UDP)
4575 ib.rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
4577 if (dev_addr->bound_dev_if)
4578 ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4583 ib.rec.rate = iboe_get_rate(ndev);
4584 ib.rec.hop_limit = 1;
4585 ib.rec.mtu = iboe_get_mtu(ndev->mtu);
4587 if (addr->sa_family == AF_INET) {
4588 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4589 ib.rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4591 err = cma_igmp_send(ndev, &ib.rec.mgid,
4596 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4600 if (err || !ib.rec.mtu) {
4605 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4608 INIT_WORK(&work->work, cma_work_handler);
4609 cma_make_mc_event(0, id_priv, &ib, &work->event, mc);
4610 /* Balances with cma_id_put() in cma_work_handler */
4611 cma_id_get(id_priv);
4612 queue_work(cma_wq, &work->work);
4620 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4621 u8 join_state, void *context)
4623 struct rdma_id_private *id_priv =
4624 container_of(id, struct rdma_id_private, id);
4625 struct cma_multicast *mc;
4628 /* Not supported for kernel QPs */
4629 if (WARN_ON(id->qp))
4632 /* ULP is calling this wrong. */
4633 if (!id->device || (READ_ONCE(id_priv->state) != RDMA_CM_ADDR_BOUND &&
4634 READ_ONCE(id_priv->state) != RDMA_CM_ADDR_RESOLVED))
4637 mc = kzalloc(sizeof(*mc), GFP_KERNEL);
4641 memcpy(&mc->addr, addr, rdma_addr_size(addr));
4642 mc->context = context;
4643 mc->id_priv = id_priv;
4644 mc->join_state = join_state;
4646 if (rdma_protocol_roce(id->device, id->port_num)) {
4647 ret = cma_iboe_join_multicast(id_priv, mc);
4650 } else if (rdma_cap_ib_mcast(id->device, id->port_num)) {
4651 ret = cma_join_ib_multicast(id_priv, mc);
4659 spin_lock(&id_priv->lock);
4660 list_add(&mc->list, &id_priv->mc_list);
4661 spin_unlock(&id_priv->lock);
4668 EXPORT_SYMBOL(rdma_join_multicast);
4670 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4672 struct rdma_id_private *id_priv;
4673 struct cma_multicast *mc;
4675 id_priv = container_of(id, struct rdma_id_private, id);
4676 spin_lock_irq(&id_priv->lock);
4677 list_for_each_entry(mc, &id_priv->mc_list, list) {
4678 if (memcmp(&mc->addr, addr, rdma_addr_size(addr)) != 0)
4680 list_del(&mc->list);
4681 spin_unlock_irq(&id_priv->lock);
4683 WARN_ON(id_priv->cma_dev->device != id->device);
4684 destroy_mc(id_priv, mc);
4687 spin_unlock_irq(&id_priv->lock);
4689 EXPORT_SYMBOL(rdma_leave_multicast);
4691 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4693 struct rdma_dev_addr *dev_addr;
4694 struct cma_work *work;
4696 dev_addr = &id_priv->id.route.addr.dev_addr;
4698 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
4699 (net_eq(dev_net(ndev), dev_addr->net)) &&
4700 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
4701 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4702 ndev->name, &id_priv->id);
4703 work = kzalloc(sizeof *work, GFP_KERNEL);
4707 INIT_WORK(&work->work, cma_work_handler);
4709 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4710 cma_id_get(id_priv);
4711 queue_work(cma_wq, &work->work);
4717 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
4720 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
4721 struct cma_device *cma_dev;
4722 struct rdma_id_private *id_priv;
4723 int ret = NOTIFY_DONE;
4725 if (event != NETDEV_BONDING_FAILOVER)
4728 if (!netif_is_bond_master(ndev))
4732 list_for_each_entry(cma_dev, &dev_list, list)
4733 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4734 ret = cma_netdev_change(ndev, id_priv);
4740 mutex_unlock(&lock);
4744 static struct notifier_block cma_nb = {
4745 .notifier_call = cma_netdev_callback
4748 static int cma_add_one(struct ib_device *device)
4750 struct cma_device *cma_dev;
4751 struct rdma_id_private *id_priv;
4753 unsigned long supported_gids = 0;
4756 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
4760 cma_dev->device = device;
4761 cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4762 sizeof(*cma_dev->default_gid_type),
4764 if (!cma_dev->default_gid_type) {
4769 cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt,
4770 sizeof(*cma_dev->default_roce_tos),
4772 if (!cma_dev->default_roce_tos) {
4777 rdma_for_each_port (device, i) {
4778 supported_gids = roce_gid_type_mask_support(device, i);
4779 WARN_ON(!supported_gids);
4780 if (supported_gids & (1 << CMA_PREFERRED_ROCE_GID_TYPE))
4781 cma_dev->default_gid_type[i - rdma_start_port(device)] =
4782 CMA_PREFERRED_ROCE_GID_TYPE;
4784 cma_dev->default_gid_type[i - rdma_start_port(device)] =
4785 find_first_bit(&supported_gids, BITS_PER_LONG);
4786 cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0;
4789 init_completion(&cma_dev->comp);
4790 refcount_set(&cma_dev->refcount, 1);
4791 INIT_LIST_HEAD(&cma_dev->id_list);
4792 ib_set_client_data(device, &cma_client, cma_dev);
4795 list_add_tail(&cma_dev->list, &dev_list);
4796 list_for_each_entry(id_priv, &listen_any_list, list)
4797 cma_listen_on_dev(id_priv, cma_dev);
4798 mutex_unlock(&lock);
4800 trace_cm_add_one(device);
4804 kfree(cma_dev->default_gid_type);
4811 static void cma_send_device_removal_put(struct rdma_id_private *id_priv)
4813 struct rdma_cm_event event = { .event = RDMA_CM_EVENT_DEVICE_REMOVAL };
4814 enum rdma_cm_state state;
4815 unsigned long flags;
4817 mutex_lock(&id_priv->handler_mutex);
4818 /* Record that we want to remove the device */
4819 spin_lock_irqsave(&id_priv->lock, flags);
4820 state = id_priv->state;
4821 if (state == RDMA_CM_DESTROYING || state == RDMA_CM_DEVICE_REMOVAL) {
4822 spin_unlock_irqrestore(&id_priv->lock, flags);
4823 mutex_unlock(&id_priv->handler_mutex);
4824 cma_id_put(id_priv);
4827 id_priv->state = RDMA_CM_DEVICE_REMOVAL;
4828 spin_unlock_irqrestore(&id_priv->lock, flags);
4830 if (cma_cm_event_handler(id_priv, &event)) {
4832 * At this point the ULP promises it won't call
4833 * rdma_destroy_id() concurrently
4835 cma_id_put(id_priv);
4836 mutex_unlock(&id_priv->handler_mutex);
4837 trace_cm_id_destroy(id_priv);
4838 _destroy_id(id_priv, state);
4841 mutex_unlock(&id_priv->handler_mutex);
4844 * If this races with destroy then the thread that first assigns state
4845 * to a destroying does the cancel.
4847 cma_cancel_operation(id_priv, state);
4848 cma_id_put(id_priv);
4851 static void cma_process_remove(struct cma_device *cma_dev)
4854 while (!list_empty(&cma_dev->id_list)) {
4855 struct rdma_id_private *id_priv = list_first_entry(
4856 &cma_dev->id_list, struct rdma_id_private, list);
4858 list_del(&id_priv->listen_list);
4859 list_del_init(&id_priv->list);
4860 cma_id_get(id_priv);
4861 mutex_unlock(&lock);
4863 cma_send_device_removal_put(id_priv);
4867 mutex_unlock(&lock);
4869 cma_dev_put(cma_dev);
4870 wait_for_completion(&cma_dev->comp);
4873 static void cma_remove_one(struct ib_device *device, void *client_data)
4875 struct cma_device *cma_dev = client_data;
4877 trace_cm_remove_one(device);
4880 list_del(&cma_dev->list);
4881 mutex_unlock(&lock);
4883 cma_process_remove(cma_dev);
4884 kfree(cma_dev->default_roce_tos);
4885 kfree(cma_dev->default_gid_type);
4889 static int cma_init_net(struct net *net)
4891 struct cma_pernet *pernet = cma_pernet(net);
4893 xa_init(&pernet->tcp_ps);
4894 xa_init(&pernet->udp_ps);
4895 xa_init(&pernet->ipoib_ps);
4896 xa_init(&pernet->ib_ps);
4901 static void cma_exit_net(struct net *net)
4903 struct cma_pernet *pernet = cma_pernet(net);
4905 WARN_ON(!xa_empty(&pernet->tcp_ps));
4906 WARN_ON(!xa_empty(&pernet->udp_ps));
4907 WARN_ON(!xa_empty(&pernet->ipoib_ps));
4908 WARN_ON(!xa_empty(&pernet->ib_ps));
4911 static struct pernet_operations cma_pernet_operations = {
4912 .init = cma_init_net,
4913 .exit = cma_exit_net,
4914 .id = &cma_pernet_id,
4915 .size = sizeof(struct cma_pernet),
4918 static int __init cma_init(void)
4923 * There is a rare lock ordering dependency in cma_netdev_callback()
4924 * that only happens when bonding is enabled. Teach lockdep that rtnl
4925 * must never be nested under lock so it can find these without having
4926 * to test with bonding.
4928 if (IS_ENABLED(CONFIG_LOCKDEP)) {
4931 mutex_unlock(&lock);
4935 cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
4939 ret = register_pernet_subsys(&cma_pernet_operations);
4943 ib_sa_register_client(&sa_client);
4944 register_netdevice_notifier(&cma_nb);
4946 ret = ib_register_client(&cma_client);
4950 ret = cma_configfs_init();
4957 ib_unregister_client(&cma_client);
4959 unregister_netdevice_notifier(&cma_nb);
4960 ib_sa_unregister_client(&sa_client);
4961 unregister_pernet_subsys(&cma_pernet_operations);
4963 destroy_workqueue(cma_wq);
4967 static void __exit cma_cleanup(void)
4969 cma_configfs_exit();
4970 ib_unregister_client(&cma_client);
4971 unregister_netdevice_notifier(&cma_nb);
4972 ib_sa_unregister_client(&sa_client);
4973 unregister_pernet_subsys(&cma_pernet_operations);
4974 destroy_workqueue(cma_wq);
4977 module_init(cma_init);
4978 module_exit(cma_cleanup);