1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
/*
* Copyright (c) 2005-2006,2012 Intel Corporation. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* $Id$
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
#include <rdma/rdma_cma.h>
#include "common.h"
int use_rs = 1;
int get_rdma_addr(const char *src, const char *dst, const char *port,
struct rdma_addrinfo *hints, struct rdma_addrinfo **rai)
{
struct rdma_addrinfo rai_hints, *res;
int ret;
if (hints->ai_flags & RAI_PASSIVE) {
ret = rdma_getaddrinfo(src, port, hints, rai);
goto out;
}
rai_hints = *hints;
if (src) {
rai_hints.ai_flags |= RAI_PASSIVE;
ret = rdma_getaddrinfo(src, NULL, &rai_hints, &res);
if (ret)
goto out;
rai_hints.ai_src_addr = res->ai_src_addr;
rai_hints.ai_src_len = res->ai_src_len;
rai_hints.ai_flags &= ~RAI_PASSIVE;
}
ret = rdma_getaddrinfo(dst, port, &rai_hints, rai);
if (src)
rdma_freeaddrinfo(res);
out:
if (ret)
printf("rdma_getaddrinfo error: %s\n", gai_strerror(ret));
return ret;
}
void size_str(char *str, size_t ssize, long long size)
{
long long base, fraction = 0;
char mag;
if (size >= (1 << 30)) {
base = 1 << 30;
mag = 'g';
} else if (size >= (1 << 20)) {
base = 1 << 20;
mag = 'm';
} else if (size >= (1 << 10)) {
base = 1 << 10;
mag = 'k';
} else {
base = 1;
mag = '\0';
}
if (size / base < 10)
fraction = (size % base) * 10 / base;
if (fraction) {
snprintf(str, ssize, "%lld.%lld%c", size / base, fraction, mag);
} else {
snprintf(str, ssize, "%lld%c", size / base, mag);
}
}
void cnt_str(char *str, size_t ssize, long long cnt)
{
if (cnt >= 1000000000)
snprintf(str, ssize, "%lldb", cnt / 1000000000);
else if (cnt >= 1000000)
snprintf(str, ssize, "%lldm", cnt / 1000000);
else if (cnt >= 1000)
snprintf(str, ssize, "%lldk", cnt / 1000);
else
snprintf(str, ssize, "%lld", cnt);
}
int size_to_count(int size)
{
if (size >= (1 << 20))
return 100;
else if (size >= (1 << 16))
return 1000;
else if (size >= (1 << 10))
return 10000;
else
return 100000;
}
void format_buf(void *buf, int size)
{
uint8_t *array = buf;
static uint8_t data;
int i;
for (i = 0; i < size; i++)
array[i] = data++;
}
int verify_buf(void *buf, int size)
{
static long long total_bytes;
uint8_t *array = buf;
static uint8_t data;
int i;
for (i = 0; i < size; i++, total_bytes++) {
if (array[i] != data++) {
printf("data verification failed byte %lld\n", total_bytes);
return -1;
}
}
return 0;
}
int do_poll(struct pollfd *fds, int timeout)
{
int ret;
do {
ret = rs_poll(fds, 1, timeout);
} while (!ret);
return ret == 1 ? (fds->revents & (POLLERR | POLLHUP)) : ret;
}
struct rdma_event_channel *create_event_channel(void)
{
struct rdma_event_channel *channel;
channel = rdma_create_event_channel();
if (!channel) {
if (errno == ENODEV)
fprintf(stderr, "No RDMA devices were detected\n");
else
perror("failed to create RDMA CM event channel");
}
return channel;
}
int oob_server_setup(const char *src_addr, const char *port, int *sock)
{
struct addrinfo hint = {}, *ai;
int listen_sock;
int optval = 1;
int ret;
hint.ai_flags = AI_PASSIVE;
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_STREAM;
ret = getaddrinfo(src_addr, port, &hint, &ai);
if (ret) {
printf("getaddrinfo error: %s\n", gai_strerror(ret));
return ret;
}
listen_sock = socket(ai->ai_family, ai->ai_socktype, 0);
if (listen_sock == -1) {
ret = -errno;
goto free;
}
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
ret = bind(listen_sock, ai->ai_addr, ai->ai_addrlen);
if (ret) {
ret = -errno;
goto close;
}
ret = listen(listen_sock, 1);
if (ret) {
ret = -errno;
goto close;
}
*sock = accept(listen_sock, NULL, NULL);
if (*sock == -1)
ret = -errno;
setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval));
close:
close(listen_sock);
free:
freeaddrinfo(ai);
return ret;
}
int oob_client_setup(const char *dst_addr, const char *port, int *sock)
{
struct addrinfo hint = {}, *ai;
int nodelay = 1;
int ret;
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_STREAM;
ret = getaddrinfo(dst_addr, port, &hint, &ai);
if (ret) {
printf("getaddrinfo error: %s\n", gai_strerror(ret));
return ret;
}
*sock = socket(ai->ai_family, ai->ai_socktype, 0);
if (*sock == -1) {
ret = -errno;
goto out;
}
setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay));
ret = connect(*sock, ai->ai_addr, ai->ai_addrlen);
out:
freeaddrinfo(ai);
return ret;
}
int oob_sendrecv(int sock, char val)
{
char c = val;
ssize_t ret;
ret = send(sock, (void *) &c, sizeof(c), 0);
if (ret != sizeof(c))
return -errno;
ret = recv(sock, (void *) &c, sizeof(c), 0);
if (ret != sizeof(c))
return -errno;
if (c != val)
return -EINVAL;
return 0;
}
int oob_recvsend(int sock, char val)
{
char c = 0;
ssize_t ret;
ret = recv(sock, (void *) &c, sizeof(c), 0);
if (ret != sizeof(c))
return -errno;
if (c != val)
return -EINVAL;
ret = send(sock, (void *) &c, sizeof(c), 0);
if (ret != sizeof(c))
return -errno;
return 0;
}
static void *wq_handler(void *arg);
int wq_init(struct work_queue *wq, int thread_cnt)
{
int ret, i;
wq->head = NULL;
wq->tail = NULL;
ret = pthread_mutex_init(&wq->lock, NULL);
if (ret) {
perror("pthread_mutex_init");
return ret;
}
ret = pthread_cond_init(&wq->cond, NULL);
if (ret) {
perror("pthread_cond_init");
return ret;
}
wq->thread_cnt = thread_cnt;
wq->thread = calloc(thread_cnt, sizeof(*wq->thread));
if (!wq->thread)
return -ENOMEM;
wq->running = true;
for (i = 0; i < thread_cnt; i++) {
ret = pthread_create(&wq->thread[i], NULL, wq_handler, wq);
if (ret) {
perror("pthread_create");
return ret;
}
}
return 0;
}
void wq_cleanup(struct work_queue *wq)
{
int i;
pthread_mutex_lock(&wq->lock);
wq->running = false;
pthread_cond_broadcast(&wq->cond);
pthread_mutex_unlock(&wq->lock);
for (i = 0; i < wq->thread_cnt; i++)
pthread_join(wq->thread[i], NULL);
pthread_cond_destroy(&wq->cond);
pthread_mutex_destroy(&wq->lock);
}
void wq_insert(struct work_queue *wq, struct work_item *item,
void (*work_handler)(struct work_item *item))
{
bool empty;
item->next = NULL;
item->work_handler = work_handler;
pthread_mutex_lock(&wq->lock);
if (wq->head) {
wq->tail->next = item;
empty = false;
} else {
wq->head = item;
empty = true;
}
wq->tail = item;
pthread_mutex_unlock(&wq->lock);
if (empty)
pthread_cond_signal(&wq->cond);
}
struct work_item *wq_remove(struct work_queue *wq)
{
struct work_item *item;
item = wq->head;
wq->head = wq->head->next;
item->next = NULL;
return item;
}
static void *wq_handler(void *arg)
{
struct work_queue *wq = arg;
struct work_item *item;
pthread_mutex_lock(&wq->lock);
while (wq->running) {
while (!wq->head) {
pthread_cond_wait(&wq->cond, &wq->lock);
if (!wq->running)
goto out;
}
item = wq_remove(wq);
if (wq->head)
pthread_cond_signal(&wq->cond);
pthread_mutex_unlock(&wq->lock);
item->work_handler(item);
pthread_mutex_lock(&wq->lock);
}
out:
if (wq->head)
pthread_cond_signal(&wq->cond);
pthread_mutex_unlock(&wq->lock);
return NULL;
}
|