Add FD_NET debugging value
[fio.git] / client.c
CommitLineData
132159a5
JA
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <limits.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <sys/poll.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <sys/wait.h>
11#include <sys/mman.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
15
16#include "fio.h"
17#include "server.h"
18#include "crc/crc32.h"
b66570dc 19#include "flist.h"
132159a5 20
b66570dc
JA
21struct fio_client {
22 struct flist_head list;
23 struct sockaddr_in addr;
24 char *hostname;
25 int fd;
26};
27
28static FLIST_HEAD(client_list);
b66570dc
JA
29
30static struct fio_client *find_client_by_fd(int fd)
31{
32 struct fio_client *client;
33 struct flist_head *entry;
34
35 flist_for_each(entry, &client_list) {
36 client = flist_entry(entry, struct fio_client, list);
37
38 if (client->fd == fd)
39 return client;
40 }
41
42 return NULL;
43}
44
a37f69b7 45#if 0
b66570dc
JA
46static struct fio_client *find_client_by_name(const char *name)
47{
48 struct fio_client *client;
49 struct flist_head *entry;
50
51 flist_for_each(entry, &client_list) {
52 client = flist_entry(entry, struct fio_client, list);
53
54 if (!strcmp(name, client->hostname))
55 return client;
56 }
57
58 return NULL;
59}
a37f69b7 60#endif
b66570dc
JA
61
62static void remove_client(struct fio_client *client)
63{
b66570dc
JA
64 flist_del(&client->list);
65 nr_clients--;
66 free(client->hostname);
67 free(client);
68}
132159a5 69
a37f69b7 70void fio_client_add(const char *hostname)
132159a5 71{
b66570dc 72 struct fio_client *client;
132159a5 73
b66570dc 74 client = malloc(sizeof(*client));
a37f69b7
JA
75 memset(client, 0, sizeof(*client));
76 client->hostname = strdup(hostname);
77 client->fd = -1;
78 flist_add(&client->list, &client_list);
79 nr_clients++;
80}
81
82static int fio_client_connect(struct fio_client *client)
83{
84 int fd;
132159a5 85
b66570dc
JA
86 memset(&client->addr, 0, sizeof(client->addr));
87 client->addr.sin_family = AF_INET;
88 client->addr.sin_port = htons(fio_net_port);
89
a37f69b7 90 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
132159a5
JA
91 struct hostent *hent;
92
a37f69b7 93 hent = gethostbyname(client->hostname);
132159a5
JA
94 if (!hent) {
95 log_err("fio: gethostbyname: %s\n", strerror(errno));
96 return 1;
97 }
98
b66570dc 99 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
132159a5
JA
100 }
101
102 fd = socket(AF_INET, SOCK_STREAM, 0);
103 if (fd < 0) {
104 log_err("fio: socket: %s\n", strerror(errno));
b66570dc 105 free(client);
132159a5
JA
106 return 1;
107 }
108
b66570dc 109 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
132159a5 110 log_err("fio: connect: %s\n", strerror(errno));
b66570dc 111 free(client);
132159a5
JA
112 return 1;
113 }
114
b66570dc 115 client->fd = fd;
132159a5
JA
116 return 0;
117}
118
a37f69b7
JA
119int fio_clients_connect(void)
120{
121 struct fio_client *client;
122 struct flist_head *entry, *tmp;
123 int ret;
124
125 flist_for_each_safe(entry, tmp, &client_list) {
126 client = flist_entry(entry, struct fio_client, list);
127
128 ret = fio_client_connect(client);
129 if (ret)
130 remove_client(client);
131 }
132
133 return !nr_clients;
134}
135
b66570dc 136static int send_file_buf(struct fio_client *client, char *buf, off_t size)
132159a5 137{
b66570dc 138 return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
132159a5
JA
139}
140
141/*
142 * Send file contents to server backend. We could use sendfile(), but to remain
143 * more portable lets just read/write the darn thing.
144 */
a37f69b7 145static int fio_client_send_ini(struct fio_client *client, const char *filename)
132159a5
JA
146{
147 struct stat sb;
148 char *p, *buf;
149 off_t len;
150 int fd, ret;
151
152 fd = open(filename, O_RDONLY);
153 if (fd < 0) {
154 log_err("fio: job file open: %s\n", strerror(errno));
155 return 1;
156 }
157
158 if (fstat(fd, &sb) < 0) {
159 log_err("fio: job file stat: %s\n", strerror(errno));
160 return 1;
161 }
162
163 buf = malloc(sb.st_size);
164
165 len = sb.st_size;
166 p = buf;
167 do {
168 ret = read(fd, p, len);
169 if (ret > 0) {
170 len -= ret;
171 if (!len)
172 break;
173 p += ret;
174 continue;
175 } else if (!ret)
176 break;
177 else if (errno == EAGAIN || errno == EINTR)
178 continue;
179 } while (1);
180
b66570dc 181 ret = send_file_buf(client, buf, sb.st_size);
132159a5
JA
182 free(buf);
183 return ret;
184}
37db14fe 185
a37f69b7
JA
186int fio_clients_send_ini(const char *filename)
187{
188 struct fio_client *client;
189 struct flist_head *entry, *tmp;
190
191 flist_for_each_safe(entry, tmp, &client_list) {
192 client = flist_entry(entry, struct fio_client, list);
193
194 if (fio_client_send_ini(client, filename))
195 remove_client(client);
196 }
197
198 return !nr_clients;
199}
200
b66570dc 201static int handle_client(struct fio_client *client)
37db14fe
JA
202{
203 struct fio_net_cmd *cmd;
204
b66570dc 205 while ((cmd = fio_net_cmd_read(client->fd)) != NULL) {
37db14fe
JA
206 if (cmd->opcode == FIO_NET_CMD_ACK) {
207 free(cmd);
208 continue;
209 }
437377e1 210 if (cmd->opcode == FIO_NET_CMD_QUIT) {
b66570dc 211 remove_client(client);
437377e1
JA
212 free(cmd);
213 break;
214 }
37db14fe
JA
215 if (cmd->opcode != FIO_NET_CMD_TEXT) {
216 printf("non text: %d\n", cmd->opcode);
217 free(cmd);
218 continue;
219 }
49d14c0f 220 fwrite(cmd->payload, cmd->pdu_len, 1, stdout);
37db14fe
JA
221 fflush(stdout);
222 free(cmd);
223 }
224
225 return 0;
226}
b66570dc
JA
227
228int fio_handle_clients(void)
229{
230 struct fio_client *client;
231 struct flist_head *entry;
232 struct pollfd *pfds;
82a4be1b 233 int i, ret = 0;
b66570dc
JA
234
235 pfds = malloc(nr_clients * sizeof(struct pollfd));
236
82a4be1b
JA
237 while (!exit_backend && nr_clients) {
238 i = 0;
239 flist_for_each(entry, &client_list) {
240 client = flist_entry(entry, struct fio_client, list);
b66570dc 241
82a4be1b
JA
242 pfds[i].fd = client->fd;
243 pfds[i].events = POLLIN;
244 i++;
245 }
246
247 assert(i == nr_clients);
b66570dc 248
b66570dc
JA
249 ret = poll(pfds, nr_clients, 100);
250 if (ret < 0) {
251 if (errno == EINTR)
252 continue;
253 log_err("fio: poll clients: %s\n", strerror(errno));
254 break;
255 } else if (!ret)
256 continue;
257
258 for (i = 0; i < nr_clients; i++) {
259 if (!(pfds[i].revents & POLLIN))
260 continue;
261
262 client = find_client_by_fd(pfds[i].fd);
263 if (!client) {
264 log_err("fio: unknown client\n");
265 continue;
266 }
267 handle_client(client);
268 }
269 }
270
271 free(pfds);
272
273 return 0;
274}