-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_library.c
163 lines (125 loc) · 4.53 KB
/
test_library.c
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
/**
* A small test program that uses libkvmchan
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libkvmchan.h"
#define perror_e(x) do { perror(x); exit(1); } while (0)
static void block_on_eventfd(int eventfd) {
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(eventfd, &rfds);
select(eventfd + 1, &rfds, NULL, NULL, NULL);
}
/**
* Wrappers for libkvmchan stream functions to emulate packet semantics.
*/
int read_wrapper(struct libkvmchan *chan, void *data, size_t size) {
size_t read = 0;
while (read < size) {
size_t remaining = size - read;
int n;
if ((n = libkvmchan_read(chan, data+read, remaining)) < 0)
return -1;
read += n;
}
return read;
}
int write_wrapper(struct libkvmchan *chan, void *data, size_t size) {
size_t written = 0;
while (written < size) {
size_t remaining = size - written;
int n;
if ((n = libkvmchan_write(chan, data+written, remaining)) < 0)
return -1;
written += n;
}
return written;
}
const char *vchan_state_str(int state) {
switch (state) {
case -1: return "FAILED_TO_CONTACT_KVMCHAND";
case VCHAN_DISCONNECTED: return "VCHAN_DISCONNECTED";
case VCHAN_CONNECTED: return "VCHAN_CONNECTED";
case VCHAN_WAITING: return "VCHAN_WAITING";
default:
return "(unknown)";
}
}
typedef int (*read_func_t)(struct libkvmchan *chan, void *data, size_t size);
typedef int (*write_func_t)(struct libkvmchan *chan, void *data, size_t size);
void do_test(bool client, int domain_no, int port_no, read_func_t read_func, write_func_t write_func) {
const size_t num_runs = 10;
struct libkvmchan *chan;
if (client) {
chan = libkvmchan_client_init(domain_no, port_no);
if (!chan)
perror_e("libkvmchan_client_init");
// Keep sending hello (u32 len, string)
for(size_t i=0; i<num_runs; i++) {
char *hello_str = "Hello, libkvmchan server!";
size_t hello_len = strlen(hello_str);
if (write_func(chan, &hello_len, sizeof(size_t)) < 0)
perror_e("libkvmchan_write1");
if (write_func(chan, hello_str, hello_len) < 0)
perror_e("libkvmchan_write2");
printf("Sent message to server!\n");
// Get the state of the vchan
int state = libkvmchan_get_state(chan);
printf("vchan state: %s\n", vchan_state_str(state));
usleep(1 * 1000 * 1000);
}
} else {
chan = libkvmchan_server_init(domain_no, port_no, 100, 100);
if (!chan)
perror_e("libkvmchan_server_init");
int state = libkvmchan_get_state(chan);
printf("vchan state after init: %s\n", vchan_state_str(state));
// Wait for messages from client and print them out
char str_buf[256];
size_t len_buf;
for (size_t i=0; i<num_runs; i++) {
int fd = libkvmchan_get_eventfd(chan);
if (fd < 0)
perror_e("libkvmchan_get_eventfd");
printf("Waiting for message...\n");
block_on_eventfd(fd);
printf("Received!\n");
if (read_func(chan, &len_buf, sizeof(size_t)) < 0)
perror_e("libkvmchan_read1");
if (read_func(chan, &str_buf, len_buf) < 0)
perror_e("libkvmchan_read1");
str_buf[len_buf] = '\0';
printf("Got message: %s\n", str_buf);
libkvmchan_clear_eventfd(chan);
int state = libkvmchan_get_state(chan);
printf("vchan state: %s\n", vchan_state_str(state));
usleep(1 * 1000 * 1000);
}
printf("Done. Waiting 5s and probing state\n");
usleep(5 * 1000 * 1000);
state = libkvmchan_get_state(chan);
printf("vchan state: %s\n", vchan_state_str(state));
}
printf("Closing vchan.\n");
if (!libkvmchan_close(chan))
perror_e("libkvmchan_close");
}
int main(int argc, char **argv) {
if (argc != 4) {
printf("usage: %s: <c/s> <domain_no> <port_no>\n", argv[0]);
return EXIT_FAILURE;
}
if (argv[1][0] != 'c' && argv[1][0] != 's') {
printf("usage: %s: <c/s> <domain_no> <port_no>\n", argv[0]);
return EXIT_FAILURE;
}
bool client = argv[1][0] == 'c';
int domain_no = atoi(argv[2]);
int port_no = atoi(argv[3]);
// Test w/ stream read/write functions
do_test(client, domain_no, port_no, read_wrapper, write_wrapper);
}