⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ibwrapper_test.c

📁 samba最新软件
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Unix SMB/CIFS implementation. * Test the infiniband wrapper. * * Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006 * * Major code contributions by Peter Somogyi <psomogyi@gamax.hu> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see <http://www.gnu.org/licenses/>. */#include <stdlib.h>#include <string.h>#include <stdio.h>#include <errno.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <netdb.h>#include <arpa/inet.h>#include <malloc.h>#include <assert.h>#include <unistd.h>#include <signal.h>#include <sys/time.h>#include <time.h>#include "includes.h"#include "lib/events/events.h"#include "ib/ibwrapper.h"struct ibwtest_ctx {	int	is_server;	char	*id; /* my id */	struct ibw_initattr *attrs;	int	nattrs;	char	*opts; /* option string */	struct sockaddr_in *addrs; /* dynamic array of dest addrs */	int	naddrs;	unsigned int	nsec; /* delta times between messages in nanosec */	unsigned int	sleep_usec; /* microsecs to sleep in the main loop to emulate overloading */	uint32_t	maxsize; /* maximum variable message size */	int	cnt;	int	nsent;	int	nmsg; /* number of messages to send (client) */	int	kill_me;	int	stopping;	int	error;	struct ibw_ctx	*ibwctx;	struct timeval	start_time, end_time;};struct ibwtest_conn {	char	*id;};enum testopcode {	TESTOP_SEND_ID = 1,	TESTOP_SEND_TEXT = 2,	TESTOP_SEND_RND = 3};int ibwtest_connect_everybody(struct ibwtest_ctx *tcx){	struct ibw_conn		*conn;	struct ibwtest_conn	*tconn = talloc_zero(tcx, struct ibwtest_conn);	int	i;	for(i=0; i<tcx->naddrs; i++) {		conn = ibw_conn_new(tcx->ibwctx, tconn);		if (ibw_connect(conn, &tcx->addrs[i], tconn)) {			fprintf(stderr, "ibw_connect error at %d\n", i);			return -1;		}	}	DEBUG(10, ("sent %d connect request...\n", tcx->naddrs));	return 0;}int ibwtest_send_id(struct ibw_conn *conn){	struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);	char *buf;	void *key;	uint32_t	len;	DEBUG(10, ("ibwtest_send_id\n"));	len = sizeof(uint32_t)+strlen(tcx->id)+2;	if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {		DEBUG(0, ("send_id: ibw_alloc_send_buf failed\n"));		return -1;	}	/* first sizeof(uint32_t) size bytes are for length */	*((uint32_t *)buf) = len;	buf[sizeof(uint32_t)] = (char)TESTOP_SEND_ID;	strcpy(buf+sizeof(uint32_t)+1, tcx->id);	if (ibw_send(conn, buf, key, len)) {		DEBUG(0, ("send_id: ibw_send error\n"));		return -1;	}	tcx->nsent++;	return 0;}int ibwtest_send_test_msg(struct ibwtest_ctx *tcx, struct ibw_conn *conn, const char *msg){	char *buf, *p;	void *key;	uint32_t len;	if (conn->state!=IBWC_CONNECTED)		return 0; /* not yet up */	len = strlen(msg) + 2 + sizeof(uint32_t);	if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {		fprintf(stderr, "send_test_msg: ibw_alloc_send_buf failed\n");		return -1;	}	*((uint32_t *)buf) = len;	p = buf;	p += sizeof(uint32_t);	p[0] = (char)TESTOP_SEND_TEXT;	p++;	strcpy(p, msg);	if (ibw_send(conn, buf, key, len)) {		DEBUG(0, ("send_test_msg: ibw_send error\n"));		return -1;	}	tcx->nsent++;	return 0;}unsigned char ibwtest_fill_random(unsigned char *buf, uint32_t size){	uint32_t	i = size;	unsigned char	sum = 0;	unsigned char	value;	while(i) {		i--;		value = (unsigned char)(256.0 * (rand() / (RAND_MAX + 1.0)));		buf[i] = value;		sum += value;	}	return sum;}unsigned char ibwtest_get_sum(unsigned char *buf, uint32_t size){	uint32_t	i = size;	unsigned char	sum = 0;	while(i) {		i--;		sum += buf[i];	}	return sum;}int ibwtest_do_varsize_scenario_conn_size(struct ibwtest_ctx *tcx, struct ibw_conn *conn, uint32_t size){	unsigned char *buf;	void	*key;	uint32_t	len;	unsigned char	sum;	len = sizeof(uint32_t) + 1 + size + 1;	if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {		DEBUG(0, ("varsize/ibw_alloc_send_buf failed\n"));		return -1;	}	*((uint32_t *)buf) = len;	buf[sizeof(uint32_t)] = TESTOP_SEND_RND;	sum = ibwtest_fill_random(buf + sizeof(uint32_t) + 1, size);	buf[sizeof(uint32_t) + 1 + size] = sum;	if (ibw_send(conn, buf, key, len)) {		DEBUG(0, ("varsize/ibw_send failed\n"));		return -1;	}	tcx->nsent++;	return 0;}int ibwtest_do_varsize_scenario_conn(struct ibwtest_ctx *tcx, struct ibw_conn *conn){	uint32_t	size;	int	i;	for(i=0; i<tcx->nmsg; i++)	{		//size = (uint32_t)((float)(tcx->maxsize) * (rand() / (RAND_MAX + 1.0)));		size = (uint32_t)((float)(tcx->maxsize) * ((float)(i+1)/(float)tcx->nmsg));		if (ibwtest_do_varsize_scenario_conn_size(tcx, conn, size))			return -1;	}	return 0;}/*int ibwtest_do_varsize_scenario(ibwtest_ctx *tcx){	int	rc;	struct ibw_conn *conn;	for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) {		if (conn->state==IBWC_CONNECTED) {			rc = ibwtest_do_varsize_scenario_conn(tcx, conn);			if (rc)				tcx->error = rc;		}	}}*/int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn){	struct ibwtest_ctx	*tcx = NULL; /* userdata */	struct ibwtest_conn	*tconn = NULL; /* userdata */	if (ctx) {		tcx = talloc_get_type(ctx->ctx_userdata, struct ibwtest_ctx);		switch(ctx->state) {		case IBWS_INIT:			DEBUG(10, ("test IBWS_INIT\n"));			break;		case IBWS_READY:			DEBUG(10, ("test IBWS_READY\n"));			break;		case IBWS_CONNECT_REQUEST:			DEBUG(10, ("test IBWS_CONNECT_REQUEST\n"));			tconn = talloc_zero(conn, struct ibwtest_conn);			if (ibw_accept(ctx, conn, tconn)) {				DEBUG(0, ("error accepting the connect request\n"));			}			break;		case IBWS_STOPPED:			DEBUG(10, ("test IBWS_STOPPED\n"));			tcx->kill_me = 1; /* main loop can exit */			break;		case IBWS_ERROR:			DEBUG(10, ("test IBWS_ERROR\n"));			ibw_stop(tcx->ibwctx);			break;		default:			assert(0);			break;		}	}	if (conn) {		tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);		switch(conn->state) {		case IBWC_INIT:			DEBUG(10, ("test IBWC_INIT\n"));			break;		case IBWC_CONNECTED:			if (gettimeofday(&tcx->start_time, NULL)) {				DEBUG(0, ("gettimeofday error %d", errno));				return -1;			}			ibwtest_send_id(conn);			break;		case IBWC_DISCONNECTED:			DEBUG(10, ("test IBWC_DISCONNECTED\n"));			talloc_free(conn);			break;		case IBWC_ERROR:			DEBUG(10, ("test IBWC_ERROR %s\n", ibw_getLastError()));			break;		default:			assert(0);			break;		}	}	return 0;}int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n){	struct ibwtest_conn *tconn;	enum testopcode op;	struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);	int	rc = 0;	assert(conn!=NULL);	assert(n>=sizeof(uint32_t)+1);	tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);	op = (enum testopcode)((char *)buf)[sizeof(uint32_t)];	if (op==TESTOP_SEND_ID) {		tconn->id = talloc_strdup(tconn, ((char *)buf)+sizeof(uint32_t)+1);	}	if (op==TESTOP_SEND_ID || op==TESTOP_SEND_TEXT) {		DEBUG(11, ("[%d]msg from %s: \"%s\"(%d)\n", op,			tconn->id ? tconn->id : "NULL", ((char *)buf)+sizeof(uint32_t)+1, n));	}	if (tcx->is_server) {		if (op==TESTOP_SEND_RND) {			unsigned char sum;			sum = ibwtest_get_sum((unsigned char *)buf + sizeof(uint32_t) + 1,				n - sizeof(uint32_t) - 2);			DEBUG(11, ("[%d]msg varsize %u/sum %u from %s\n",				op,				n - sizeof(uint32_t) - 2,				(uint32_t)sum,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -