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