blob: 73e36a506641af0dcff0b4ae812c48a3e742513a [file] [log] [blame]
Jens Axboe50d16972011-09-29 17:45:28 -06001#include <stdio.h>
2#include <stdlib.h>
Jens Axboe142575e2011-09-30 17:35:45 -06003#include <stdarg.h>
Jens Axboe50d16972011-09-29 17:45:28 -06004#include <unistd.h>
5#include <limits.h>
Jens Axboe50d16972011-09-29 17:45:28 -06006#include <errno.h>
7#include <fcntl.h>
8#include <sys/poll.h>
Jens Axboe50d16972011-09-29 17:45:28 -06009#include <sys/types.h>
10#include <sys/wait.h>
Jens Axboed05c4a02011-10-05 00:16:11 +020011#include <sys/socket.h>
Jens Axboe87aa8f12011-10-06 21:24:13 +020012#include <sys/stat.h>
13#include <sys/un.h>
Jens Axboe50d16972011-09-29 17:45:28 -060014#include <netinet/in.h>
15#include <arpa/inet.h>
16#include <netdb.h>
Jens Axboee46d8092011-10-03 09:11:02 +020017#include <syslog.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020018#include <signal.h>
Jens Axboe1b427252012-03-14 15:03:03 +010019#include <zlib.h>
Jens Axboe50d16972011-09-29 17:45:28 -060020
21#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060022#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060023#include "crc/crc16.h"
Jens Axboec7c6cb42011-10-13 14:12:40 +020024#include "lib/ieee754.h"
Jens Axboe50d16972011-09-29 17:45:28 -060025
Jens Axboe89cf1482011-10-07 10:10:18 +020026#include "fio_version.h"
27
Stephen M. Cameron5adc2442012-02-24 08:17:31 +010028int fio_net_port = FIO_NET_PORT;
Jens Axboe50d16972011-09-29 17:45:28 -060029
Jens Axboe009b1be2011-09-29 18:27:02 -060030int exit_backend = 0;
31
Jens Axboe46c48f12011-10-01 12:50:51 -060032static int server_fd = -1;
Jens Axboe87aa8f12011-10-06 21:24:13 +020033static char *fio_server_arg;
34static char *bind_sock;
35static struct sockaddr_in saddr_in;
Jens Axboe811826b2011-10-24 09:11:50 +020036static struct sockaddr_in6 saddr_in6;
Jens Axboe01be0382011-10-15 14:43:41 +020037static int first_cmd_check;
Jens Axboe811826b2011-10-24 09:11:50 +020038static int use_ipv6;
Jens Axboe37db14f2011-09-30 17:00:42 -060039
Jens Axboe89c17072011-10-11 10:15:51 +020040static const char *fio_server_ops[FIO_NET_CMD_NR] = {
41 "",
42 "QUIT",
43 "EXIT",
44 "JOB",
45 "JOBLINE",
46 "TEXT",
47 "TS",
48 "GS",
49 "SEND_ETA",
50 "ETA",
51 "PROBE",
52 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020053 "STOP",
54 "DISK_UTIL",
Jens Axboeb9d2f302012-03-08 20:36:28 +010055 "SERVER_START",
Jens Axboe807f9972012-03-02 10:25:24 +010056 "ADD_JOB",
Jens Axboeb9d2f302012-03-08 20:36:28 +010057 "CMD_RUN"
Jens Axboe89c17072011-10-11 10:15:51 +020058};
59
60const char *fio_server_op(unsigned int op)
61{
62 static char buf[32];
63
64 if (op < FIO_NET_CMD_NR)
65 return fio_server_ops[op];
66
67 sprintf(buf, "UNKNOWN/%d", op);
68 return buf;
69}
70
Jens Axboe5235f622012-03-14 21:25:51 +010071static ssize_t iov_total_len(const struct iovec *iov, int count)
Jens Axboe132159a2011-09-30 15:01:32 -060072{
Jens Axboe5235f622012-03-14 21:25:51 +010073 ssize_t ret = 0;
74
75 while (count--) {
76 ret += iov->iov_len;
77 iov++;
78 }
79
80 return ret;
81}
82
83static int fio_sendv_data(int sk, struct iovec *iov, int count)
84{
85 ssize_t total_len = iov_total_len(iov, count);
86 ssize_t ret;
Jens Axboe794d69c2011-10-01 08:48:50 -060087
Jens Axboe132159a2011-09-30 15:01:32 -060088 do {
Jens Axboe5235f622012-03-14 21:25:51 +010089 ret = writev(sk, iov, count);
Jens Axboe132159a2011-09-30 15:01:32 -060090 if (ret > 0) {
Jens Axboe5235f622012-03-14 21:25:51 +010091 total_len -= ret;
92 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -060093 break;
Jens Axboe5235f622012-03-14 21:25:51 +010094
95 while (ret) {
96 if (ret >= iov->iov_len) {
97 ret -= iov->iov_len;
98 iov++;
99 continue;
100 }
101 iov->iov_base += ret;
102 iov->iov_len -= ret;
103 ret = 0;
104 }
Jens Axboe132159a2011-09-30 15:01:32 -0600105 } else if (!ret)
106 break;
107 else if (errno == EAGAIN || errno == EINTR)
108 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200109 else
110 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600111 } while (!exit_backend);
112
Jens Axboe5235f622012-03-14 21:25:51 +0100113 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600114 return 0;
115
Jens Axboe8b4e61b2012-03-09 17:10:54 +0100116 if (errno)
117 return -errno;
118
Jens Axboe132159a2011-09-30 15:01:32 -0600119 return 1;
120}
121
Jens Axboe5235f622012-03-14 21:25:51 +0100122int fio_send_data(int sk, const void *p, unsigned int len)
123{
124 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
125
126 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
127
128 return fio_sendv_data(sk, &iov, 1);
129}
130
Jens Axboe132159a2011-09-30 15:01:32 -0600131int fio_recv_data(int sk, void *p, unsigned int len)
132{
133 do {
134 int ret = recv(sk, p, len, MSG_WAITALL);
135
136 if (ret > 0) {
137 len -= ret;
138 if (!len)
139 break;
140 p += ret;
141 continue;
142 } else if (!ret)
143 break;
144 else if (errno == EAGAIN || errno == EINTR)
145 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200146 else
147 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600148 } while (!exit_backend);
149
150 if (!len)
151 return 0;
152
153 return -1;
154}
155
156static int verify_convert_cmd(struct fio_net_cmd *cmd)
157{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600158 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600159
Jens Axboefcee5ff2011-09-30 22:50:39 -0600160 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
161 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600162
Jens Axboe25dfa842012-02-29 10:01:34 +0100163 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600164 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600165 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600166 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600167 return 1;
168 }
169
170 cmd->version = le16_to_cpu(cmd->version);
171 cmd->opcode = le16_to_cpu(cmd->opcode);
172 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200173 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600174 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
175
176 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200177 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600178 break;
179 default:
180 log_err("fio: bad server cmd version %d\n", cmd->version);
181 return 1;
182 }
183
Jens Axboeb9d2f302012-03-08 20:36:28 +0100184 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600185 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
186 return 1;
187 }
188
189 return 0;
190}
191
Jens Axboea64e88d2011-10-03 14:20:01 +0200192/*
193 * Read (and defragment, if necessary) incoming commands
194 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200195struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600196{
Jens Axboea64e88d2011-10-03 14:20:01 +0200197 struct fio_net_cmd cmd, *cmdret = NULL;
198 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600199 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200200 int ret, first = 1;
201 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600202
Jens Axboea64e88d2011-10-03 14:20:01 +0200203 do {
204 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
205 if (ret)
206 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600207
Jens Axboea64e88d2011-10-03 14:20:01 +0200208 /* We have a command, verify it and swap if need be */
209 ret = verify_convert_cmd(&cmd);
210 if (ret)
211 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600212
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200213 if (first) {
214 /* if this is text, add room for \0 at the end */
215 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
216 assert(!cmdret);
217 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200218 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600219
Jens Axboea64e88d2011-10-03 14:20:01 +0200220 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600221
Jens Axboea64e88d2011-10-03 14:20:01 +0200222 if (first)
223 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200224 else if (cmdret->opcode != cmd.opcode) {
225 log_err("fio: fragment opcode mismatch (%d != %d)\n",
226 cmdret->opcode, cmd.opcode);
227 ret = 1;
228 break;
229 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200230
231 if (!cmd.pdu_len)
232 break;
233
234 /* There's payload, get it */
235 pdu = (void *) cmdret->payload + pdu_offset;
236 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
237 if (ret)
238 break;
239
240 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100241 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200242 if (crc != cmd.pdu_crc16) {
243 log_err("fio: server bad crc on payload ");
244 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
245 ret = 1;
246 break;
247 }
248
249 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200250 if (!first)
251 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200252 first = 0;
253 } while (cmd.flags & FIO_NET_CMD_F_MORE);
254
255 if (ret) {
256 free(cmdret);
257 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200258 } else if (cmdret) {
259 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100260 if (cmdret->pdu_len) {
261 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
262 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
263 char *buf = (char *) pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200264
Jens Axboe084d1c62012-03-03 20:28:07 +0100265 buf[pdu->buf_len ] = '\0';
266 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
Jens Axboe46bcd492012-03-14 11:31:21 +0100267 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
268 char *buf = (char *) pdu->buf;
269 int len = le32_to_cpu(pdu->buf_len);
Jens Axboe084d1c62012-03-03 20:28:07 +0100270
Jens Axboe46bcd492012-03-14 11:31:21 +0100271 buf[len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100272 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200273 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100274
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200275 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200276 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200277 }
Jens Axboe132159a2011-09-30 15:01:32 -0600278
Jens Axboea64e88d2011-10-03 14:20:01 +0200279 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600280}
281
Jens Axboe1b427252012-03-14 15:03:03 +0100282void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, void *pdu)
Jens Axboe132159a2011-09-30 15:01:32 -0600283{
284 uint32_t pdu_len;
285
Jens Axboe25dfa842012-02-29 10:01:34 +0100286 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600287
288 pdu_len = le32_to_cpu(cmd->pdu_len);
Jens Axboe1b427252012-03-14 15:03:03 +0100289 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
290}
291
292void fio_net_cmd_crc(struct fio_net_cmd *cmd)
293{
294 fio_net_cmd_crc_pdu(cmd, cmd->payload);
Jens Axboe132159a2011-09-30 15:01:32 -0600295}
296
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200297int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
298 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600299{
Jens Axboe7f868312011-10-10 09:55:21 +0200300 struct fio_net_cmd *cmd = NULL;
301 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600302 int ret;
303
304 do {
305 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100306 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
307 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600308
Jens Axboe7f868312011-10-10 09:55:21 +0200309 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
310 if (cmd)
311 free(cmd);
312
313 cur_len = sizeof(*cmd) + this_len;
314 cmd = malloc(cur_len);
315 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600316
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200317 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600318
319 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200320 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600321
322 fio_net_cmd_crc(cmd);
323
324 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600325 size -= this_len;
326 buf += this_len;
327 } while (!ret && size);
328
Jens Axboe7f868312011-10-10 09:55:21 +0200329 if (cmd)
330 free(cmd);
331
Jens Axboe794d69c2011-10-01 08:48:50 -0600332 return ret;
333}
334
Jens Axboe89c17072011-10-11 10:15:51 +0200335static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600336{
Jens Axboe178cde92011-10-05 22:14:31 +0200337 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600338
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200339 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600340 fio_net_cmd_crc(&cmd);
341
342 return fio_send_data(sk, &cmd, sizeof(cmd));
343}
344
Jens Axboe89c17072011-10-11 10:15:51 +0200345/*
346 * If 'list' is non-NULL, then allocate and store the sent command for
347 * later verification.
348 */
349int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
350 struct flist_head *list)
351{
352 struct fio_net_int_cmd *cmd;
353 int ret;
354
355 if (!list)
356 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
357
358 cmd = malloc(sizeof(*cmd));
359
Jens Axboedf380932011-10-11 14:25:08 +0200360 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
Jens Axboe89c17072011-10-11 10:15:51 +0200361 fio_net_cmd_crc(&cmd->cmd);
362
363 INIT_FLIST_HEAD(&cmd->list);
364 gettimeofday(&cmd->tv, NULL);
365 cmd->saved_tag = tag;
366
367 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
368 if (ret) {
369 free(cmd);
370 return ret;
371 }
372
373 flist_add_tail(&cmd->list, list);
374 return 0;
375}
376
Jens Axboe9abea482011-10-04 13:21:54 +0200377static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600378{
Jens Axboe46c48f12011-10-01 12:50:51 -0600379 dprint(FD_NET, "server: sending quit\n");
Jens Axboe89c17072011-10-11 10:15:51 +0200380 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600381}
382
Jens Axboeb9d2f302012-03-08 20:36:28 +0100383static int handle_run_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600384{
Jens Axboe11e950b2011-10-16 21:34:14 +0200385 struct cmd_end_pdu epdu;
Jens Axboea64e88d2011-10-03 14:20:01 +0200386 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600387
Jens Axboe2e1df072012-02-09 11:15:02 +0100388 ret = fio_backend();
Jens Axboe11e950b2011-10-16 21:34:14 +0200389
390 epdu.error = ret;
391 fio_net_send_cmd(server_fd, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0);
392
Jens Axboe9abea482011-10-04 13:21:54 +0200393 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200394 reset_fio_state();
Jens Axboe8663ea62012-03-02 14:04:30 +0100395 first_cmd_check = 0;
Jens Axboe81179ee2011-10-04 12:42:06 +0200396 return ret;
397}
398
Jens Axboeb9d2f302012-03-08 20:36:28 +0100399static int handle_job_cmd(struct fio_net_cmd *cmd)
400{
Jens Axboe46bcd492012-03-14 11:31:21 +0100401 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
402 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100403 struct cmd_start_pdu spdu;
404
Jens Axboe46bcd492012-03-14 11:31:21 +0100405 pdu->buf_len = le32_to_cpu(pdu->buf_len);
406 pdu->client_type = le32_to_cpu(pdu->client_type);
407
408 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboeb9d2f302012-03-08 20:36:28 +0100409 fio_server_send_quit_cmd();
410 return -1;
411 }
412
413 spdu.jobs = cpu_to_le32(thread_number);
414 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
415 return 0;
416}
417
Jens Axboe81179ee2011-10-04 12:42:06 +0200418static int handle_jobline_cmd(struct fio_net_cmd *cmd)
419{
Jens Axboefa2ea802011-10-08 21:07:29 +0200420 void *pdu = cmd->payload;
421 struct cmd_single_line_pdu *cslp;
422 struct cmd_line_pdu *clp;
423 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100424 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200425 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100426 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200427
Jens Axboefa2ea802011-10-08 21:07:29 +0200428 clp = pdu;
429 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100430 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200431 argv = malloc(clp->lines * sizeof(char *));
432 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200433
Jens Axboefa2ea802011-10-08 21:07:29 +0200434 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200435
Jens Axboefa2ea802011-10-08 21:07:29 +0200436 for (i = 0; i < clp->lines; i++) {
437 cslp = pdu + offset;
438 argv[i] = (char *) cslp->text;
439
440 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200441 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
442 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200443
Jens Axboe46bcd492012-03-14 11:31:21 +0100444 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200445 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200446 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200447 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200448 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200449
Jens Axboefa2ea802011-10-08 21:07:29 +0200450 free(argv);
451
Jens Axboeb9d2f302012-03-08 20:36:28 +0100452 spdu.jobs = cpu_to_le32(thread_number);
453 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
454 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600455}
456
Jens Axboec28e8e82011-10-04 08:57:39 +0200457static int handle_probe_cmd(struct fio_net_cmd *cmd)
458{
459 struct cmd_probe_pdu probe;
460
Jens Axboe89c17072011-10-11 10:15:51 +0200461 dprint(FD_NET, "server: sending probe reply\n");
462
Jens Axboec28e8e82011-10-04 08:57:39 +0200463 memset(&probe, 0, sizeof(probe));
464 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200465#ifdef FIO_BIG_ENDIAN
466 probe.bigendian = 1;
467#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200468 probe.fio_major = FIO_MAJOR;
469 probe.fio_minor = FIO_MINOR;
470 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200471
Jens Axboecca84642011-10-07 12:47:57 +0200472 probe.os = FIO_OS;
473 probe.arch = FIO_ARCH;
474
Jens Axboe38fdef22011-10-11 14:30:06 +0200475 probe.bpp = sizeof(void *);
476
Jens Axboe89c17072011-10-11 10:15:51 +0200477 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200478}
479
480static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
481{
482 struct jobs_eta *je;
483 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200484 int i;
485
Jens Axboeb814fb22011-10-12 09:20:34 +0200486 if (!thread_number)
487 return 0;
488
489 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200490 je = malloc(size);
491 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200492
493 if (!calc_thread_status(je, 1)) {
494 free(je);
495 return 0;
496 }
497
498 dprint(FD_NET, "server sending status\n");
499
500 je->nr_running = cpu_to_le32(je->nr_running);
501 je->nr_ramp = cpu_to_le32(je->nr_ramp);
502 je->nr_pending = cpu_to_le32(je->nr_pending);
503 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200504
505 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100506 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
507 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
508 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
509 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200510 je->rate[i] = cpu_to_le32(je->rate[i]);
511 je->iops[i] = cpu_to_le32(je->iops[i]);
512 }
513
Jens Axboec1970b92012-03-06 15:37:40 +0100514 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200515 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100516 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200517
Jens Axboe7f868312011-10-10 09:55:21 +0200518 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200519 free(je);
520 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200521}
522
Jens Axboe132159a2011-09-30 15:01:32 -0600523static int handle_command(struct fio_net_cmd *cmd)
524{
525 int ret;
526
Jens Axboe89c17072011-10-11 10:15:51 +0200527 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
528 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600529
Jens Axboe132159a2011-09-30 15:01:32 -0600530 switch (cmd->opcode) {
531 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200532 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200533 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200534 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600535 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200536 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600537 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200538 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600539 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200540 case FIO_NET_CMD_JOBLINE:
541 ret = handle_jobline_cmd(cmd);
542 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200543 case FIO_NET_CMD_PROBE:
544 ret = handle_probe_cmd(cmd);
545 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200546 case FIO_NET_CMD_SEND_ETA:
547 ret = handle_send_eta_cmd(cmd);
548 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100549 case FIO_NET_CMD_RUN:
550 ret = handle_run_cmd(cmd);
551 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600552 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200553 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600554 ret = 1;
555 }
556
557 return ret;
558}
559
Jens Axboe70e0c312011-10-04 09:18:30 +0200560static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600561{
562 struct fio_net_cmd *cmd = NULL;
563 int ret = 0;
564
565 /* read forever */
566 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200567 struct pollfd pfd = {
568 .fd = sk,
569 .events = POLLIN,
570 };
571
572 ret = 0;
573 do {
574 ret = poll(&pfd, 1, 100);
575 if (ret < 0) {
576 if (errno == EINTR)
577 break;
578 log_err("fio: poll: %s\n", strerror(errno));
579 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200580 } else if (!ret) {
581 if (!block)
582 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200583 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200584 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200585
586 if (pfd.revents & POLLIN)
587 break;
588 if (pfd.revents & (POLLERR|POLLHUP)) {
589 ret = 1;
590 break;
591 }
Jens Axboe19c65172011-10-05 22:05:37 +0200592 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200593
594 if (ret < 0)
595 break;
596
597 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600598 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200599 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600600 break;
601 }
602
Jens Axboe132159a2011-09-30 15:01:32 -0600603 ret = handle_command(cmd);
604 if (ret)
605 break;
606
607 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400608 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600609 }
610
611 if (cmd)
612 free(cmd);
613
614 return ret;
615}
616
Jens Axboecc0df002011-10-03 20:53:32 +0200617void fio_server_idle_loop(void)
618{
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100619 if (!first_cmd_check) {
Jens Axboe5d7793a2012-03-08 19:59:16 +0100620 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100621 first_cmd_check = 1;
622 }
Jens Axboecc0df002011-10-03 20:53:32 +0200623 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200624 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200625}
626
Jens Axboe50d16972011-09-29 17:45:28 -0600627static int accept_loop(int listen_sk)
628{
Jens Axboebb447a22011-10-04 15:06:42 +0200629 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200630 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600631 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600632 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600633
Jens Axboe60efd142011-10-04 13:30:11 +0200634 dprint(FD_NET, "server enter accept loop\n");
635
Jens Axboe009b1be2011-09-29 18:27:02 -0600636 flags = fcntl(listen_sk, F_GETFL);
637 flags |= O_NONBLOCK;
638 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600639again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600640 pfd.fd = listen_sk;
641 pfd.events = POLLIN;
642 do {
643 ret = poll(&pfd, 1, 100);
644 if (ret < 0) {
645 if (errno == EINTR)
646 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600647 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600648 goto out;
649 } else if (!ret)
650 continue;
651
652 if (pfd.revents & POLLIN)
653 break;
654 } while (!exit_backend);
655
656 if (exit_backend)
657 goto out;
658
Jens Axboe6b976bf2011-10-04 15:07:43 +0200659 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600660 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600661 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600662 return -1;
663 }
664
Jens Axboebb447a22011-10-04 15:06:42 +0200665 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600666
Jens Axboe37db14f2011-09-30 17:00:42 -0600667 server_fd = sk;
668
Jens Axboe70e0c312011-10-04 09:18:30 +0200669 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600670
Jens Axboe37db14f2011-09-30 17:00:42 -0600671 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600672 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600673
Jens Axboe009b1be2011-09-29 18:27:02 -0600674 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600675 goto again;
676
Jens Axboe009b1be2011-09-29 18:27:02 -0600677out:
Jens Axboe132159a2011-09-30 15:01:32 -0600678 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600679}
680
Jens Axboe084d1c62012-03-03 20:28:07 +0100681int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600682{
Jens Axboe084d1c62012-03-03 20:28:07 +0100683 struct cmd_text_pdu *pdu;
684 unsigned int tlen;
685 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600686
Jens Axboe084d1c62012-03-03 20:28:07 +0100687 if (server_fd == -1)
688 return log_local_buf(buf, len);
689
690 tlen = sizeof(*pdu) + len;
691 pdu = malloc(tlen);
692
693 pdu->level = __cpu_to_le32(level);
694 pdu->buf_len = __cpu_to_le32(len);
695
696 gettimeofday(&tv, NULL);
697 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
698 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
699
700 memcpy(pdu->buf, buf, len);
701
702 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, 0);
703 free(pdu);
704 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600705}
706
Jens Axboea64e88d2011-10-03 14:20:01 +0200707static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
708{
709 dst->max_val = cpu_to_le64(src->max_val);
710 dst->min_val = cpu_to_le64(src->min_val);
711 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200712
713 /*
714 * Encode to IEEE 754 for network transfer
715 */
716 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
717 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200718}
719
720static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
721{
722 int i;
723
724 for (i = 0; i < 2; i++) {
725 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
726 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
727 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
728 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
729 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
730 dst->agg[i] = cpu_to_le64(src->agg[i]);
731 }
732
733 dst->kb_base = cpu_to_le32(src->kb_base);
734 dst->groupid = cpu_to_le32(src->groupid);
735}
736
737/*
738 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
739 * into a single payload.
740 */
741void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
742{
743 struct cmd_ts_pdu p;
744 int i, j;
745
Jens Axboe60efd142011-10-04 13:30:11 +0200746 dprint(FD_NET, "server sending end stats\n");
747
Jens Axboe317b3c82011-10-03 21:47:27 +0200748 memset(&p, 0, sizeof(p));
749
Jens Axboea64e88d2011-10-03 14:20:01 +0200750 strcpy(p.ts.name, ts->name);
751 strcpy(p.ts.verror, ts->verror);
752 strcpy(p.ts.description, ts->description);
753
Jens Axboeddcc0b62011-10-03 14:45:27 +0200754 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200755 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200756 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200757 p.ts.members = cpu_to_le32(ts->members);
758
759 for (i = 0; i < 2; i++) {
760 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
761 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
762 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
763 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
764 }
765
766 p.ts.usr_time = cpu_to_le64(ts->usr_time);
767 p.ts.sys_time = cpu_to_le64(ts->sys_time);
768 p.ts.ctx = cpu_to_le64(ts->ctx);
769 p.ts.minf = cpu_to_le64(ts->minf);
770 p.ts.majf = cpu_to_le64(ts->majf);
771 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200772
773 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200774 fio_fp64_t *src = &ts->percentile_list[i];
775 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200776
Jens Axboecfc03e42011-10-12 21:20:42 +0200777 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200778 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200779
780 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
781 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
782 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
783 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
784 }
785
786 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
787 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
788 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
789 }
790
791 for (i = 0; i < 2; i++)
792 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
793 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
794
795 for (i = 0; i < 3; i++) {
796 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200797 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200798 }
799
Jens Axboe93eee042011-10-03 21:08:48 +0200800 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200801 p.ts.total_complete = cpu_to_le64(ts->total_complete);
802
803 for (i = 0; i < 2; i++) {
804 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
805 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
806 }
807
808 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
809 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
810 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200811 p.ts.first_error = cpu_to_le32(ts->first_error);
812 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200813
814 convert_gs(&p.rs, rs);
815
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200816 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200817}
818
819void fio_server_send_gs(struct group_run_stats *rs)
820{
821 struct group_run_stats gs;
822
Jens Axboe60efd142011-10-04 13:30:11 +0200823 dprint(FD_NET, "server sending group run stats\n");
824
Jens Axboea64e88d2011-10-03 14:20:01 +0200825 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200826 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200827}
828
Jens Axboed09a64a2011-10-13 11:38:56 +0200829static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
830{
831 int i;
832
833 for (i = 0; i < 2; i++) {
834 dst->ios[i] = cpu_to_le32(src->ios[i]);
835 dst->merges[i] = cpu_to_le32(src->merges[i]);
836 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
837 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
838 }
839
840 dst->io_ticks = cpu_to_le32(src->io_ticks);
841 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
842 dst->slavecount = cpu_to_le32(src->slavecount);
843 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
844}
845
846static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
847{
848 int i;
849
850 strcpy((char *) dst->name, (char *) src->name);
851
852 for (i = 0; i < 2; i++) {
853 dst->ios[i] = cpu_to_le32(src->ios[i]);
854 dst->merges[i] = cpu_to_le32(src->merges[i]);
855 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
856 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
857 }
858
859 dst->io_ticks = cpu_to_le32(src->io_ticks);
860 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
861 dst->msec = cpu_to_le64(src->msec);
862}
863
864void fio_server_send_du(void)
865{
866 struct disk_util *du;
867 struct flist_head *entry;
868 struct cmd_du_pdu pdu;
869
870 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
871
Jens Axboe0766d922011-10-13 12:02:08 +0200872 memset(&pdu, 0, sizeof(pdu));
873
Jens Axboed09a64a2011-10-13 11:38:56 +0200874 flist_for_each(entry, &disk_list) {
875 du = flist_entry(entry, struct disk_util, list);
876
877 convert_dus(&pdu.dus, &du->dus);
878 convert_agg(&pdu.agg, &du->agg);
879
880 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
881 }
882}
883
Jens Axboe1b427252012-03-14 15:03:03 +0100884int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
885{
Jens Axboef5ed7652012-03-14 16:28:07 +0100886 struct cmd_iolog_pdu pdu;
Jens Axboe1b427252012-03-14 15:03:03 +0100887 struct fio_net_cmd cmd;
888 z_stream stream;
889 void *out_pdu;
Jens Axboe1b427252012-03-14 15:03:03 +0100890 int i;
891
Jens Axboef5ed7652012-03-14 16:28:07 +0100892 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
893 pdu.log_type = cpu_to_le32(log->log_type);
894 strcpy((char *) pdu.name, name);
Jens Axboe1b427252012-03-14 15:03:03 +0100895
896 for (i = 0; i < log->nr_samples; i++) {
Jens Axboef5ed7652012-03-14 16:28:07 +0100897 struct io_sample *s = &log->log[i];
Jens Axboe1b427252012-03-14 15:03:03 +0100898
Jens Axboef5ed7652012-03-14 16:28:07 +0100899 s->time = cpu_to_le64(s->time);
900 s->val = cpu_to_le64(s->val);
901 s->ddir = cpu_to_le32(s->ddir);
902 s->bs = cpu_to_le32(s->bs);
Jens Axboe1b427252012-03-14 15:03:03 +0100903 }
904
905 /*
906 * Dirty - since the log is potentially huge, compress it into
907 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
908 * side defragment it.
909 */
910 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
911
912 stream.zalloc = Z_NULL;
913 stream.zfree = Z_NULL;
914 stream.opaque = Z_NULL;
915
916 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
917 free(out_pdu);
Jens Axboe1b427252012-03-14 15:03:03 +0100918 return 1;
919 }
920
921 /*
Jens Axboef5ed7652012-03-14 16:28:07 +0100922 * Send header first, it's not compressed.
Jens Axboe1b427252012-03-14 15:03:03 +0100923 */
Jens Axboef5ed7652012-03-14 16:28:07 +0100924 __fio_init_net_cmd(&cmd, FIO_NET_CMD_IOLOG, sizeof(pdu), 0);
Jens Axboe1b427252012-03-14 15:03:03 +0100925 cmd.flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboef5ed7652012-03-14 16:28:07 +0100926 fio_net_cmd_crc_pdu(&cmd, &pdu);
Jens Axboe1b427252012-03-14 15:03:03 +0100927 fio_send_data(server_fd, &cmd, sizeof(cmd));
Jens Axboef5ed7652012-03-14 16:28:07 +0100928 fio_send_data(server_fd, &pdu, sizeof(pdu));
Jens Axboe1b427252012-03-14 15:03:03 +0100929
Jens Axboef5ed7652012-03-14 16:28:07 +0100930 stream.next_in = (void *) log->log;
931 stream.avail_in = log->nr_samples * sizeof(struct io_sample);
Jens Axboe1b427252012-03-14 15:03:03 +0100932
933 do {
934 unsigned int this_len;
935
936 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
937 stream.next_out = out_pdu;
Jens Axboef5ed7652012-03-14 16:28:07 +0100938 assert(deflate(&stream, Z_FINISH) == Z_OK);
Jens Axboe1b427252012-03-14 15:03:03 +0100939
940 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
941
942 __fio_init_net_cmd(&cmd, FIO_NET_CMD_IOLOG, this_len, 0);
943
944 if (stream.avail_in)
945 cmd.flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
946
947 fio_net_cmd_crc_pdu(&cmd, out_pdu);
948
949 fio_send_data(server_fd, &cmd, sizeof(cmd));
950 fio_send_data(server_fd, out_pdu, this_len);
951 } while (stream.avail_in);
952
Jens Axboe1b427252012-03-14 15:03:03 +0100953 free(out_pdu);
954 deflateEnd(&stream);
955 return 0;
956}
957
Jens Axboe807f9972012-03-02 10:25:24 +0100958void fio_server_send_add_job(struct thread_options *o, const char *ioengine)
959{
960 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +0100961
Jens Axboe731e30a2012-03-14 16:35:37 +0100962 memset(&pdu, 0, sizeof(pdu));
Jens Axboedcaeb602012-03-08 19:45:37 +0100963 convert_thread_options_to_net(&pdu.top, o);
Jens Axboe807f9972012-03-02 10:25:24 +0100964
965 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), 0);
966}
967
Jens Axboe87aa8f12011-10-06 21:24:13 +0200968static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200969{
Jens Axboe811826b2011-10-24 09:11:50 +0200970 struct sockaddr *addr;
971 fio_socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200972 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200973
Jens Axboe811826b2011-10-24 09:11:50 +0200974 if (use_ipv6)
975 sk = socket(AF_INET6, SOCK_STREAM, 0);
976 else
977 sk = socket(AF_INET, SOCK_STREAM, 0);
978
Jens Axboe81179ee2011-10-04 12:42:06 +0200979 if (sk < 0) {
980 log_err("fio: socket: %s\n", strerror(errno));
981 return -1;
982 }
983
984 opt = 1;
985 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
986 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200987 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200988 return -1;
989 }
990#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200991 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200992 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200993 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200994 return -1;
995 }
996#endif
997
Jens Axboe811826b2011-10-24 09:11:50 +0200998 if (use_ipv6) {
999 addr = (struct sockaddr *) &saddr_in6;
1000 socklen = sizeof(saddr_in6);
1001 saddr_in6.sin6_family = AF_INET6;
1002 } else {
1003 addr = (struct sockaddr *) &saddr_in;
1004 socklen = sizeof(saddr_in);
1005 saddr_in.sin_family = AF_INET;
1006 }
Jens Axboe81179ee2011-10-04 12:42:06 +02001007
Jens Axboe811826b2011-10-24 09:11:50 +02001008 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001009 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001010 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001011 return -1;
1012 }
1013
Jens Axboe87aa8f12011-10-06 21:24:13 +02001014 return sk;
1015}
1016
1017static int fio_init_server_sock(void)
1018{
1019 struct sockaddr_un addr;
1020 fio_socklen_t len;
1021 mode_t mode;
1022 int sk;
1023
1024 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1025 if (sk < 0) {
1026 log_err("fio: socket: %s\n", strerror(errno));
1027 return -1;
1028 }
1029
1030 mode = umask(000);
1031
1032 memset(&addr, 0, sizeof(addr));
1033 addr.sun_family = AF_UNIX;
1034 strcpy(addr.sun_path, bind_sock);
1035 unlink(bind_sock);
1036
1037 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1038
1039 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1040 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001041 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001042 return -1;
1043 }
1044
1045 umask(mode);
1046 return sk;
1047}
1048
1049static int fio_init_server_connection(void)
1050{
Jens Axboebebe6392011-10-07 10:00:51 +02001051 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001052 int sk;
1053
1054 dprint(FD_NET, "starting server\n");
1055
1056 if (!bind_sock)
1057 sk = fio_init_server_ip();
1058 else
1059 sk = fio_init_server_sock();
1060
1061 if (sk < 0)
1062 return sk;
1063
Jens Axboe811826b2011-10-24 09:11:50 +02001064 if (!bind_sock) {
1065 char *p, port[16];
1066 const void *src;
1067 int af;
1068
1069 if (use_ipv6) {
1070 af = AF_INET6;
1071 src = &saddr_in6.sin6_addr;
1072 } else {
1073 af = AF_INET;
1074 src = &saddr_in.sin_addr;
1075 }
1076
1077 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1078
1079 sprintf(port, ",%u", fio_net_port);
1080 if (p)
1081 strcat(p, port);
1082 else
1083 strcpy(bind_str, port);
1084 } else
Jens Axboebebe6392011-10-07 10:00:51 +02001085 strcpy(bind_str, bind_sock);
1086
1087 log_info("fio: server listening on %s\n", bind_str);
1088
Jens Axboe89c17072011-10-11 10:15:51 +02001089 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001090 log_err("fio: listen: %s\n", strerror(errno));
1091 return -1;
1092 }
1093
Jens Axboe87aa8f12011-10-06 21:24:13 +02001094 return sk;
1095}
1096
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001097int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1098 struct in6_addr *inp6)
1099
1100{
1101 int ret = 0;
1102
1103 if (*ipv6)
1104 ret = inet_pton(AF_INET6, host, inp6);
1105 else
1106 ret = inet_pton(AF_INET, host, inp);
1107
1108 if (ret != 1) {
1109 struct hostent *hent;
1110
1111 hent = gethostbyname(host);
1112 if (!hent) {
1113 log_err("fio: failed to resolve <%s>\n", host);
1114 return 0;
1115 }
1116
1117 if (*ipv6) {
1118 if (hent->h_addrtype != AF_INET6) {
1119 log_info("fio: falling back to IPv4\n");
1120 *ipv6 = 0;
1121 } else
1122 memcpy(inp6, hent->h_addr_list[0], 16);
1123 }
1124 if (!*ipv6) {
1125 if (hent->h_addrtype != AF_INET) {
1126 log_err("fio: lookup type mismatch\n");
1127 return 0;
1128 }
1129 memcpy(inp, hent->h_addr_list[0], 4);
1130 }
1131 ret = 1;
1132 }
1133
1134 return !(ret == 1);
1135}
1136
Jens Axboe660a2bf2011-10-24 09:35:06 +02001137/*
1138 * Parse a host/ip/port string. Reads from 'str'.
1139 *
1140 * Outputs:
1141 *
1142 * For IPv4:
1143 * *ptr is the host, *port is the port, inp is the destination.
1144 * For IPv6:
1145 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1146 * For local domain sockets:
1147 * *ptr is the filename, *is_sock is 1.
1148 */
Jens Axboebebe6392011-10-07 10:00:51 +02001149int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001150 int *port, struct in_addr *inp,
1151 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001152{
Jens Axboe76867622011-10-25 09:52:51 +02001153 const char *host = str;
1154 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001155 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001156
Jens Axboebebe6392011-10-07 10:00:51 +02001157 *ptr = NULL;
1158 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001159 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001160 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001161
1162 if (!strncmp(str, "sock:", 5)) {
1163 *ptr = strdup(str + 5);
1164 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001165
Jens Axboe76867622011-10-25 09:52:51 +02001166 return 0;
1167 }
1168
1169 /*
1170 * Is it ip:<ip or host>:port
1171 */
1172 if (!strncmp(host, "ip:", 3))
1173 host += 3;
1174 else if (!strncmp(host, "ip4:", 4))
1175 host += 4;
1176 else if (!strncmp(host, "ip6:", 4)) {
1177 host += 4;
1178 *ipv6 = 1;
1179 } else if (host[0] == ':') {
1180 /* String is :port */
1181 host++;
1182 lport = atoi(host);
1183 if (!lport || lport > 65535) {
1184 log_err("fio: bad server port %u\n", port);
1185 return 1;
1186 }
1187 /* no hostname given, we are done */
1188 *port = lport;
1189 return 0;
1190 }
1191
1192 /*
1193 * If no port seen yet, check if there's a last ':' at the end
1194 */
1195 if (!lport) {
1196 portp = strchr(host, ',');
1197 if (portp) {
1198 *portp = '\0';
1199 portp++;
1200 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001201 if (!lport || lport > 65535) {
1202 log_err("fio: bad server port %u\n", port);
1203 return 1;
1204 }
Jens Axboe76867622011-10-25 09:52:51 +02001205 }
1206 }
1207
1208 if (lport)
1209 *port = lport;
1210
1211 if (!strlen(host))
1212 return 0;
1213
1214 *ptr = strdup(host);
1215
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001216 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1217 free(*ptr);
1218 *ptr = NULL;
1219 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001220 }
1221
1222 if (*port == 0)
1223 *port = fio_net_port;
1224
1225 return 0;
1226}
1227
Jens Axboe87aa8f12011-10-06 21:24:13 +02001228/*
1229 * Server arg should be one of:
1230 *
1231 * sock:/path/to/socket
1232 * ip:1.2.3.4
1233 * 1.2.3.4
1234 *
1235 * Where sock uses unix domain sockets, and ip binds the server to
1236 * a specific interface. If no arguments are given to the server, it
1237 * uses IP and binds to 0.0.0.0.
1238 *
1239 */
1240static int fio_handle_server_arg(void)
1241{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001242 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001243 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001244
Jens Axboe87aa8f12011-10-06 21:24:13 +02001245 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1246
1247 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001248 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001249
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001250 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001251 &port, &saddr_in.sin_addr,
1252 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001253
1254 if (!is_sock && bind_sock) {
1255 free(bind_sock);
1256 bind_sock = NULL;
1257 }
1258
Jens Axboea7de0a12011-10-07 12:55:14 +02001259out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001260 fio_net_port = port;
1261 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001262 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001263 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001264}
1265
1266static int fio_server(void)
1267{
1268 int sk, ret;
1269
1270 dprint(FD_NET, "starting server\n");
1271
1272 if (fio_handle_server_arg())
1273 return -1;
1274
1275 sk = fio_init_server_connection();
1276 if (sk < 0)
1277 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001278
1279 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001280
Jens Axboe81179ee2011-10-04 12:42:06 +02001281 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001282
1283 if (fio_server_arg) {
1284 free(fio_server_arg);
1285 fio_server_arg = NULL;
1286 }
Jens Axboebebe6392011-10-07 10:00:51 +02001287 if (bind_sock)
1288 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001289
Jens Axboe81179ee2011-10-04 12:42:06 +02001290 return ret;
1291}
1292
Jens Axboe7b821682011-10-11 12:16:32 +02001293void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001294{
Jens Axboe7b821682011-10-11 12:16:32 +02001295 if (signal == SIGPIPE)
1296 server_fd = -1;
1297 else {
1298 log_info("\nfio: terminating on signal %d\n", signal);
1299 exit_backend = 1;
1300 }
Jens Axboe9abea482011-10-04 13:21:54 +02001301}
1302
Jens Axboe13755d92011-10-10 19:51:26 +02001303static int check_existing_pidfile(const char *pidfile)
1304{
1305 struct stat sb;
1306 char buf[16];
1307 pid_t pid;
1308 FILE *f;
1309
1310 if (stat(pidfile, &sb))
1311 return 0;
1312
1313 f = fopen(pidfile, "r");
1314 if (!f)
1315 return 0;
1316
Jens Axboebfc3b172011-10-10 21:16:55 +02001317 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001318 fclose(f);
1319 return 1;
1320 }
1321 fclose(f);
1322
1323 pid = atoi(buf);
1324 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001325 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001326
1327 return 1;
1328}
1329
1330static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001331{
1332 FILE *fpid;
1333
1334 fpid = fopen(pidfile, "w");
1335 if (!fpid) {
1336 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001337 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001338 }
1339
1340 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001341 fclose(fpid);
1342 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001343}
1344
1345/*
1346 * If pidfile is specified, background us.
1347 */
1348int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001349{
1350 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001351 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001352
Bruce Cran93bcfd22012-02-20 20:18:19 +01001353#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001354 WSADATA wsd;
1355 WSAStartup(MAKEWORD(2,2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001356#endif
1357
Jens Axboe402668f2011-10-10 15:28:58 +02001358 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001359 return fio_server();
1360
Jens Axboe13755d92011-10-10 19:51:26 +02001361 if (check_existing_pidfile(pidfile)) {
1362 log_err("fio: pidfile %s exists and server appears alive\n",
1363 pidfile);
1364 return -1;
1365 }
1366
Jens Axboee46d8092011-10-03 09:11:02 +02001367 pid = fork();
1368 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001369 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001370 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001371 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001372 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001373 int ret = write_pid(pid, pidfile);
1374
1375 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001376 }
Jens Axboee46d8092011-10-03 09:11:02 +02001377
1378 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001379 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1380 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001381 close(STDIN_FILENO);
1382 close(STDOUT_FILENO);
1383 close(STDERR_FILENO);
1384 f_out = NULL;
1385 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001386
1387 ret = fio_server();
1388
1389 closelog();
1390 unlink(pidfile);
1391 free(pidfile);
1392 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001393}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001394
Jens Axboebebe6392011-10-07 10:00:51 +02001395void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001396{
1397 fio_server_arg = strdup(arg);
1398}