2 * Copyright (c) 2013 Intel Corporation. All rights reserved.
4 * This software is available to you under the OpenIB.org BSD license
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * - Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AWV
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <sys/types.h>
37 #include <sys/socket.h>
43 #include <netinet/tcp.h>
45 #include <rdma/rdma_cma.h>
48 static struct rdma_addrinfo hints, *rai;
49 static struct rdma_event_channel *channel;
50 static const char *port = "7471";
51 static char *dst_addr;
52 static char *src_addr;
53 static int timeout = 2000;
54 static int retries = 2;
68 static const char *step_str[] = {
80 struct rdma_cm_id *id;
81 struct timeval times[STEP_CNT][2];
87 struct list_head *prev;
88 struct list_head *next;
89 struct rdma_cm_id *id;
95 struct list_head list;
98 #define INIT_LIST(x) ((x)->prev = (x)->next = (x))
100 static struct work_list req_work;
101 static struct work_list disc_work;
102 static struct node *nodes;
103 static struct timeval times[STEP_CNT][2];
104 static int connections = 100;
105 static volatile int started[STEP_CNT];
106 static volatile int completed[STEP_CNT];
107 static struct ibv_qp_init_attr init_qp_attr;
108 static struct rdma_conn_param conn_param;
110 #define start_perf(n, s) gettimeofday(&((n)->times[s][0]), NULL)
111 #define end_perf(n, s) gettimeofday(&((n)->times[s][1]), NULL)
112 #define start_time(s) gettimeofday(×[s][0], NULL)
113 #define end_time(s) gettimeofday(×[s][1], NULL)
115 static inline void __list_delete(struct list_head *list)
117 struct list_head *prev, *next;
125 static inline int __list_empty(struct work_list *list)
127 return list->list.next == &list->list;
130 static inline struct list_head *__list_remove_head(struct work_list *work_list)
132 struct list_head *list_item;
134 list_item = work_list->list.next;
135 __list_delete(list_item);
139 static inline void list_add_tail(struct work_list *work_list, struct list_head *req)
142 pthread_mutex_lock(&work_list->lock);
143 empty = __list_empty(work_list);
144 req->prev = work_list->list.prev;
145 req->next = &work_list->list;
146 req->prev->next = work_list->list.prev = req;
147 pthread_mutex_unlock(&work_list->lock);
149 pthread_cond_signal(&work_list->cond);
152 static int zero_time(struct timeval *t)
154 return !(t->tv_sec || t->tv_usec);
157 static float diff_us(struct timeval *end, struct timeval *start)
159 return (end->tv_sec - start->tv_sec) * 1000000. + (end->tv_usec - start->tv_usec);
162 static void show_perf(void)
165 float us, max[STEP_CNT], min[STEP_CNT];
167 for (i = 0; i < STEP_CNT; i++) {
170 for (c = 0; c < connections; c++) {
171 if (!zero_time(&nodes[c].times[i][0]) &&
172 !zero_time(&nodes[c].times[i][1])) {
173 us = diff_us(&nodes[c].times[i][1], &nodes[c].times[i][0]);
182 printf("step total ms max ms min us us / conn\n");
183 for (i = 0; i < STEP_CNT; i++) {
184 if (i == STEP_BIND && !src_addr)
187 us = diff_us(×[i][1], ×[i][0]);
188 printf("%-13s: %11.2f%11.2f%11.2f%11.2f\n", step_str[i], us / 1000.,
189 max[i] / 1000., min[i], us / connections);
193 static void addr_handler(struct node *n)
195 end_perf(n, STEP_RESOLVE_ADDR);
196 completed[STEP_RESOLVE_ADDR]++;
199 static void route_handler(struct node *n)
201 end_perf(n, STEP_RESOLVE_ROUTE);
202 completed[STEP_RESOLVE_ROUTE]++;
205 static void conn_handler(struct node *n)
207 end_perf(n, STEP_CONNECT);
208 completed[STEP_CONNECT]++;
211 static void disc_handler(struct node *n)
213 end_perf(n, STEP_DISCONNECT);
214 completed[STEP_DISCONNECT]++;
217 static void __req_handler(struct rdma_cm_id *id)
221 ret = rdma_create_qp(id, NULL, &init_qp_attr);
223 perror("failure creating qp");
227 ret = rdma_accept(id, NULL);
229 perror("failure accepting");
235 printf("failing connection request\n");
236 rdma_reject(id, NULL, 0);
241 static void *req_handler_thread(void *arg)
243 struct list_head *work;
245 pthread_mutex_lock(&req_work.lock);
246 if (__list_empty(&req_work))
247 pthread_cond_wait(&req_work.cond, &req_work.lock);
248 work = __list_remove_head(&req_work);
249 pthread_mutex_unlock(&req_work.lock);
250 __req_handler(work->id);
256 static void *disc_handler_thread(void *arg)
258 struct list_head *work;
260 pthread_mutex_lock(&disc_work.lock);
261 if (__list_empty(&disc_work))
262 pthread_cond_wait(&disc_work.cond, &disc_work.lock);
263 work = __list_remove_head(&disc_work);
264 pthread_mutex_unlock(&disc_work.lock);
265 rdma_disconnect(work->id);
266 rdma_destroy_id(work->id);
272 static void cma_handler(struct rdma_cm_id *id, struct rdma_cm_event *event)
274 struct node *n = id->context;
275 struct list_head *request;
277 switch (event->event) {
278 case RDMA_CM_EVENT_ADDR_RESOLVED:
281 case RDMA_CM_EVENT_ROUTE_RESOLVED:
284 case RDMA_CM_EVENT_CONNECT_REQUEST:
285 request = malloc(sizeof *request);
287 perror("out of memory accepting connect request");
288 rdma_reject(id, NULL, 0);
293 list_add_tail(&req_work, request);
296 case RDMA_CM_EVENT_ESTABLISHED:
300 case RDMA_CM_EVENT_ADDR_ERROR:
302 if (!rdma_resolve_addr(n->id, rai->ai_src_addr,
303 rai->ai_dst_addr, timeout))
306 printf("RDMA_CM_EVENT_ADDR_ERROR, error: %d\n", event->status);
310 case RDMA_CM_EVENT_ROUTE_ERROR:
312 if (!rdma_resolve_route(n->id, timeout))
315 printf("RDMA_CM_EVENT_ROUTE_ERROR, error: %d\n", event->status);
319 case RDMA_CM_EVENT_CONNECT_ERROR:
320 case RDMA_CM_EVENT_UNREACHABLE:
321 case RDMA_CM_EVENT_REJECTED:
322 printf("event: %s, error: %d\n",
323 rdma_event_str(event->event), event->status);
327 case RDMA_CM_EVENT_DISCONNECTED:
329 request = malloc(sizeof *request);
331 perror("out of memory queueing disconnect request, handling synchronously");
337 list_add_tail(&disc_work, request);
342 case RDMA_CM_EVENT_DEVICE_REMOVAL:
343 /* Cleanup will occur after test completes. */
348 rdma_ack_cm_event(event);
351 static int alloc_nodes(void)
355 nodes = calloc(sizeof *nodes, connections);
359 printf("creating id\n");
360 start_time(STEP_CREATE_ID);
361 for (i = 0; i < connections; i++) {
362 start_perf(&nodes[i], STEP_CREATE_ID);
364 ret = rdma_create_id(channel, &nodes[i].id, &nodes[i],
365 hints.ai_port_space);
369 end_perf(&nodes[i], STEP_CREATE_ID);
371 end_time(STEP_CREATE_ID);
376 rdma_destroy_id(nodes[i].id);
381 static void cleanup_nodes(void)
385 printf("destroying id\n");
386 start_time(STEP_DESTROY);
387 for (i = 0; i < connections; i++) {
388 start_perf(&nodes[i], STEP_DESTROY);
390 rdma_destroy_id(nodes[i].id);
391 end_perf(&nodes[i], STEP_DESTROY);
393 end_time(STEP_DESTROY);
396 static void *process_events(void *arg)
398 struct rdma_cm_event *event;
402 ret = rdma_get_cm_event(channel, &event);
404 cma_handler(event->id, event);
406 perror("failure in rdma_get_cm_event in process_server_events");
413 static int run_server(void)
415 pthread_t req_thread, disc_thread;
416 struct rdma_cm_id *listen_id;
419 INIT_LIST(&req_work.list);
420 INIT_LIST(&disc_work.list);
421 ret = pthread_mutex_init(&req_work.lock, NULL);
423 perror("initializing mutex for req work");
427 ret = pthread_mutex_init(&disc_work.lock, NULL);
429 perror("initializing mutex for disc work");
433 ret = pthread_cond_init(&req_work.cond, NULL);
435 perror("initializing cond for req work");
439 ret = pthread_cond_init(&disc_work.cond, NULL);
441 perror("initializing cond for disc work");
445 ret = pthread_create(&req_thread, NULL, req_handler_thread, NULL);
447 perror("failed to create req handler thread");
451 ret = pthread_create(&disc_thread, NULL, disc_handler_thread, NULL);
453 perror("failed to create disconnect handler thread");
457 ret = rdma_create_id(channel, &listen_id, NULL, hints.ai_port_space);
459 perror("listen request failed");
463 ret = get_rdma_addr(src_addr, dst_addr, port, &hints, &rai);
465 printf("getrdmaaddr error: %s\n", gai_strerror(ret));
469 ret = rdma_bind_addr(listen_id, rai->ai_src_addr);
471 perror("bind address failed");
475 ret = rdma_listen(listen_id, 0);
477 perror("failure trying to listen");
481 process_events(NULL);
483 rdma_destroy_id(listen_id);
487 static int run_client(void)
489 pthread_t event_thread;
492 ret = get_rdma_addr(src_addr, dst_addr, port, &hints, &rai);
494 printf("getaddrinfo error: %s\n", gai_strerror(ret));
498 conn_param.responder_resources = 1;
499 conn_param.initiator_depth = 1;
500 conn_param.retry_count = retries;
501 conn_param.private_data = rai->ai_connect;
502 conn_param.private_data_len = rai->ai_connect_len;
504 ret = pthread_create(&event_thread, NULL, process_events, NULL);
506 perror("failure creating event thread");
511 printf("binding source address\n");
512 start_time(STEP_BIND);
513 for (i = 0; i < connections; i++) {
514 start_perf(&nodes[i], STEP_BIND);
515 ret = rdma_bind_addr(nodes[i].id, rai->ai_src_addr);
517 perror("failure bind addr");
521 end_perf(&nodes[i], STEP_BIND);
526 printf("resolving address\n");
527 start_time(STEP_RESOLVE_ADDR);
528 for (i = 0; i < connections; i++) {
531 nodes[i].retries = retries;
532 start_perf(&nodes[i], STEP_RESOLVE_ADDR);
533 ret = rdma_resolve_addr(nodes[i].id, rai->ai_src_addr,
534 rai->ai_dst_addr, timeout);
536 perror("failure getting addr");
540 started[STEP_RESOLVE_ADDR]++;
542 while (started[STEP_RESOLVE_ADDR] != completed[STEP_RESOLVE_ADDR]) sched_yield();
543 end_time(STEP_RESOLVE_ADDR);
545 printf("resolving route\n");
546 start_time(STEP_RESOLVE_ROUTE);
547 for (i = 0; i < connections; i++) {
550 nodes[i].retries = retries;
551 start_perf(&nodes[i], STEP_RESOLVE_ROUTE);
552 ret = rdma_resolve_route(nodes[i].id, timeout);
554 perror("failure resolving route");
558 started[STEP_RESOLVE_ROUTE]++;
560 while (started[STEP_RESOLVE_ROUTE] != completed[STEP_RESOLVE_ROUTE]) sched_yield();
561 end_time(STEP_RESOLVE_ROUTE);
563 printf("creating qp\n");
564 start_time(STEP_CREATE_QP);
565 for (i = 0; i < connections; i++) {
568 start_perf(&nodes[i], STEP_CREATE_QP);
569 ret = rdma_create_qp(nodes[i].id, NULL, &init_qp_attr);
571 perror("failure creating qp");
575 end_perf(&nodes[i], STEP_CREATE_QP);
577 end_time(STEP_CREATE_QP);
579 printf("connecting\n");
580 start_time(STEP_CONNECT);
581 for (i = 0; i < connections; i++) {
584 start_perf(&nodes[i], STEP_CONNECT);
585 ret = rdma_connect(nodes[i].id, &conn_param);
587 perror("failure rconnecting");
591 started[STEP_CONNECT]++;
593 while (started[STEP_CONNECT] != completed[STEP_CONNECT]) sched_yield();
594 end_time(STEP_CONNECT);
596 printf("disconnecting\n");
597 start_time(STEP_DISCONNECT);
598 for (i = 0; i < connections; i++) {
601 start_perf(&nodes[i], STEP_DISCONNECT);
602 rdma_disconnect(nodes[i].id);
603 started[STEP_DISCONNECT]++;
605 while (started[STEP_DISCONNECT] != completed[STEP_DISCONNECT]) sched_yield();
606 end_time(STEP_DISCONNECT);
611 int main(int argc, char **argv)
615 hints.ai_port_space = RDMA_PS_TCP;
616 hints.ai_qp_type = IBV_QPT_RC;
617 while ((op = getopt(argc, argv, "s:b:c:p:r:t:")) != -1) {
626 connections = atoi(optarg);
632 retries = atoi(optarg);
635 timeout = atoi(optarg);
638 printf("usage: %s\n", argv[0]);
639 printf("\t[-s server_address]\n");
640 printf("\t[-b bind_address]\n");
641 printf("\t[-c connections]\n");
642 printf("\t[-p port_number]\n");
643 printf("\t[-r retries]\n");
644 printf("\t[-t timeout_ms]\n");
649 init_qp_attr.cap.max_send_wr = 1;
650 init_qp_attr.cap.max_recv_wr = 1;
651 init_qp_attr.cap.max_send_sge = 1;
652 init_qp_attr.cap.max_recv_sge = 1;
653 init_qp_attr.qp_type = IBV_QPT_RC;
655 channel = rdma_create_event_channel();
657 printf("failed to create event channel\n");
665 hints.ai_flags |= RAI_PASSIVE;
670 rdma_destroy_event_channel(channel);
672 rdma_freeaddrinfo(rai);