socket.c
11.8 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*************************************************************************
*
* File: socket.c
*
* These routines form a simple IPC mechanism to communicate between Verilog
* and C programs. This file is compiled in this directory with PLI defined
* for interfacing with the Verilog files and re-compiled in the 'src'
* directory for interfacing with the C-based I/O test driver.
*
*
*/
#include <sys/types.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#ifdef __sgi__
#include <bstring.h>
#else
#include <netinet/in.h>
#endif
#include "simipc.h"
#include "trace.h"
#ifdef PLI
#include "acc_user.h"
#include "vcsuser.h"
static s_setval_value value_s = { accIntVal };
static s_setval_delay delay_s = { { accRealTime, 0, 0, 0 }, accNoDelay };
#endif /* PLI */
#ifndef PLI
#define acc_initialize()
#define acc_configure(a, b)
#define acc_set_value(a, b, c)
#define tf_putp(a,b) return(b)
extern FILE *LogFp;
#else /* !PLI */
unsigned long Dflags = 0;
#ifndef __sgi__
FILE *LogFp;
#else
FILE *LogFp = stderr;
#endif
#endif /* PLI */
char IpcName[256];
static IpcPkt pkt;
#ifndef __sgi__
char *iosim_server=NULL;
int iosim_port;
#endif
/*
* int IpcInit(char *name)
*/
#ifdef PLI
void
IpcInit(void)
{
char *name;
#else
int
IpcInit(char *name)
{
#endif
acc_initialize();
acc_configure(accDevelopmentVersion, "1.6");
#ifdef PLI
#ifndef __sgi__
LogFp = stderr;
#endif
if (tf_nump() != 1) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
name = (char *)tf_getcstringp(1);
#endif
if ((name == NULL) || (strcmp(name, "NULL") == 0)) {
sprintf(IpcName, DEFAULT_NAME_PATH, getenv("USER"));
}
else
sprintf(IpcName, "/tmp/%s", name);
fprintf(stderr, "IpcInit(iosim server): ipc_name=%s\n", IpcName)
_TRACE(DALL, fprintf(LogFp, "IpcInit: ipc_name=%s\n", IpcName));
#ifndef __sgi__
{ /* Setup Linux test environment */
char *sport;
iosim_server = getenv("IOSIM_SERVER");
sport = getenv("IOSIM_PORT");
if (iosim_server == NULL || sport == NULL) {
fprintf(stderr, "\n\nFatal error: please set environment variable: \n");
fprintf(stderr, " IOSIM_SERVER and IOSIM_PORT\n\n\n");
tf_putp(0, -1);
exit(1);
}
iosim_port = strtoul(sport, NULL, 0);
fprintf(stderr, "IOSIM server=%s port=%d\n", iosim_server, iosim_port);
}
#endif
tf_putp(0, 0);
} /* IpcInit */
int
IpcReceive(int fd, char *buf, int nbytes)
{
int nleft, nread;
_TRACE(DREAD, fprintf(LogFp,
"IpcReceive: fd=%d, buf=0x%08x, nbytes=%d\n", fd, buf, nbytes));
bzero(buf, nbytes);
nleft = nbytes;
while (nleft > 0) {
nread = recv(fd, buf, nleft, 0);
if (nread < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcReceive: recv failed, errno=%d\n", errno));
perror("recv failed: ");
return(-1);
}
#ifdef __sgi__
if (nread == 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcReceive: nread == 0!\n"));
break;
}
#endif
nleft -= nread;
buf += nread;
}
_TRACE(DREAD, fprintf(LogFp,
"IpcReceive: return=%d, nbytes=%d, nleft=%d\n", nbytes - nleft,
nbytes, nleft));
return( (nleft > 0) ? -1 : 0);
} /* IpcReceive */
int
IpcSend(int fd, char *buf, int nbytes)
{
int nleft, nwritten;
int ret;
_TRACE(DWRITE, fprintf(LogFp,"ipcWrite: fd=%d, buf=0x%08x, nbytes=%d\n",
fd, buf, nbytes));
nleft = nbytes;
while (nleft > 0) {
nwritten = send(fd, buf, nleft, 0);
if (nwritten <= 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcSend: send failed, errno=%d\n", errno));
perror("send failed: ");
return(-1);
}
nleft -= nwritten;
buf += nwritten;
}
_TRACE(DWRITE, fprintf(LogFp,
"ipcWrite: return=%d, nbytes=%d, nleft=%d\n", nbytes - nleft,
nbytes, nleft));
return( (nleft > 0) ? -1 : 0);
} /* IpcSend */
/*
* int IpcOpen(int type)
*/
#ifdef PLI
void
IpcOpen(void)
{
int type;
#else
int
IpcOpen(int type)
{
#endif
int fd;
#ifdef __sgi__
struct sockaddr_un addr;
#else
struct sockaddr_in addr;
int on;
#endif
#ifdef PLI
if (tf_nump() != 1) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
type = (int)tf_getp(1);
#endif
#ifdef __sgi__
fd = socket(AF_UNIX, SOCK_STREAM, 0);
#else
fd = socket(AF_INET, SOCK_STREAM, 0);
#endif
if (fd < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcOpen: socket failed, errno=%d\n", errno));
perror("opening stream socket");
tf_putp(0, -1);
}
if (type == IPC_SERVER) {
#ifdef __sgi__
/*
* Arrange to have ourselves die when parent dies
*/
if (prctl(PR_TERMCHILD) == -1) {
_TRACE(DERROR, fprintf(LogFp,
"IpcOpen: prctl failed, errno=%d\n", errno));
perror("prctl(PR_TERMCHILD)");
tf_putp(0, -1);
}
#else
#endif
/*
* Unlink UNIX socket streams path if exists
*/
unlink(IpcName);
#ifndef __sgi__
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(iosim_port);
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on) < 0) {
perror("SO_REUSEADDR stream socket");
tf_putp(0, -1);
}
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcOpen: bind failed, errno=%d\n", errno));
perror("binding stream socket");
tf_putp(0, -1);
close(fd);
fd = -1;
}
#else
strcpy(addr.sun_path, IpcName);
addr.sun_family = AF_UNIX;
/* Bind the name to the socket */
if (bind(fd, (struct sockaddr *)&addr,
strlen(addr.sun_path) + sizeof(addr.sun_family)) < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcOpen: bind failed, errno=%d\n", errno));
perror("binding stream socket");
tf_putp(0, -1);
}
#endif
/* Listen on the socket */
if (listen(fd, 5) == -1) {
_TRACE(DERROR, fprintf(LogFp,
"IpcOpen: listen failed, errno=%d\n", errno));
perror("listening on stream socket");
tf_putp(0, -1);
}
}
tf_putp(0, fd);
} /* IpcOpen */
/*
* int IpcAccept(int fd)
*/
#ifdef PLI
void
IpcAccept(void)
{
int fd;
#else
int
IpcAccept(int fd)
{
#endif
int newfd;
#ifdef __sgi__
struct sockaddr_un from;
#else
struct sockaddr_in from;
#endif
int fromlen = sizeof (from);
#ifdef PLI
if (tf_nump() != 1) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
fd = (int)tf_getp(1);
#endif
newfd = accept(fd, (struct sockaddr *)&from, &fromlen);
if (newfd < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcAccept: accept failed, errno=%d\n", errno));
/*
perror("accepting stream socket");
*/
tf_putp(0, -1);
}
else {
tf_putp(0, newfd);
}
} /* IpcAccept */
/*
* int IpcConnect(int fd)
*/
#ifdef PLI
void
IpcConnect(void)
{
int fd;
#else
int
IpcConnect(int fd)
{
#endif
#ifdef __sgi__
struct sockaddr_un addr;
#else
struct sockaddr_in addr;
#endif
int addrlen = sizeof (addr);
#ifdef PLI
if (tf_nump() != 1) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
fd = (int)tf_getp(1);
#endif
#ifndef __sgi__
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(iosim_server);
addr.sin_port = htons(iosim_port);
#else
strcpy(addr.sun_path, IpcName);
addr.sun_family = AF_UNIX;
#endif
if (connect(fd, (struct sockaddr *)&addr, addrlen) < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcConnect: connect failed, errno=%d\n", errno));
/*
perror("connecting stream socket");
*/
tf_putp(0, -1);
}
else {
tf_putp(0, 0);
}
} /* IpcConnect */
/*
* int IpcClose(int fd)
*/
#ifdef PLI
void
IpcClose(void)
{
int fd;
#else
int
IpcClose(int fd)
{
#endif
#ifdef PLI
if (tf_nump() != 1) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
fd = (int)tf_getp(1);
#endif
close(fd);
tf_putp(0, 0);
} /* IpcClose */
#ifdef PLI
/*
* int IpcReceivePli(int fd, int *code, int *size, int *address,
* int *data0, int *data1, int *data2, int *data3,
* int *data4, int *data5, int *data6, int *data7)
*/
void
IpcReceivePli(void)
{
int fd, i, combine_word, code;
unsigned int buf[20];
handle hcode, hsize, haddr, hdata[8], hmsg;
s_acc_vecval tmp;
char *str;
tf_putp(0, 0);
if (tf_nump() != 13) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
fd = (int)tf_getp(1);
hcode = acc_handle_tfarg(2);
hsize = acc_handle_tfarg(3);
haddr = acc_handle_tfarg(4);
for (i=0; i<8; i++)
hdata[i] = acc_handle_tfarg(5+i);
hmsg = acc_handle_tfarg(13);
if (IpcReceive(fd, (char *)buf, sizeof(IpcPkt)) < 0) {
tf_error("IpcReceivePli: IpcReceive failed");
tf_putp(0, -1);
}
else {
code = ntohl((int)buf[PKT_CODE_OFS]);
value_s.value.integer = code;
acc_set_value(hcode, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_SIZE_OFS]);
acc_set_value(hsize, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_ADDR_OFS]);
acc_set_value(haddr, &value_s, &delay_s);
if (code != BD_REQ_DISPLAY_MSG) {
combine_word = (code & BCP_COMBINE_XZ)?BCP_COMBINE_WORD(code):0;
value_s.format = accVectorVal;
for (i=0; i<combine_word; i++) {
tmp.aval = ntohl((int)buf[PKT_DATA_OFS+i]);
tmp.bval = ntohl((int)buf[PKT_DATA_OFS+8+i]);
value_s.value.vector = &tmp;
acc_set_value(hdata[i], &value_s, &delay_s);
}
value_s.format = accIntVal;
for (i=combine_word; i<8; i++) {
value_s.value.integer = ntohl((int)buf[PKT_DATA_OFS+i]);
acc_set_value(hdata[i], &value_s, &delay_s);
}
} else {
value_s.format = accStringVal;
str = (char *)(buf+PKT_DATA_OFS);
value_s.value.str = str;
acc_set_value(hmsg, &value_s, &delay_s);
value_s.format = accIntVal;
}
tf_putp(0, 0);
}
} /* IpcReceivePli */
/*
* int IpcSendPli(int fd, int code, int size, int address,
* int data0, int data1, int data2, int data3,
* int data4, int data5, int data6, int data7,
* int xz0, int xz1, int xz2, int xz3,
* int xz4, int xz5, int xz6, int xz7,
*/
void
IpcSendPli(void)
{
#define MSG_1 "\t\tdata:0x%08x, 0x%08x, 0x%08x, 0x%08x, \
0x%08x, 0x%08x, 0x%08x, 0x%08x\n"
int fd, split_word, i, code;
IpcPkt pkt;
int ret;
handle hdata[8];
unsigned char *buf;
double mm;
s_acc_value v;
s_acc_vecval tmp;
if (tf_nump() != 12) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
bzero(&pkt, sizeof(IpcPkt));
pkt.length = sizeof(IpcPkt);
fd = (int)tf_getp(1);
code = (int)tf_getp(2);
pkt.code = htonl(BCP_DEL_SPLIT(code));
pkt.size = htonl((int)tf_getp(3));
pkt.address = htonl((int)tf_getp(4));
split_word = (code & BCP_SPLIT_XZ)?BCP_SPLIT_WORD(code):0;
v.value.vector = &tmp;
v.format = accVectorVal;
for (i=0; i<split_word; i++) {
hdata[i] = acc_handle_tfarg(5+i);
acc_fetch_value(hdata[i], "%%", &v);
pkt.data[i] = htonl(v.value.vector->aval);
pkt.data[i+8] = htonl(v.value.vector->bval);
}
for (i=split_word; i<8; i++)
pkt.data[i] = htonl((int)tf_getp(5+i));
_TRACE(DWRITE, fprintf(LogFp, "IpcSendPli: fd=%d, len=%d\n",
fd, pkt.length));
_TRACE(DWRITE, fprintf(LogFp,
"IpcSendPli: pkt: code=%d, size=%d, addr=0x%08x\n",
pkt.code, pkt.size, pkt.address));
_TRACE(DWRITE, fprintf(LogFp, MSG_1,
pkt.data[0], pkt.data[1], pkt.data[2], pkt.data[3],
pkt.data[4], pkt.data[5], pkt.data[6], pkt.data[7]));
ret = IpcSend(fd, (char *)&pkt, sizeof(IpcPkt));
if (ret < 0)
tf_error("IpcSendPli: IpcSend failed");
tf_putp(0, ret);
} /* IpcSendPli */
#endif /* PLI */