Add FD_NET debugging value
[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         flist_del(&client->list);
65         nr_clients--;
66         free(client->hostname);
67         free(client);
68 }
69
70 void fio_client_add(const char *hostname)
71 {
72         struct fio_client *client;
73
74         client = malloc(sizeof(*client));
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
82 static int fio_client_connect(struct fio_client *client)
83 {
84         int fd;
85
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
90         if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
91                 struct hostent *hent;
92
93                 hent = gethostbyname(client->hostname);
94                 if (!hent) {
95                         log_err("fio: gethostbyname: %s\n", strerror(errno));
96                         return 1;
97                 }
98
99                 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
100         }
101
102         fd = socket(AF_INET, SOCK_STREAM, 0);
103         if (fd < 0) {
104                 log_err("fio: socket: %s\n", strerror(errno));
105                 free(client);
106                 return 1;
107         }
108
109         if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
110                 log_err("fio: connect: %s\n", strerror(errno));
111                 free(client);
112                 return 1;
113         }
114
115         client->fd = fd;
116         return 0;
117 }
118
119 int 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
136 static int send_file_buf(struct fio_client *client, char *buf, off_t size)
137 {
138         return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
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  */
145 static int fio_client_send_ini(struct fio_client *client, const char *filename)
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
181         ret = send_file_buf(client, buf, sb.st_size);
182         free(buf);
183         return ret;
184 }
185
186 int 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
201 static int handle_client(struct fio_client *client)
202 {
203         struct fio_net_cmd *cmd;
204
205         while ((cmd = fio_net_cmd_read(client->fd)) != NULL) {
206                 if (cmd->opcode == FIO_NET_CMD_ACK) {
207                         free(cmd);
208                         continue;
209                 }
210                 if (cmd->opcode == FIO_NET_CMD_QUIT) {
211                         remove_client(client);
212                         free(cmd);
213                         break;
214                 }
215                 if (cmd->opcode != FIO_NET_CMD_TEXT) {
216                         printf("non text: %d\n", cmd->opcode);
217                         free(cmd);
218                         continue;
219                 }
220                 fwrite(cmd->payload, cmd->pdu_len, 1, stdout);
221                 fflush(stdout);
222                 free(cmd);
223         }
224
225         return 0;
226 }
227
228 int fio_handle_clients(void)
229 {
230         struct fio_client *client;
231         struct flist_head *entry;
232         struct pollfd *pfds;
233         int i, ret = 0;
234
235         pfds = malloc(nr_clients * sizeof(struct pollfd));
236
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);
241
242                         pfds[i].fd = client->fd;
243                         pfds[i].events = POLLIN;
244                         i++;
245                 }
246
247                 assert(i == nr_clients);
248
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 }