1*d6b92ffaSHans Petter Selasky /*
2*d6b92ffaSHans Petter Selasky * Copyright (c) 2005-2006,2012 Intel Corporation. All rights reserved.
3*d6b92ffaSHans Petter Selasky *
4*d6b92ffaSHans Petter Selasky * This software is available to you under a choice of one of two
5*d6b92ffaSHans Petter Selasky * licenses. You may choose to be licensed under the terms of the GNU
6*d6b92ffaSHans Petter Selasky * General Public License (GPL) Version 2, available from the file
7*d6b92ffaSHans Petter Selasky * COPYING in the main directory of this source tree, or the
8*d6b92ffaSHans Petter Selasky * OpenIB.org BSD license below:
9*d6b92ffaSHans Petter Selasky *
10*d6b92ffaSHans Petter Selasky * Redistribution and use in source and binary forms, with or
11*d6b92ffaSHans Petter Selasky * without modification, are permitted provided that the following
12*d6b92ffaSHans Petter Selasky * conditions are met:
13*d6b92ffaSHans Petter Selasky *
14*d6b92ffaSHans Petter Selasky * - Redistributions of source code must retain the above
15*d6b92ffaSHans Petter Selasky * copyright notice, this list of conditions and the following
16*d6b92ffaSHans Petter Selasky * disclaimer.
17*d6b92ffaSHans Petter Selasky *
18*d6b92ffaSHans Petter Selasky * - Redistributions in binary form must reproduce the above
19*d6b92ffaSHans Petter Selasky * copyright notice, this list of conditions and the following
20*d6b92ffaSHans Petter Selasky * disclaimer in the documentation and/or other materials
21*d6b92ffaSHans Petter Selasky * provided with the distribution.
22*d6b92ffaSHans Petter Selasky *
23*d6b92ffaSHans Petter Selasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*d6b92ffaSHans Petter Selasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*d6b92ffaSHans Petter Selasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*d6b92ffaSHans Petter Selasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*d6b92ffaSHans Petter Selasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*d6b92ffaSHans Petter Selasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*d6b92ffaSHans Petter Selasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*d6b92ffaSHans Petter Selasky * SOFTWARE.
31*d6b92ffaSHans Petter Selasky *
32*d6b92ffaSHans Petter Selasky * $Id$
33*d6b92ffaSHans Petter Selasky */
34*d6b92ffaSHans Petter Selasky
35*d6b92ffaSHans Petter Selasky #include <stdlib.h>
36*d6b92ffaSHans Petter Selasky #include <string.h>
37*d6b92ffaSHans Petter Selasky #include <stdio.h>
38*d6b92ffaSHans Petter Selasky #include <errno.h>
39*d6b92ffaSHans Petter Selasky #include <sys/types.h>
40*d6b92ffaSHans Petter Selasky #include <sys/socket.h>
41*d6b92ffaSHans Petter Selasky #include <netdb.h>
42*d6b92ffaSHans Petter Selasky
43*d6b92ffaSHans Petter Selasky #include <rdma/rdma_cma.h>
44*d6b92ffaSHans Petter Selasky #include "common.h"
45*d6b92ffaSHans Petter Selasky
46*d6b92ffaSHans Petter Selasky int use_rs = 1;
47*d6b92ffaSHans Petter Selasky
get_rdma_addr(const char * src,const char * dst,const char * port,struct rdma_addrinfo * hints,struct rdma_addrinfo ** rai)48*d6b92ffaSHans Petter Selasky int get_rdma_addr(const char *src, const char *dst, const char *port,
49*d6b92ffaSHans Petter Selasky struct rdma_addrinfo *hints, struct rdma_addrinfo **rai)
50*d6b92ffaSHans Petter Selasky {
51*d6b92ffaSHans Petter Selasky struct rdma_addrinfo rai_hints, *res;
52*d6b92ffaSHans Petter Selasky int ret;
53*d6b92ffaSHans Petter Selasky
54*d6b92ffaSHans Petter Selasky if (hints->ai_flags & RAI_PASSIVE)
55*d6b92ffaSHans Petter Selasky return rdma_getaddrinfo(src, port, hints, rai);
56*d6b92ffaSHans Petter Selasky
57*d6b92ffaSHans Petter Selasky rai_hints = *hints;
58*d6b92ffaSHans Petter Selasky if (src) {
59*d6b92ffaSHans Petter Selasky rai_hints.ai_flags |= RAI_PASSIVE;
60*d6b92ffaSHans Petter Selasky ret = rdma_getaddrinfo(src, NULL, &rai_hints, &res);
61*d6b92ffaSHans Petter Selasky if (ret)
62*d6b92ffaSHans Petter Selasky return ret;
63*d6b92ffaSHans Petter Selasky
64*d6b92ffaSHans Petter Selasky rai_hints.ai_src_addr = res->ai_src_addr;
65*d6b92ffaSHans Petter Selasky rai_hints.ai_src_len = res->ai_src_len;
66*d6b92ffaSHans Petter Selasky rai_hints.ai_flags &= ~RAI_PASSIVE;
67*d6b92ffaSHans Petter Selasky }
68*d6b92ffaSHans Petter Selasky
69*d6b92ffaSHans Petter Selasky ret = rdma_getaddrinfo(dst, port, &rai_hints, rai);
70*d6b92ffaSHans Petter Selasky if (src)
71*d6b92ffaSHans Petter Selasky rdma_freeaddrinfo(res);
72*d6b92ffaSHans Petter Selasky
73*d6b92ffaSHans Petter Selasky return ret;
74*d6b92ffaSHans Petter Selasky }
75*d6b92ffaSHans Petter Selasky
size_str(char * str,size_t ssize,long long size)76*d6b92ffaSHans Petter Selasky void size_str(char *str, size_t ssize, long long size)
77*d6b92ffaSHans Petter Selasky {
78*d6b92ffaSHans Petter Selasky long long base, fraction = 0;
79*d6b92ffaSHans Petter Selasky char mag;
80*d6b92ffaSHans Petter Selasky
81*d6b92ffaSHans Petter Selasky if (size >= (1 << 30)) {
82*d6b92ffaSHans Petter Selasky base = 1 << 30;
83*d6b92ffaSHans Petter Selasky mag = 'g';
84*d6b92ffaSHans Petter Selasky } else if (size >= (1 << 20)) {
85*d6b92ffaSHans Petter Selasky base = 1 << 20;
86*d6b92ffaSHans Petter Selasky mag = 'm';
87*d6b92ffaSHans Petter Selasky } else if (size >= (1 << 10)) {
88*d6b92ffaSHans Petter Selasky base = 1 << 10;
89*d6b92ffaSHans Petter Selasky mag = 'k';
90*d6b92ffaSHans Petter Selasky } else {
91*d6b92ffaSHans Petter Selasky base = 1;
92*d6b92ffaSHans Petter Selasky mag = '\0';
93*d6b92ffaSHans Petter Selasky }
94*d6b92ffaSHans Petter Selasky
95*d6b92ffaSHans Petter Selasky if (size / base < 10)
96*d6b92ffaSHans Petter Selasky fraction = (size % base) * 10 / base;
97*d6b92ffaSHans Petter Selasky if (fraction) {
98*d6b92ffaSHans Petter Selasky snprintf(str, ssize, "%lld.%lld%c", size / base, fraction, mag);
99*d6b92ffaSHans Petter Selasky } else {
100*d6b92ffaSHans Petter Selasky snprintf(str, ssize, "%lld%c", size / base, mag);
101*d6b92ffaSHans Petter Selasky }
102*d6b92ffaSHans Petter Selasky }
103*d6b92ffaSHans Petter Selasky
cnt_str(char * str,size_t ssize,long long cnt)104*d6b92ffaSHans Petter Selasky void cnt_str(char *str, size_t ssize, long long cnt)
105*d6b92ffaSHans Petter Selasky {
106*d6b92ffaSHans Petter Selasky if (cnt >= 1000000000)
107*d6b92ffaSHans Petter Selasky snprintf(str, ssize, "%lldb", cnt / 1000000000);
108*d6b92ffaSHans Petter Selasky else if (cnt >= 1000000)
109*d6b92ffaSHans Petter Selasky snprintf(str, ssize, "%lldm", cnt / 1000000);
110*d6b92ffaSHans Petter Selasky else if (cnt >= 1000)
111*d6b92ffaSHans Petter Selasky snprintf(str, ssize, "%lldk", cnt / 1000);
112*d6b92ffaSHans Petter Selasky else
113*d6b92ffaSHans Petter Selasky snprintf(str, ssize, "%lld", cnt);
114*d6b92ffaSHans Petter Selasky }
115*d6b92ffaSHans Petter Selasky
size_to_count(int size)116*d6b92ffaSHans Petter Selasky int size_to_count(int size)
117*d6b92ffaSHans Petter Selasky {
118*d6b92ffaSHans Petter Selasky if (size >= (1 << 20))
119*d6b92ffaSHans Petter Selasky return 100;
120*d6b92ffaSHans Petter Selasky else if (size >= (1 << 16))
121*d6b92ffaSHans Petter Selasky return 1000;
122*d6b92ffaSHans Petter Selasky else if (size >= (1 << 10))
123*d6b92ffaSHans Petter Selasky return 10000;
124*d6b92ffaSHans Petter Selasky else
125*d6b92ffaSHans Petter Selasky return 100000;
126*d6b92ffaSHans Petter Selasky }
127*d6b92ffaSHans Petter Selasky
format_buf(void * buf,int size)128*d6b92ffaSHans Petter Selasky void format_buf(void *buf, int size)
129*d6b92ffaSHans Petter Selasky {
130*d6b92ffaSHans Petter Selasky uint8_t *array = buf;
131*d6b92ffaSHans Petter Selasky static uint8_t data;
132*d6b92ffaSHans Petter Selasky int i;
133*d6b92ffaSHans Petter Selasky
134*d6b92ffaSHans Petter Selasky for (i = 0; i < size; i++)
135*d6b92ffaSHans Petter Selasky array[i] = data++;
136*d6b92ffaSHans Petter Selasky }
137*d6b92ffaSHans Petter Selasky
verify_buf(void * buf,int size)138*d6b92ffaSHans Petter Selasky int verify_buf(void *buf, int size)
139*d6b92ffaSHans Petter Selasky {
140*d6b92ffaSHans Petter Selasky static long long total_bytes;
141*d6b92ffaSHans Petter Selasky uint8_t *array = buf;
142*d6b92ffaSHans Petter Selasky static uint8_t data;
143*d6b92ffaSHans Petter Selasky int i;
144*d6b92ffaSHans Petter Selasky
145*d6b92ffaSHans Petter Selasky for (i = 0; i < size; i++, total_bytes++) {
146*d6b92ffaSHans Petter Selasky if (array[i] != data++) {
147*d6b92ffaSHans Petter Selasky printf("data verification failed byte %lld\n", total_bytes);
148*d6b92ffaSHans Petter Selasky return -1;
149*d6b92ffaSHans Petter Selasky }
150*d6b92ffaSHans Petter Selasky }
151*d6b92ffaSHans Petter Selasky return 0;
152*d6b92ffaSHans Petter Selasky }
153*d6b92ffaSHans Petter Selasky
do_poll(struct pollfd * fds,int timeout)154*d6b92ffaSHans Petter Selasky int do_poll(struct pollfd *fds, int timeout)
155*d6b92ffaSHans Petter Selasky {
156*d6b92ffaSHans Petter Selasky int ret;
157*d6b92ffaSHans Petter Selasky
158*d6b92ffaSHans Petter Selasky do {
159*d6b92ffaSHans Petter Selasky #ifdef __linux__
160*d6b92ffaSHans Petter Selasky ret = rs_poll(fds, 1, timeout);
161*d6b92ffaSHans Petter Selasky #else
162*d6b92ffaSHans Petter Selasky ret = poll(fds, 1, timeout);
163*d6b92ffaSHans Petter Selasky #endif
164*d6b92ffaSHans Petter Selasky } while (!ret);
165*d6b92ffaSHans Petter Selasky
166*d6b92ffaSHans Petter Selasky return ret == 1 ? (fds->revents & (POLLERR | POLLHUP)) : ret;
167*d6b92ffaSHans Petter Selasky }
168