From 080f7181a2b4d2316d03c702f65640d6e8b1031e Mon Sep 17 00:00:00 2001
From: Kamal Heib <kamalheib1@gmail.com>
Date: Mon, 19 Sep 2022 11:39:15 -0400
Subject: mckey: Use rdma_create_qp_ex only for loopback prevention

[ Upstream commit 926a1158e33d78573859f5dfea399f7a7edcf11f ]

As not all the providers support the rdma_create_qp_ex(), change the
code to use rdma_create_qp_ex() only when loopback prevention is requested.

Fixes: 40806cc22936 ("rdma-core: Add support for multicast loopback prevention to mckey")
Signed-off-by: Kamal Heib <kamalheib1@gmail.com>
Signed-off-by: Nicolas Morey-Chaisemartin <nmoreychaisemartin@suse.com>
---
 librdmacm/examples/mckey.c | 48 +++++++++++++++++++++++++-------------
 1 file changed, 32 insertions(+), 16 deletions(-)

diff --git a/librdmacm/examples/mckey.c b/librdmacm/examples/mckey.c
index 7015ed2..6df53a2 100644
--- a/librdmacm/examples/mckey.c
+++ b/librdmacm/examples/mckey.c
@@ -134,7 +134,8 @@ static int verify_test_params(struct cmatest_node *node)
 static int init_node(struct cmatest_node *node)
 {
 	struct ibv_qp_init_attr_ex init_qp_attr_ex;
-	int cqe, ret;
+	struct ibv_qp_init_attr init_qp_attr;
+	int cqe, ret = 0;
 
 	node->pd = ibv_alloc_pd(node->cma_id->verbs);
 	if (!node->pd) {
@@ -151,23 +152,38 @@ static int init_node(struct cmatest_node *node)
 		goto out;
 	}
 
-	memset(&init_qp_attr_ex, 0, sizeof(init_qp_attr_ex));
-	init_qp_attr_ex.cap.max_send_wr = message_count ? message_count : 1;
-	init_qp_attr_ex.cap.max_recv_wr = message_count ? message_count : 1;
-	init_qp_attr_ex.cap.max_send_sge = 1;
-	init_qp_attr_ex.cap.max_recv_sge = 1;
-	init_qp_attr_ex.qp_context = node;
-	init_qp_attr_ex.sq_sig_all = 0;
-	init_qp_attr_ex.qp_type = IBV_QPT_UD;
-	init_qp_attr_ex.send_cq = node->cq;
-	init_qp_attr_ex.recv_cq = node->cq;
-
-	init_qp_attr_ex.comp_mask = IBV_QP_INIT_ATTR_CREATE_FLAGS|IBV_QP_INIT_ATTR_PD;
-	init_qp_attr_ex.pd = node->pd;
-	if (!loopback)
+	memset(&init_qp_attr, 0, sizeof init_qp_attr);
+	init_qp_attr.cap.max_send_wr = message_count ? message_count : 1;
+	init_qp_attr.cap.max_recv_wr = message_count ? message_count : 1;
+	init_qp_attr.cap.max_send_sge = 1;
+	init_qp_attr.cap.max_recv_sge = 1;
+	init_qp_attr.qp_context = node;
+	init_qp_attr.sq_sig_all = 0;
+	init_qp_attr.qp_type = IBV_QPT_UD;
+	init_qp_attr.send_cq = node->cq;
+	init_qp_attr.recv_cq = node->cq;
+
+	if (!loopback) {
+		memset(&init_qp_attr_ex, 0, sizeof(init_qp_attr_ex));
+		init_qp_attr_ex.cap.max_send_wr = message_count ? message_count : 1;
+		init_qp_attr_ex.cap.max_recv_wr = message_count ? message_count : 1;
+		init_qp_attr_ex.cap.max_send_sge = 1;
+		init_qp_attr_ex.cap.max_recv_sge = 1;
+		init_qp_attr_ex.qp_context = node;
+		init_qp_attr_ex.sq_sig_all = 0;
+		init_qp_attr_ex.qp_type = IBV_QPT_UD;
+		init_qp_attr_ex.send_cq = node->cq;
+		init_qp_attr_ex.recv_cq = node->cq;
+
+		init_qp_attr_ex.comp_mask = IBV_QP_INIT_ATTR_CREATE_FLAGS|IBV_QP_INIT_ATTR_PD;
+		init_qp_attr_ex.pd = node->pd;
 		init_qp_attr_ex.create_flags = IBV_QP_CREATE_BLOCK_SELF_MCAST_LB;
 
-	ret = rdma_create_qp_ex(node->cma_id, &init_qp_attr_ex);
+		ret = rdma_create_qp_ex(node->cma_id, &init_qp_attr_ex);
+	} else {
+		ret = rdma_create_qp(node->cma_id, node->pd, &init_qp_attr);
+	}
+
 	if (ret) {
 		perror("mckey: unable to create QP");
 		goto out;
-- 
2.34.1

