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