头文件和库以及全局变量
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <netinet/tcp.h>
#include <rdma/rdma_cma.h>
#include <rdma/rsocket.h>
#include <util/compiler.h>
#include "common.h"
struct test_size_param {
int size;
int option;
};
static struct test_size_param test_size[] = {
{ 1 << 6, 0 },
{ 1 << 7, 1 }, { (1 << 7) + (1 << 6), 1},
{ 1 << 8, 1 }, { (1 << 8) + (1 << 7), 1},
{ 1 << 9, 1 }, { (1 << 9) + (1 << 8), 1},
{ 1 << 10, 1 }, { (1 << 10) + (1 << 9), 1},
{ 1 << 11, 1 }, { (1 << 11) + (1 << 10), 1},
{ 1 << 12, 0 }, { (1 << 12) + (1 << 11), 1},
{ 1 << 13, 1 }, { (1 << 13) + (1 << 12), 1},
{ 1 << 14, 1 }, { (1 << 14) + (1 << 13), 1},
{ 1 << 15, 1 }, { (1 << 15) + (1 << 14), 1},
{ 1 << 16, 0 }, { (1 << 16) + (1 << 15), 1},
{ 1 << 17, 1 }, { (1 << 17) + (1 << 16), 1},
{ 1 << 18, 1 }, { (1 << 18) + (1 << 17), 1},
{ 1 << 19, 1 }, { (1 << 19) + (1 << 18), 1},
{ 1 << 20, 0 }, { (1 << 20) + (1 << 19), 1},
{ 1 << 21, 1 }, { (1 << 21) + (1 << 20), 1},
{ 1 << 22, 1 }, { (1 << 22) + (1 << 21), 1},
};
#define TEST_CNT (sizeof test_size / sizeof test_size[0])
// 都是静态变量,都在本文件用于测试
static int rs, lrs;
static int use_async;
static int use_rgai;
static int verify;
static int flags = MSG_DONTWAIT;
static int poll_timeout = 0;
static int custom;
static enum rs_optimization optimization;
static int size_option;
static int iterations = 1;
static int transfer_size = 1000;
static int transfer_count = 1000;
static int buffer_size, inline_size = 64;
static char test_name[10] = "custom";
static const char *port = "7471";
static char *dst_addr;
static char *src_addr;
static struct timeval start, end;
static void *buf;
static volatile uint8_t *poll_byte;
static struct rdma_addrinfo rai_hints;
static struct addrinfo ai_hints;
main函数:
主要工作是解析参数,判断参数的输入是否合法,然后调用run函数
int main(int argc, char **argv)
{
int op, ret;
ai_hints.ai_socktype = SOCK_STREAM;
rai_hints.ai_port_space = RDMA_PS_TCP;
while ((op = getopt(argc, argv, "s:b:f:B:i:I:C:S:p:T:")) != -1) {
switch (op) {
case 's':
dst_addr = optarg;
break;
case 'b':
src_addr = optarg;
break;
case 'f':
if (!strncasecmp("ip", optarg, 2)) {
ai_hints.ai_flags = AI_NUMERICHOST;
} else if (!strncasecmp("gid", optarg, 3)) {
rai_hints.ai_flags = RAI_NUMERICHOST | RAI_FAMILY;
rai_hints.ai_family = AF_IB;
use_rgai = 1;
} else {
fprintf(stderr, "Warning: unknown address format\n");
}
break;
case 'B':
buffer_size = atoi(optarg);
break;
case 'i':
inline_size = atoi(optarg);
break;
case 'I':
custom = 1;
iterations = atoi(optarg);
break;
case 'C':
custom = 1;
transfer_count = atoi(optarg);
break;
case 'S':
if (!strncasecmp("all", optarg, 3)) {
size_option = 1;
} else {
custom = 1;
transfer_size = atoi(optarg);
}
break;
case 'p':
port = optarg;
break;
case 'T':
if (!set_test_opt(optarg))
break;
/* invalid option - fall through */
SWITCH_FALLTHROUGH;
default:
printf("usage: %s\n", argv[0]);
printf("\t[-s server_address]\n");
printf("\t[-b bind_address]\n");
printf("\t[-f address_format]\n");
printf("\t name, ip, ipv6, or gid\n");
printf("\t[-B buffer_size]\n");
printf("\t[-i inline_size]\n");
printf("\t[-I iterations]\n");
printf("\t[-C transfer_count]\n");
printf("\t[-S transfer_size or all]\n");
printf("\t[-p port_number]\n");
printf("\t[-T test_option]\n");
printf("\t a|async - asynchronous operation (use poll)\n");
printf("\t b|blocking - use blocking calls\n");
printf("\t n|nonblocking - use nonblocking calls\n");
printf("\t v|verify - verify data\n");
exit(1);
}
}
if (!(flags & MSG_DONTWAIT))
poll_timeout = -1;
ret = run();
return ret;
}
run函数:
static int run(void)
{
int i, ret = 0;
// 申请buf的长度信息
buf = malloc(!custom ? test_size[TEST_CNT - 1].size : transfer_size);
if (!buf) {
perror("malloc");
return -1;
}
if (!dst_addr) {
ret = server_listen(); // 如果没有目标地址,默认自己就是服务器,服务器就得开启侦听模式,里面创建对应的信息。(建立连接、绑定信息)
if (ret)
goto free;
}
printf("%-10s%-8s%-8s%-8s%-8s%8s %10s%13s\n",
"name", "bytes", "xfers", "iters", "total", "time", "Gb/sec", "usec/xfer");
if (!custom) {
optimization = opt_latency;
ret = dst_addr ? client_connect() : server_connect(); // 根据dst_addr存在与否,启动client_connect和server_connect连接
if (ret)
goto free;
for (i = 0; i < TEST_CNT; i++) {
if (test_size[i].option > size_option)
continue;
init_latency_test(test_size[i].size); // 测试延时
run_test();
}
rshutdown(rs, SHUT_RDWR);
rclose(rs);
optimization = opt_bandwidth;
ret = dst_addr ? client_connect() : server_connect();
if (ret)
goto free;
for (i = 0; i < TEST_CNT; i++) {
if (test_size[i].option > size_option)
continue;
init_bandwidth_test(test_size[i].size);
run_test();
}
} else {
ret = dst_addr ? client_connect() : server_connect();
if (ret)
goto free;
ret = run_test();
}
rshutdown(rs, SHUT_RDWR);
rclose(rs);
free:
free(buf);
return ret;
}