client: initial support for multiple connections
[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);
29static unsigned int nr_clients;
30
31static struct fio_client *find_client_by_fd(int fd)
32{
33 struct fio_client *client;
34 struct flist_head *entry;
35
36 flist_for_each(entry, &client_list) {
37 client = flist_entry(entry, struct fio_client, list);
38
39 if (client->fd == fd)
40 return client;
41 }
42
43 return NULL;
44}
45
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}
60
61static void remove_client(struct fio_client *client)
62{
63 printf("remove client %s\n", client->hostname);
64 flist_del(&client->list);
65 nr_clients--;
66 free(client->hostname);
67 free(client);
68}
132159a5
JA
69
70int fio_client_connect(const char *host)
71{
b66570dc 72 struct fio_client *client;
132159a5
JA
73 int fd;
74
b66570dc 75 client = malloc(sizeof(*client));
132159a5 76
b66570dc
JA
77 memset(&client->addr, 0, sizeof(client->addr));
78 client->addr.sin_family = AF_INET;
79 client->addr.sin_port = htons(fio_net_port);
80
81 if (inet_aton(host, &client->addr.sin_addr) != 1) {
132159a5
JA
82 struct hostent *hent;
83
84 hent = gethostbyname(host);
85 if (!hent) {
86 log_err("fio: gethostbyname: %s\n", strerror(errno));
87 return 1;
88 }
89
b66570dc 90 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
132159a5
JA
91 }
92
93 fd = socket(AF_INET, SOCK_STREAM, 0);
94 if (fd < 0) {
95 log_err("fio: socket: %s\n", strerror(errno));
b66570dc 96 free(client);
132159a5
JA
97 return 1;
98 }
99
b66570dc 100 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
132159a5 101 log_err("fio: connect: %s\n", strerror(errno));
b66570dc 102 free(client);
132159a5
JA
103 return 1;
104 }
105
b66570dc
JA
106 client->hostname = strdup(host);
107 flist_add(&client->list, &client_list);
108 nr_clients++;
109 client->fd = fd;
132159a5
JA
110 return 0;
111}
112
b66570dc 113static int send_file_buf(struct fio_client *client, char *buf, off_t size)
132159a5 114{
b66570dc 115 return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
132159a5
JA
116}
117
118/*
119 * Send file contents to server backend. We could use sendfile(), but to remain
120 * more portable lets just read/write the darn thing.
121 */
b66570dc 122int fio_client_send_ini(const char *hostname, const char *filename)
132159a5 123{
b66570dc 124 struct fio_client *client;
132159a5
JA
125 struct stat sb;
126 char *p, *buf;
127 off_t len;
128 int fd, ret;
129
b66570dc
JA
130 client = find_client_by_name(hostname);
131 if (!client) {
132 log_err("fio: can't find client for host %s\n", hostname);
133 return 1;
134 }
135
132159a5
JA
136 fd = open(filename, O_RDONLY);
137 if (fd < 0) {
138 log_err("fio: job file open: %s\n", strerror(errno));
139 return 1;
140 }
141
142 if (fstat(fd, &sb) < 0) {
143 log_err("fio: job file stat: %s\n", strerror(errno));
144 return 1;
145 }
146
147 buf = malloc(sb.st_size);
148
149 len = sb.st_size;
150 p = buf;
151 do {
152 ret = read(fd, p, len);
153 if (ret > 0) {
154 len -= ret;
155 if (!len)
156 break;
157 p += ret;
158 continue;
159 } else if (!ret)
160 break;
161 else if (errno == EAGAIN || errno == EINTR)
162 continue;
163 } while (1);
164
b66570dc 165 ret = send_file_buf(client, buf, sb.st_size);
132159a5
JA
166 free(buf);
167 return ret;
168}
37db14fe 169
b66570dc 170static int handle_client(struct fio_client *client)
37db14fe
JA
171{
172 struct fio_net_cmd *cmd;
173
b66570dc 174 while ((cmd = fio_net_cmd_read(client->fd)) != NULL) {
37db14fe
JA
175 if (cmd->opcode == FIO_NET_CMD_ACK) {
176 free(cmd);
177 continue;
178 }
437377e1 179 if (cmd->opcode == FIO_NET_CMD_QUIT) {
b66570dc 180 remove_client(client);
437377e1
JA
181 free(cmd);
182 break;
183 }
37db14fe
JA
184 if (cmd->opcode != FIO_NET_CMD_TEXT) {
185 printf("non text: %d\n", cmd->opcode);
186 free(cmd);
187 continue;
188 }
49d14c0f 189 fwrite(cmd->payload, cmd->pdu_len, 1, stdout);
37db14fe
JA
190 fflush(stdout);
191 free(cmd);
192 }
193
194 return 0;
195}
b66570dc
JA
196
197int fio_handle_clients(void)
198{
199 struct fio_client *client;
200 struct flist_head *entry;
201 struct pollfd *pfds;
202 int i = 0, ret = 0;
203
204 pfds = malloc(nr_clients * sizeof(struct pollfd));
205
206 flist_for_each(entry, &client_list) {
207 client = flist_entry(entry, struct fio_client, list);
208
209 pfds[i].fd = client->fd;
210 pfds[i].events = POLLIN;
211 i++;
212 }
213
214 while (!exit_backend && nr_clients) {
215 ret = poll(pfds, nr_clients, 100);
216 if (ret < 0) {
217 if (errno == EINTR)
218 continue;
219 log_err("fio: poll clients: %s\n", strerror(errno));
220 break;
221 } else if (!ret)
222 continue;
223
224 for (i = 0; i < nr_clients; i++) {
225 if (!(pfds[i].revents & POLLIN))
226 continue;
227
228 client = find_client_by_fd(pfds[i].fd);
229 if (!client) {
230 log_err("fio: unknown client\n");
231 continue;
232 }
233 handle_client(client);
234 }
235 }
236
237 free(pfds);
238
239 return 0;
240}