[openib-general] [libibverbs/examples] pingpong tests cleanup (resource destroy + ack events + asprintf failure check)

Dotan Barak dotanb at dev.mellanox.co.il
Tue Jan 16 01:58:37 PST 2007


Added resource cleaning before end of test + ack to CQ events.
Added checks to memory allocation failure when using asprintf.

Signed-off-by: Dotan Barak <dotanb at mellanox.co.il>
---

Index: gen2_devel_user/src/userspace/libibverbs/examples/rc_pingpong.c
===================================================================
--- gen2_devel_user.orig/src/userspace/libibverbs/examples/rc_pingpong.c	2007-01-15 17:02:23.000000000 +0200
+++ gen2_devel_user/src/userspace/libibverbs/examples/rc_pingpong.c	2007-01-16 09:56:52.000000000 +0200
@@ -139,7 +139,9 @@ static struct pingpong_dest *pp_client_e
 	int sockfd = -1;
 	struct pingpong_dest *rem_dest = NULL;
 
-	asprintf(&service, "%d", port);
+	if (asprintf(&service, "%d", port) < 0)
+		return NULL;
+
 	n = getaddrinfo(servername, service, &hints, &res);
 
 	if (n < 0) {
@@ -205,7 +207,9 @@ static struct pingpong_dest *pp_server_e
 	int sockfd = -1, connfd;
 	struct pingpong_dest *rem_dest = NULL;
 
-	asprintf(&service, "%d", port);
+	if (asprintf(&service, "%d", port) < 0)
+		return NULL;
+
 	n = getaddrinfo(NULL, service, &hints, &res);
 
 	if (n < 0) {
@@ -322,7 +326,7 @@ static struct pingpong_context *pp_init_
 
 	ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size, IBV_ACCESS_LOCAL_WRITE);
 	if (!ctx->mr) {
-		fprintf(stderr, "Couldn't allocate MR\n");
+		fprintf(stderr, "Couldn't register MR\n");
 		return NULL;
 	}
 
@@ -374,6 +378,46 @@ static struct pingpong_context *pp_init_
 	return ctx;
 }
 
+int pp_close_ctx(struct pingpong_context *ctx)
+{
+	if (ibv_destroy_qp(ctx->qp)) {
+		fprintf(stderr, "Couldn't destroy QP\n");
+		return 1;
+	}
+
+	if (ibv_destroy_cq(ctx->cq)) {
+		fprintf(stderr, "Couldn't destroy CQ\n");
+		return 1;
+	}
+
+	if (ibv_dereg_mr(ctx->mr)) {
+		fprintf(stderr, "Couldn't deregister MR\n");
+		return 1;
+	}
+
+	if (ibv_dealloc_pd(ctx->pd)) {
+		fprintf(stderr, "Couldn't deallocate PD\n");
+		return 1;
+	}
+
+	if (ctx->channel) {
+		if (ibv_destroy_comp_channel(ctx->channel)) {
+			fprintf(stderr, "Couldn't destroy completion channel\n");
+			return 1;
+		}
+	}
+
+	if (ibv_close_device(ctx->context)) {
+		fprintf(stderr, "Couldn't release context\n");
+		return 1;
+	}
+
+	free(ctx->buf);
+	free(ctx);
+
+	return 0;
+}
+
 static int pp_post_recv(struct pingpong_context *ctx, int n)
 {
 	struct ibv_sge list = {
@@ -622,6 +666,8 @@ int main(int argc, char *argv[])
 				return 1;
 			}
 
+			ibv_ack_cq_events(ev_cq, 1);
+
 			if (ev_cq != ctx->cq) {
 				fprintf(stderr, "CQ event for unknown CQ %p\n", ev_cq);
 				return 1;
@@ -706,5 +752,11 @@ int main(int argc, char *argv[])
 		       iters, usec / 1000000., usec / iters);
 	}
 
+	if (pp_close_ctx(ctx))
+		return 1;
+
+	ibv_free_device_list(dev_list);
+	free(rem_dest);
+
 	return 0;
 }
Index: gen2_devel_user/src/userspace/libibverbs/examples/srq_pingpong.c
===================================================================
--- gen2_devel_user.orig/src/userspace/libibverbs/examples/srq_pingpong.c	2007-01-15 17:02:23.000000000 +0200
+++ gen2_devel_user/src/userspace/libibverbs/examples/srq_pingpong.c	2007-01-16 09:57:25.000000000 +0200
@@ -150,7 +150,9 @@ static struct pingpong_dest *pp_client_e
 	int sockfd = -1;
 	struct pingpong_dest *rem_dest = NULL;
 
-	asprintf(&service, "%d", port);
+	if (asprintf(&service, "%d", port) < 0)
+		return NULL;
+
 	n = getaddrinfo(servername, service, &hints, &res);
 
 	if (n < 0) {
@@ -229,7 +231,9 @@ static struct pingpong_dest *pp_server_e
 	int sockfd = -1, connfd;
 	struct pingpong_dest *rem_dest = NULL;
 
-	asprintf(&service, "%d", port);
+	if (asprintf(&service, "%d", port) < 0)
+		return NULL;
+
 	n = getaddrinfo(NULL, service, &hints, &res);
 
 	if (n < 0) {
@@ -358,7 +362,7 @@ static struct pingpong_context *pp_init_
 
 	ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size, IBV_ACCESS_LOCAL_WRITE);
 	if (!ctx->mr) {
-		fprintf(stderr, "Couldn't allocate MR\n");
+		fprintf(stderr, "Couldn't register MR\n");
 		return NULL;
 	}
 
@@ -424,6 +428,55 @@ static struct pingpong_context *pp_init_
 	return ctx;
 }
 
+int pp_close_ctx(struct pingpong_context *ctx, int num_qp)
+{
+	int i;
+
+	for (i = 0; i < num_qp; ++i) {
+		if (ibv_destroy_qp(ctx->qp[i])) {
+			fprintf(stderr, "Couldn't destroy QP[%d]\n", i);
+			return 1;
+		}
+	}
+
+	if (ibv_destroy_srq(ctx->srq)) {
+		fprintf(stderr, "Couldn't destroy SRQ\n");
+		return 1;
+	}
+
+	if (ibv_destroy_cq(ctx->cq)) {
+		fprintf(stderr, "Couldn't destroy CQ\n");
+		return 1;
+	}
+
+	if (ibv_dereg_mr(ctx->mr)) {
+		fprintf(stderr, "Couldn't deregister MR\n");
+		return 1;
+	}
+
+	if (ibv_dealloc_pd(ctx->pd)) {
+		fprintf(stderr, "Couldn't deallocate PD\n");
+		return 1;
+	}
+
+	if (ctx->channel) {
+		if (ibv_destroy_comp_channel(ctx->channel)) {
+			fprintf(stderr, "Couldn't destroy completion channel\n");
+			return 1;
+		}
+	}
+
+	if (ibv_close_device(ctx->context)) {
+		fprintf(stderr, "Couldn't release context\n");
+		return 1;
+	}
+
+	free(ctx->buf);
+	free(ctx);
+
+	return 0;
+}
+
 static int pp_post_recv(struct pingpong_context *ctx, int n)
 {
 	struct ibv_sge list = {
@@ -710,6 +763,8 @@ int main(int argc, char *argv[])
 				return 1;
 			}
 
+			ibv_ack_cq_events(ev_cq, 1);
+
 			if (ev_cq != ctx->cq) {
 				fprintf(stderr, "CQ event for unknown CQ %p\n", ev_cq);
 				return 1;
@@ -801,5 +856,11 @@ int main(int argc, char *argv[])
 		       iters, usec / 1000000., usec / iters);
 	}
 
+	if (pp_close_ctx(ctx, num_qp))
+		return 1;
+
+	ibv_free_device_list(dev_list);
+	free(rem_dest);
+
 	return 0;
 }
Index: gen2_devel_user/src/userspace/libibverbs/examples/uc_pingpong.c
===================================================================
--- gen2_devel_user.orig/src/userspace/libibverbs/examples/uc_pingpong.c	2007-01-15 17:02:23.000000000 +0200
+++ gen2_devel_user/src/userspace/libibverbs/examples/uc_pingpong.c	2007-01-16 09:57:54.000000000 +0200
@@ -127,7 +127,9 @@ static struct pingpong_dest *pp_client_e
 	int sockfd = -1;
 	struct pingpong_dest *rem_dest = NULL;
 
-	asprintf(&service, "%d", port);
+	if (asprintf(&service, "%d", port) < 0)
+		return NULL;
+
 	n = getaddrinfo(servername, service, &hints, &res);
 
 	if (n < 0) {
@@ -193,7 +195,9 @@ static struct pingpong_dest *pp_server_e
 	int sockfd = -1, connfd;
 	struct pingpong_dest *rem_dest = NULL;
 
-	asprintf(&service, "%d", port);
+	if (asprintf(&service, "%d", port) < 0)
+		return NULL;
+
 	n = getaddrinfo(NULL, service, &hints, &res);
 
 	if (n < 0) {
@@ -310,7 +314,7 @@ static struct pingpong_context *pp_init_
 
 	ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size, IBV_ACCESS_LOCAL_WRITE);
 	if (!ctx->mr) {
-		fprintf(stderr, "Couldn't allocate MR\n");
+		fprintf(stderr, "Couldn't register MR\n");
 		return NULL;
 	}
 
@@ -362,6 +366,46 @@ static struct pingpong_context *pp_init_
 	return ctx;
 }
 
+int pp_close_ctx(struct pingpong_context *ctx)
+{
+	if (ibv_destroy_qp(ctx->qp)) {
+		fprintf(stderr, "Couldn't destroy QP\n");
+		return 1;
+	}
+
+	if (ibv_destroy_cq(ctx->cq)) {
+		fprintf(stderr, "Couldn't destroy CQ\n");
+		return 1;
+	}
+
+	if (ibv_dereg_mr(ctx->mr)) {
+		fprintf(stderr, "Couldn't deregister MR\n");
+		return 1;
+	}
+
+	if (ibv_dealloc_pd(ctx->pd)) {
+		fprintf(stderr, "Couldn't deallocate PD\n");
+		return 1;
+	}
+
+	if (ctx->channel) {
+		if (ibv_destroy_comp_channel(ctx->channel)) {
+			fprintf(stderr, "Couldn't destroy completion channel\n");
+			return 1;
+		}
+	}
+
+	if (ibv_close_device(ctx->context)) {
+		fprintf(stderr, "Couldn't release context\n");
+		return 1;
+	}
+
+	free(ctx->buf);
+	free(ctx);
+
+	return 0;
+}
+
 static int pp_post_recv(struct pingpong_context *ctx, int n)
 {
 	struct ibv_sge list = {
@@ -610,6 +654,8 @@ int main(int argc, char *argv[])
 				return 1;
 			}
 
+			ibv_ack_cq_events(ev_cq, 1);
+
 			if (ev_cq != ctx->cq) {
 				fprintf(stderr, "CQ event for unknown CQ %p\n", ev_cq);
 				return 1;
@@ -694,5 +740,11 @@ int main(int argc, char *argv[])
 		       iters, usec / 1000000., usec / iters);
 	}
 
+	if (pp_close_ctx(ctx))
+		return 1;
+
+	ibv_free_device_list(dev_list);
+	free(rem_dest);
+
 	return 0;
 }
Index: gen2_devel_user/src/userspace/libibverbs/examples/ud_pingpong.c
===================================================================
--- gen2_devel_user.orig/src/userspace/libibverbs/examples/ud_pingpong.c	2007-01-15 17:02:23.000000000 +0200
+++ gen2_devel_user/src/userspace/libibverbs/examples/ud_pingpong.c	2007-01-16 09:58:22.000000000 +0200
@@ -128,7 +128,9 @@ static struct pingpong_dest *pp_client_e
 	int sockfd = -1;
 	struct pingpong_dest *rem_dest = NULL;
 
-	asprintf(&service, "%d", port);
+	if (asprintf(&service, "%d", port) < 0)
+		return NULL;
+
 	n = getaddrinfo(servername, service, &hints, &res);
 
 	if (n < 0) {
@@ -194,7 +196,9 @@ static struct pingpong_dest *pp_server_e
 	int sockfd = -1, connfd;
 	struct pingpong_dest *rem_dest = NULL;
 
-	asprintf(&service, "%d", port);
+	if (asprintf(&service, "%d", port) < 0)
+		return NULL;
+
 	n = getaddrinfo(NULL, service, &hints, &res);
 
 	if (n < 0) {
@@ -311,7 +315,7 @@ static struct pingpong_context *pp_init_
 
 	ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size + 40, IBV_ACCESS_LOCAL_WRITE);
 	if (!ctx->mr) {
-		fprintf(stderr, "Couldn't allocate MR\n");
+		fprintf(stderr, "Couldn't register MR\n");
 		return NULL;
 	}
 
@@ -363,6 +367,51 @@ static struct pingpong_context *pp_init_
 	return ctx;
 }
 
+int pp_close_ctx(struct pingpong_context *ctx)
+{
+	if (ibv_destroy_qp(ctx->qp)) {
+		fprintf(stderr, "Couldn't destroy QP\n");
+		return 1;
+	}
+
+	if (ibv_destroy_cq(ctx->cq)) {
+		fprintf(stderr, "Couldn't destroy CQ\n");
+		return 1;
+	}
+
+	if (ibv_dereg_mr(ctx->mr)) {
+		fprintf(stderr, "Couldn't deregister MR\n");
+		return 1;
+	}
+
+	if (ibv_destroy_ah(ctx->ah)) {
+		fprintf(stderr, "Couldn't destroy AH\n");
+		return 1;
+	}
+
+	if (ibv_dealloc_pd(ctx->pd)) {
+		fprintf(stderr, "Couldn't deallocate PD\n");
+		return 1;
+	}
+
+	if (ctx->channel) {
+		if (ibv_destroy_comp_channel(ctx->channel)) {
+			fprintf(stderr, "Couldn't destroy completion channel\n");
+			return 1;
+		}
+	}
+
+	if (ibv_close_device(ctx->context)) {
+		fprintf(stderr, "Couldn't release context\n");
+		return 1;
+	}
+
+	free(ctx->buf);
+	free(ctx);
+
+	return 0;
+}
+
 static int pp_post_recv(struct pingpong_context *ctx, int n)
 {
 	struct ibv_sge list = {
@@ -608,6 +657,8 @@ int main(int argc, char *argv[])
 				return 1;
 			}
 
+			ibv_ack_cq_events(ev_cq, 1);
+
 			if (ev_cq != ctx->cq) {
 				fprintf(stderr, "CQ event for unknown CQ %p\n", ev_cq);
 				return 1;
@@ -692,5 +743,11 @@ int main(int argc, char *argv[])
 		       iters, usec / 1000000., usec / iters);
 	}
 
+	if (pp_close_ctx(ctx))
+		return 1;
+
+	ibv_free_device_list(dev_list);
+	free(rem_dest);
+
 	return 0;
 }






More information about the general mailing list