blob: 368ad9e7bed81a9ac550fc1eb7f5efef182c65c3 [file] [log] [blame]
Jens Axboe132159a2011-09-30 15:01:32 -06001#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"
Jens Axboeb66570d2011-10-01 11:11:35 -060019#include "flist.h"
Jens Axboe132159a2011-09-30 15:01:32 -060020
Jens Axboeb66570d2011-10-01 11:11:35 -060021struct 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}
Jens Axboe132159a2011-09-30 15:01:32 -060069
70int fio_client_connect(const char *host)
71{
Jens Axboeb66570d2011-10-01 11:11:35 -060072 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -060073 int fd;
74
Jens Axboeb66570d2011-10-01 11:11:35 -060075 client = malloc(sizeof(*client));
Jens Axboe132159a2011-09-30 15:01:32 -060076
Jens Axboeb66570d2011-10-01 11:11:35 -060077 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) {
Jens Axboe132159a2011-09-30 15:01:32 -060082 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
Jens Axboeb66570d2011-10-01 11:11:35 -060090 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
Jens Axboe132159a2011-09-30 15:01:32 -060091 }
92
93 fd = socket(AF_INET, SOCK_STREAM, 0);
94 if (fd < 0) {
95 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboeb66570d2011-10-01 11:11:35 -060096 free(client);
Jens Axboe132159a2011-09-30 15:01:32 -060097 return 1;
98 }
99
Jens Axboeb66570d2011-10-01 11:11:35 -0600100 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600101 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboeb66570d2011-10-01 11:11:35 -0600102 free(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600103 return 1;
104 }
105
Jens Axboeb66570d2011-10-01 11:11:35 -0600106 client->hostname = strdup(host);
107 flist_add(&client->list, &client_list);
108 nr_clients++;
109 client->fd = fd;
Jens Axboe132159a2011-09-30 15:01:32 -0600110 return 0;
111}
112
Jens Axboeb66570d2011-10-01 11:11:35 -0600113static int send_file_buf(struct fio_client *client, char *buf, off_t size)
Jens Axboe132159a2011-09-30 15:01:32 -0600114{
Jens Axboeb66570d2011-10-01 11:11:35 -0600115 return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
Jens Axboe132159a2011-09-30 15:01:32 -0600116}
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 */
Jens Axboeb66570d2011-10-01 11:11:35 -0600122int fio_client_send_ini(const char *hostname, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600123{
Jens Axboeb66570d2011-10-01 11:11:35 -0600124 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600125 struct stat sb;
126 char *p, *buf;
127 off_t len;
128 int fd, ret;
129
Jens Axboeb66570d2011-10-01 11:11:35 -0600130 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
Jens Axboe132159a2011-09-30 15:01:32 -0600136 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
Jens Axboeb66570d2011-10-01 11:11:35 -0600165 ret = send_file_buf(client, buf, sb.st_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600166 free(buf);
167 return ret;
168}
Jens Axboe37db14f2011-09-30 17:00:42 -0600169
Jens Axboeb66570d2011-10-01 11:11:35 -0600170static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600171{
172 struct fio_net_cmd *cmd;
173
Jens Axboeb66570d2011-10-01 11:11:35 -0600174 while ((cmd = fio_net_cmd_read(client->fd)) != NULL) {
Jens Axboe37db14f2011-09-30 17:00:42 -0600175 if (cmd->opcode == FIO_NET_CMD_ACK) {
176 free(cmd);
177 continue;
178 }
Jens Axboe437377e2011-10-01 08:31:30 -0600179 if (cmd->opcode == FIO_NET_CMD_QUIT) {
Jens Axboeb66570d2011-10-01 11:11:35 -0600180 remove_client(client);
Jens Axboe437377e2011-10-01 08:31:30 -0600181 free(cmd);
182 break;
183 }
Jens Axboe37db14f2011-09-30 17:00:42 -0600184 if (cmd->opcode != FIO_NET_CMD_TEXT) {
185 printf("non text: %d\n", cmd->opcode);
186 free(cmd);
187 continue;
188 }
Jens Axboe49d14c02011-10-01 00:08:39 -0600189 fwrite(cmd->payload, cmd->pdu_len, 1, stdout);
Jens Axboe37db14f2011-09-30 17:00:42 -0600190 fflush(stdout);
191 free(cmd);
192 }
193
194 return 0;
195}
Jens Axboeb66570d2011-10-01 11:11:35 -0600196
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}