File: sim_client.c

package info (click to toggle)
ibsim 0.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 480 kB
  • sloc: ansic: 7,907; sh: 200; makefile: 49
file content (323 lines) | stat: -rw-r--r-- 8,276 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2004-2008 Voltaire, Inc. All rights reserved.
 *
 * This file is part of ibsim.
 *
 * ibsim 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.
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <infiniband/mad.h>

#include <ibsim.h>

#include "sim_client.h"

#ifdef SIM_CLIENT_NOISY_DEBUG
#undef DEBUG
#define DEBUG	IBWARN
#else
#define DEBUG(fmt...)
#endif

static unsigned int remote_mode = 0;
static char* socket_basename;

static int sim_ctl(struct sim_client *sc, int type, void *data, int len)
{
	struct sim_ctl ctl;

	DEBUG("type %d len %d", type, len);

	memset(&ctl, 0, sizeof(ctl));

	if (sc->fd_ctl < 0) {
		IBWARN("no ctl connection");
		return -1;
	}

	ctl.magic = SIM_MAGIC;
	ctl.type = type;
	ctl.clientid = sc->clientid;
	ctl.len = len;
	if (len)
		memcpy(ctl.data, data, len);

	if (write(sc->fd_ctl, &ctl, sizeof(ctl)) != sizeof(ctl)) {
		IBWARN("ctl failed(write)");
		return -1;
	}

	ctl.type = SIM_CTL_ERROR;

	if (read(sc->fd_ctl, &ctl, sizeof(ctl)) != sizeof(ctl)) {
		IBWARN("ctl failed(read)");
		return -1;
	}

	if (ctl.type == SIM_CTL_ERROR) {
		IBWARN("ctl error");
		return -1;
	}
	if (len)
		memcpy(data, &ctl.data, len);

	return 0;
}

static size_t make_name(union name_t *name, char *host, unsigned port,
			const char *fmt, ...)
{
	size_t size;
	memset(name, 0, sizeof(*name));
	if (remote_mode) {
		struct sockaddr_in *name_i = &name->name_i;
	        name_i->sin_family = AF_INET;
		if (host) {
			name_i->sin_addr.s_addr = inet_addr(host);
			if (name_i->sin_addr.s_addr == (unsigned long)INADDR_NONE) {
				struct hostent *hostp;
				if(!(hostp = gethostbyname(host)))
					IBPANIC("cannot resolve ibsim server"
						" %s: h_errno = %d\n",
						host, h_errno);
				memcpy(&name_i->sin_addr, hostp->h_addr,
				       sizeof(name_i->sin_addr));
			}
		} else
			name_i->sin_addr.s_addr = htonl(INADDR_ANY);
	        name_i->sin_port = htons(port);
		size = sizeof(*name_i);
	} else {
		va_list args;
		struct sockaddr_un *name_u = &name->name_u;
		size = sizeof(*name_u) -
				((void *)name_u->sun_path + 1 - (void*)name_u);
		name_u->sun_family = AF_UNIX;
		name_u->sun_path[0] = 0;	// abstract name space
		va_start(args, fmt);
		size = vsnprintf(name_u->sun_path + 1, size, fmt, args);
		va_end(args);
		size += 1 + ((void *)name_u->sun_path + 1 - (void*)name_u);
	}
	return size;
}

static char *get_name(union name_t *name)
{
	if (remote_mode)
		return inet_ntoa(name->name_i.sin_addr);
	else
		return name->name_u.sun_path + 1;
}

static int sim_attach(int fd, union name_t *name, size_t size)
{
	int retries;
	int r;

	for (retries = 0;; retries++) {
		DEBUG("attempt to connect to %s (attempt %d)",
		      get_name(name), retries);

		if ((r = connect(fd, (struct sockaddr *)name, size)) >= 0)
			break;

		if (r < 0 && errno == ECONNREFUSED) {
			DEBUG("waiting for %s to start", get_name(name));
			sleep(2);
			continue;
		}

		IBPANIC("can't connect to sim socket %s", get_name(name));
	}

	return 0;
}

static int sim_connect(struct sim_client *sc, int id, int qp, char *nodeid,
		       char *issm)
{
	struct sim_client_info info = { 0 };

	info.id = id;
	if (issm)
		info.issm = 1;
	else
		info.issm = 0;
	info.qp = qp;

	if (nodeid)
		strncpy(info.nodeid, nodeid, sizeof(info.nodeid) - 1);

	if (sim_ctl(sc, SIM_CTL_CONNECT, &info, sizeof(info)) < 0)
		return -1;

	id = info.id;

	if (!nodeid || strcmp(nodeid, info.nodeid))
		IBWARN("attached as client %d at node \"%s\"", id,
		       info.nodeid);
	return id;
}

static int sim_disconnect(struct sim_client *sc)
{
	return sim_ctl(sc, SIM_CTL_DISCONNECT, NULL, 0);
}

static int sim_init(struct sim_client *sc, char *nodeid, char *issm)
{
	union name_t name;
	socklen_t size;
	int fd, ctlfd;
	int pid = getpid();
	char *connect_port;
	char *connect_host;
	unsigned short port;

	connect_port = getenv("IBSIM_SERVER_PORT");
	connect_host = getenv("IBSIM_SERVER_NAME");
	socket_basename = getenv("IBSIM_SOCKNAME");

	if(!socket_basename)
		socket_basename = SIM_BASENAME;

	if (connect_host && *connect_host)
		remote_mode = 1;

	DEBUG("init client pid=%d, nodeid=%s", pid, nodeid ? nodeid : "none");

	if ((fd = socket(remote_mode ? PF_INET : PF_LOCAL, SOCK_DGRAM, 0)) < 0)
		IBPANIC("can't get socket (fd)");

	if ((ctlfd = socket(remote_mode ? PF_INET : PF_LOCAL, SOCK_DGRAM, 0)) < 0)
		IBPANIC("can't get socket (ctlfd)");

	size = make_name(&name, NULL, 0, "%s:ctl%d", socket_basename, pid);

	if (bind(ctlfd, (struct sockaddr *)&name, size) < 0)
		IBPANIC("can't bind ctl socket");

	DEBUG("init %d: opened ctl fd %d as \'%s\'",
	      pid, ctlfd, get_name(&name));

	port = connect_port ? atoi(connect_port) : IBSIM_DEFAULT_SERVER_PORT;
	size = make_name(&name, connect_host, port, "%s:ctl", socket_basename);

	sim_attach(ctlfd, &name, size);

	sc->fd_ctl = ctlfd;

	size = make_name(&name, NULL, 0, "%s:in%d", socket_basename, pid);

	if (bind(fd, (struct sockaddr *)&name, size) < 0)
		IBPANIC("can't bind input socket");

	DEBUG("init client %d: opened input data fd %d as \'%s\'\n",
	      pid, fd, get_name(&name));
	if (getsockname(fd, (struct sockaddr *)&name, &size) < 0 )
		IBPANIC("can't read data from bound socket");
	port = ntohs(name.name_i.sin_port);

	sc->clientid = sim_connect(sc, remote_mode ? port : pid, 0, nodeid, issm);
	if (sc->clientid < 0)
		IBPANIC("connect failed");

	port = connect_port ? atoi(connect_port) : IBSIM_DEFAULT_SERVER_PORT;
	size = make_name(&name, connect_host, port + sc->clientid + 1,
			 "%s:out%d", socket_basename, sc->clientid);

	sim_attach(fd, &name, size);

	DEBUG("init client %d: connect data fd %d to \'%s\'\n",
	      sc->clientid, fd, get_name(&name));

	sc->fd_pktin = fd;
	sc->fd_pktout = fd;

	return fd;
}

/*************************/

int sim_client_set_sm(struct sim_client *sc, unsigned issm)
{
	DEBUG("sim_client_is_sm: setting to %d", issm);
	return sim_ctl(sc, SIM_CTL_SET_ISSM, &issm, sizeof(int));
}

int sim_client_init(struct sim_client *sc)
{
	char *nodeid;
	char *issm;

	nodeid = getenv("SIM_HOST");
	issm = getenv("SIM_SET_ISSM");
	if (sim_init(sc, nodeid, issm) < 0)
		return -1;
	if (sim_ctl(sc, SIM_CTL_GET_VENDOR, &sc->vendor,
		    sizeof(sc->vendor)) < 0)
		goto _exit;
	if (sim_ctl(sc, SIM_CTL_GET_NODEINFO, sc->nodeinfo,
		    sizeof(sc->nodeinfo)) < 0)
		goto _exit;

	sc->portinfo[0] = 0;	// portno requested
	if (sim_ctl(sc, SIM_CTL_GET_PORTINFO, sc->portinfo,
		    sizeof(sc->portinfo)) < 0)
		goto _exit;
	if (sim_ctl(sc, SIM_CTL_GET_PKEYS, sc->pkeys, sizeof(sc->pkeys)) < 0)
		goto _exit;
	if (issm)
		sim_client_set_sm(sc, 1);
	return 0;
  _exit:
	sim_disconnect(sc);
	sc->fd_ctl = sc->fd_pktin = sc->fd_pktout = -1;
	return -1;
}

void sim_client_exit(struct sim_client *sc)
{
	sim_disconnect(sc);
	sc->fd_ctl = sc->fd_pktin = sc->fd_pktout = -1;
}