blob: 9110707656ad3e2ca4f8166521b9d407efe57551 [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 Axboec70e63e2012-03-15 08:26:18 +010058 "CMD_IOLOG",
Jens Axboe89c17072011-10-11 10:15:51 +020059};
60
61const char *fio_server_op(unsigned int op)
62{
63 static char buf[32];
64
65 if (op < FIO_NET_CMD_NR)
66 return fio_server_ops[op];
67
68 sprintf(buf, "UNKNOWN/%d", op);
69 return buf;
70}
71
Jens Axboe5235f622012-03-14 21:25:51 +010072static ssize_t iov_total_len(const struct iovec *iov, int count)
Jens Axboe132159a2011-09-30 15:01:32 -060073{
Jens Axboe5235f622012-03-14 21:25:51 +010074 ssize_t ret = 0;
75
76 while (count--) {
77 ret += iov->iov_len;
78 iov++;
79 }
80
81 return ret;
82}
83
84static int fio_sendv_data(int sk, struct iovec *iov, int count)
85{
86 ssize_t total_len = iov_total_len(iov, count);
87 ssize_t ret;
Jens Axboe794d69c2011-10-01 08:48:50 -060088
Jens Axboe132159a2011-09-30 15:01:32 -060089 do {
Jens Axboe5235f622012-03-14 21:25:51 +010090 ret = writev(sk, iov, count);
Jens Axboe132159a2011-09-30 15:01:32 -060091 if (ret > 0) {
Jens Axboe5235f622012-03-14 21:25:51 +010092 total_len -= ret;
93 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -060094 break;
Jens Axboe5235f622012-03-14 21:25:51 +010095
96 while (ret) {
97 if (ret >= iov->iov_len) {
98 ret -= iov->iov_len;
99 iov++;
100 continue;
101 }
102 iov->iov_base += ret;
103 iov->iov_len -= ret;
104 ret = 0;
105 }
Jens Axboe132159a2011-09-30 15:01:32 -0600106 } else if (!ret)
107 break;
108 else if (errno == EAGAIN || errno == EINTR)
109 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200110 else
111 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600112 } while (!exit_backend);
113
Jens Axboe5235f622012-03-14 21:25:51 +0100114 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600115 return 0;
116
Jens Axboe8b4e61b2012-03-09 17:10:54 +0100117 if (errno)
118 return -errno;
119
Jens Axboe132159a2011-09-30 15:01:32 -0600120 return 1;
121}
122
Jens Axboe5235f622012-03-14 21:25:51 +0100123int fio_send_data(int sk, const void *p, unsigned int len)
124{
125 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
126
127 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
128
129 return fio_sendv_data(sk, &iov, 1);
130}
131
Jens Axboe132159a2011-09-30 15:01:32 -0600132int fio_recv_data(int sk, void *p, unsigned int len)
133{
134 do {
135 int ret = recv(sk, p, len, MSG_WAITALL);
136
137 if (ret > 0) {
138 len -= ret;
139 if (!len)
140 break;
141 p += ret;
142 continue;
143 } else if (!ret)
144 break;
145 else if (errno == EAGAIN || errno == EINTR)
146 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200147 else
148 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600149 } while (!exit_backend);
150
151 if (!len)
152 return 0;
153
154 return -1;
155}
156
157static int verify_convert_cmd(struct fio_net_cmd *cmd)
158{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600159 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600160
Jens Axboefcee5ff2011-09-30 22:50:39 -0600161 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
162 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600163
Jens Axboe25dfa842012-02-29 10:01:34 +0100164 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600165 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600166 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600167 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600168 return 1;
169 }
170
171 cmd->version = le16_to_cpu(cmd->version);
172 cmd->opcode = le16_to_cpu(cmd->opcode);
173 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200174 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600175 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
176
177 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200178 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600179 break;
180 default:
181 log_err("fio: bad server cmd version %d\n", cmd->version);
182 return 1;
183 }
184
Jens Axboeb9d2f302012-03-08 20:36:28 +0100185 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600186 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
187 return 1;
188 }
189
190 return 0;
191}
192
Jens Axboea64e88d2011-10-03 14:20:01 +0200193/*
194 * Read (and defragment, if necessary) incoming commands
195 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200196struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600197{
Jens Axboea64e88d2011-10-03 14:20:01 +0200198 struct fio_net_cmd cmd, *cmdret = NULL;
199 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600200 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200201 int ret, first = 1;
202 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600203
Jens Axboea64e88d2011-10-03 14:20:01 +0200204 do {
205 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
206 if (ret)
207 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600208
Jens Axboea64e88d2011-10-03 14:20:01 +0200209 /* We have a command, verify it and swap if need be */
210 ret = verify_convert_cmd(&cmd);
211 if (ret)
212 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600213
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200214 if (first) {
215 /* if this is text, add room for \0 at the end */
216 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
217 assert(!cmdret);
218 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200219 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600220
Jens Axboea64e88d2011-10-03 14:20:01 +0200221 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600222
Jens Axboea64e88d2011-10-03 14:20:01 +0200223 if (first)
224 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200225 else if (cmdret->opcode != cmd.opcode) {
226 log_err("fio: fragment opcode mismatch (%d != %d)\n",
227 cmdret->opcode, cmd.opcode);
228 ret = 1;
229 break;
230 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200231
232 if (!cmd.pdu_len)
233 break;
234
235 /* There's payload, get it */
236 pdu = (void *) cmdret->payload + pdu_offset;
237 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
238 if (ret)
239 break;
240
241 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100242 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200243 if (crc != cmd.pdu_crc16) {
244 log_err("fio: server bad crc on payload ");
245 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
246 ret = 1;
247 break;
248 }
249
250 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200251 if (!first)
252 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200253 first = 0;
254 } while (cmd.flags & FIO_NET_CMD_F_MORE);
255
256 if (ret) {
257 free(cmdret);
258 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200259 } else if (cmdret) {
260 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100261 if (cmdret->pdu_len) {
262 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
263 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
264 char *buf = (char *) pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200265
Jens Axboe084d1c62012-03-03 20:28:07 +0100266 buf[pdu->buf_len ] = '\0';
267 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
Jens Axboe46bcd492012-03-14 11:31:21 +0100268 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
269 char *buf = (char *) pdu->buf;
270 int len = le32_to_cpu(pdu->buf_len);
Jens Axboe084d1c62012-03-03 20:28:07 +0100271
Jens Axboe46bcd492012-03-14 11:31:21 +0100272 buf[len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100273 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200274 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100275
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200276 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200277 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200278 }
Jens Axboe132159a2011-09-30 15:01:32 -0600279
Jens Axboea64e88d2011-10-03 14:20:01 +0200280 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600281}
282
Jens Axboe53bd8db2012-03-14 21:51:38 +0100283void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
Jens Axboe132159a2011-09-30 15:01:32 -0600284{
285 uint32_t pdu_len;
286
Jens Axboe25dfa842012-02-29 10:01:34 +0100287 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600288
289 pdu_len = le32_to_cpu(cmd->pdu_len);
Jens Axboe1b427252012-03-14 15:03:03 +0100290 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
291}
292
293void fio_net_cmd_crc(struct fio_net_cmd *cmd)
294{
295 fio_net_cmd_crc_pdu(cmd, cmd->payload);
Jens Axboe132159a2011-09-30 15:01:32 -0600296}
297
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200298int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
299 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600300{
Jens Axboe7f868312011-10-10 09:55:21 +0200301 struct fio_net_cmd *cmd = NULL;
302 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600303 int ret;
304
305 do {
306 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100307 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
308 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600309
Jens Axboe7f868312011-10-10 09:55:21 +0200310 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
311 if (cmd)
312 free(cmd);
313
314 cur_len = sizeof(*cmd) + this_len;
315 cmd = malloc(cur_len);
316 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600317
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200318 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600319
320 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200321 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600322
323 fio_net_cmd_crc(cmd);
324
325 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600326 size -= this_len;
327 buf += this_len;
328 } while (!ret && size);
329
Jens Axboe7f868312011-10-10 09:55:21 +0200330 if (cmd)
331 free(cmd);
332
Jens Axboe794d69c2011-10-01 08:48:50 -0600333 return ret;
334}
335
Jens Axboe89c17072011-10-11 10:15:51 +0200336static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600337{
Jens Axboe178cde92011-10-05 22:14:31 +0200338 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600339
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200340 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600341 fio_net_cmd_crc(&cmd);
342
343 return fio_send_data(sk, &cmd, sizeof(cmd));
344}
345
Jens Axboe89c17072011-10-11 10:15:51 +0200346/*
347 * If 'list' is non-NULL, then allocate and store the sent command for
348 * later verification.
349 */
350int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
351 struct flist_head *list)
352{
353 struct fio_net_int_cmd *cmd;
354 int ret;
355
356 if (!list)
357 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
358
359 cmd = malloc(sizeof(*cmd));
360
Jens Axboedf380932011-10-11 14:25:08 +0200361 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
Jens Axboe89c17072011-10-11 10:15:51 +0200362 fio_net_cmd_crc(&cmd->cmd);
363
364 INIT_FLIST_HEAD(&cmd->list);
365 gettimeofday(&cmd->tv, NULL);
366 cmd->saved_tag = tag;
367
368 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
369 if (ret) {
370 free(cmd);
371 return ret;
372 }
373
374 flist_add_tail(&cmd->list, list);
375 return 0;
376}
377
Jens Axboe9abea482011-10-04 13:21:54 +0200378static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600379{
Jens Axboe46c48f12011-10-01 12:50:51 -0600380 dprint(FD_NET, "server: sending quit\n");
Jens Axboe89c17072011-10-11 10:15:51 +0200381 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600382}
383
Jens Axboeb9d2f302012-03-08 20:36:28 +0100384static int handle_run_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600385{
Jens Axboe11e950b2011-10-16 21:34:14 +0200386 struct cmd_end_pdu epdu;
Jens Axboea64e88d2011-10-03 14:20:01 +0200387 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600388
Jens Axboe2e1df072012-02-09 11:15:02 +0100389 ret = fio_backend();
Jens Axboe11e950b2011-10-16 21:34:14 +0200390
391 epdu.error = ret;
392 fio_net_send_cmd(server_fd, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0);
393
Jens Axboe9abea482011-10-04 13:21:54 +0200394 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200395 reset_fio_state();
Jens Axboe8663ea62012-03-02 14:04:30 +0100396 first_cmd_check = 0;
Jens Axboe81179ee2011-10-04 12:42:06 +0200397 return ret;
398}
399
Jens Axboeb9d2f302012-03-08 20:36:28 +0100400static int handle_job_cmd(struct fio_net_cmd *cmd)
401{
Jens Axboe46bcd492012-03-14 11:31:21 +0100402 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
403 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100404 struct cmd_start_pdu spdu;
405
Jens Axboe46bcd492012-03-14 11:31:21 +0100406 pdu->buf_len = le32_to_cpu(pdu->buf_len);
407 pdu->client_type = le32_to_cpu(pdu->client_type);
408
409 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboeb9d2f302012-03-08 20:36:28 +0100410 fio_server_send_quit_cmd();
411 return -1;
412 }
413
414 spdu.jobs = cpu_to_le32(thread_number);
415 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
416 return 0;
417}
418
Jens Axboe81179ee2011-10-04 12:42:06 +0200419static int handle_jobline_cmd(struct fio_net_cmd *cmd)
420{
Jens Axboefa2ea802011-10-08 21:07:29 +0200421 void *pdu = cmd->payload;
422 struct cmd_single_line_pdu *cslp;
423 struct cmd_line_pdu *clp;
424 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100425 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200426 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100427 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200428
Jens Axboefa2ea802011-10-08 21:07:29 +0200429 clp = pdu;
430 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100431 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200432 argv = malloc(clp->lines * sizeof(char *));
433 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200434
Jens Axboefa2ea802011-10-08 21:07:29 +0200435 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200436
Jens Axboefa2ea802011-10-08 21:07:29 +0200437 for (i = 0; i < clp->lines; i++) {
438 cslp = pdu + offset;
439 argv[i] = (char *) cslp->text;
440
441 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200442 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
443 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200444
Jens Axboe46bcd492012-03-14 11:31:21 +0100445 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200446 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200447 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200448 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200449 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200450
Jens Axboefa2ea802011-10-08 21:07:29 +0200451 free(argv);
452
Jens Axboeb9d2f302012-03-08 20:36:28 +0100453 spdu.jobs = cpu_to_le32(thread_number);
454 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
455 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600456}
457
Jens Axboec28e8e82011-10-04 08:57:39 +0200458static int handle_probe_cmd(struct fio_net_cmd *cmd)
459{
460 struct cmd_probe_pdu probe;
461
Jens Axboe89c17072011-10-11 10:15:51 +0200462 dprint(FD_NET, "server: sending probe reply\n");
463
Jens Axboec28e8e82011-10-04 08:57:39 +0200464 memset(&probe, 0, sizeof(probe));
465 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200466#ifdef FIO_BIG_ENDIAN
467 probe.bigendian = 1;
468#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200469 probe.fio_major = FIO_MAJOR;
470 probe.fio_minor = FIO_MINOR;
471 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200472
Jens Axboecca84642011-10-07 12:47:57 +0200473 probe.os = FIO_OS;
474 probe.arch = FIO_ARCH;
475
Jens Axboe38fdef22011-10-11 14:30:06 +0200476 probe.bpp = sizeof(void *);
477
Jens Axboe89c17072011-10-11 10:15:51 +0200478 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200479}
480
481static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
482{
483 struct jobs_eta *je;
484 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200485 int i;
486
Jens Axboeb814fb22011-10-12 09:20:34 +0200487 if (!thread_number)
488 return 0;
489
490 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200491 je = malloc(size);
492 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200493
494 if (!calc_thread_status(je, 1)) {
495 free(je);
496 return 0;
497 }
498
499 dprint(FD_NET, "server sending status\n");
500
501 je->nr_running = cpu_to_le32(je->nr_running);
502 je->nr_ramp = cpu_to_le32(je->nr_ramp);
503 je->nr_pending = cpu_to_le32(je->nr_pending);
504 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200505
506 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100507 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
508 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
509 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
510 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200511 je->rate[i] = cpu_to_le32(je->rate[i]);
512 je->iops[i] = cpu_to_le32(je->iops[i]);
513 }
514
Jens Axboec1970b92012-03-06 15:37:40 +0100515 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200516 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100517 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200518
Jens Axboe7f868312011-10-10 09:55:21 +0200519 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200520 free(je);
521 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200522}
523
Jens Axboe132159a2011-09-30 15:01:32 -0600524static int handle_command(struct fio_net_cmd *cmd)
525{
526 int ret;
527
Jens Axboe89c17072011-10-11 10:15:51 +0200528 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
529 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600530
Jens Axboe132159a2011-09-30 15:01:32 -0600531 switch (cmd->opcode) {
532 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200533 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200534 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200535 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600536 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200537 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600538 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200539 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600540 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200541 case FIO_NET_CMD_JOBLINE:
542 ret = handle_jobline_cmd(cmd);
543 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200544 case FIO_NET_CMD_PROBE:
545 ret = handle_probe_cmd(cmd);
546 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200547 case FIO_NET_CMD_SEND_ETA:
548 ret = handle_send_eta_cmd(cmd);
549 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100550 case FIO_NET_CMD_RUN:
551 ret = handle_run_cmd(cmd);
552 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600553 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200554 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600555 ret = 1;
556 }
557
558 return ret;
559}
560
Jens Axboe70e0c312011-10-04 09:18:30 +0200561static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600562{
563 struct fio_net_cmd *cmd = NULL;
564 int ret = 0;
565
566 /* read forever */
567 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200568 struct pollfd pfd = {
569 .fd = sk,
570 .events = POLLIN,
571 };
572
573 ret = 0;
574 do {
575 ret = poll(&pfd, 1, 100);
576 if (ret < 0) {
577 if (errno == EINTR)
578 break;
579 log_err("fio: poll: %s\n", strerror(errno));
580 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200581 } else if (!ret) {
582 if (!block)
583 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200584 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200585 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200586
587 if (pfd.revents & POLLIN)
588 break;
589 if (pfd.revents & (POLLERR|POLLHUP)) {
590 ret = 1;
591 break;
592 }
Jens Axboe19c65172011-10-05 22:05:37 +0200593 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200594
595 if (ret < 0)
596 break;
597
598 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600599 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200600 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600601 break;
602 }
603
Jens Axboe132159a2011-09-30 15:01:32 -0600604 ret = handle_command(cmd);
605 if (ret)
606 break;
607
608 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400609 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600610 }
611
612 if (cmd)
613 free(cmd);
614
615 return ret;
616}
617
Jens Axboecc0df002011-10-03 20:53:32 +0200618void fio_server_idle_loop(void)
619{
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100620 if (!first_cmd_check) {
Jens Axboe5d7793a2012-03-08 19:59:16 +0100621 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100622 first_cmd_check = 1;
623 }
Jens Axboecc0df002011-10-03 20:53:32 +0200624 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200625 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200626}
627
Jens Axboe50d16972011-09-29 17:45:28 -0600628static int accept_loop(int listen_sk)
629{
Jens Axboebb447a22011-10-04 15:06:42 +0200630 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200631 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600632 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600633 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600634
Jens Axboe60efd142011-10-04 13:30:11 +0200635 dprint(FD_NET, "server enter accept loop\n");
636
Jens Axboe009b1be2011-09-29 18:27:02 -0600637 flags = fcntl(listen_sk, F_GETFL);
638 flags |= O_NONBLOCK;
639 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600640again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600641 pfd.fd = listen_sk;
642 pfd.events = POLLIN;
643 do {
644 ret = poll(&pfd, 1, 100);
645 if (ret < 0) {
646 if (errno == EINTR)
647 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600648 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600649 goto out;
650 } else if (!ret)
651 continue;
652
653 if (pfd.revents & POLLIN)
654 break;
655 } while (!exit_backend);
656
657 if (exit_backend)
658 goto out;
659
Jens Axboe6b976bf2011-10-04 15:07:43 +0200660 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600661 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600662 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600663 return -1;
664 }
665
Jens Axboebb447a22011-10-04 15:06:42 +0200666 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600667
Jens Axboe37db14f2011-09-30 17:00:42 -0600668 server_fd = sk;
669
Jens Axboe70e0c312011-10-04 09:18:30 +0200670 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600671
Jens Axboe37db14f2011-09-30 17:00:42 -0600672 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600673 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600674
Jens Axboe009b1be2011-09-29 18:27:02 -0600675 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600676 goto again;
677
Jens Axboe009b1be2011-09-29 18:27:02 -0600678out:
Jens Axboe132159a2011-09-30 15:01:32 -0600679 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600680}
681
Jens Axboe084d1c62012-03-03 20:28:07 +0100682int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600683{
Jens Axboe084d1c62012-03-03 20:28:07 +0100684 struct cmd_text_pdu *pdu;
685 unsigned int tlen;
686 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600687
Jens Axboe084d1c62012-03-03 20:28:07 +0100688 if (server_fd == -1)
689 return log_local_buf(buf, len);
690
691 tlen = sizeof(*pdu) + len;
692 pdu = malloc(tlen);
693
694 pdu->level = __cpu_to_le32(level);
695 pdu->buf_len = __cpu_to_le32(len);
696
697 gettimeofday(&tv, NULL);
698 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
699 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
700
701 memcpy(pdu->buf, buf, len);
702
703 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, 0);
704 free(pdu);
705 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600706}
707
Jens Axboea64e88d2011-10-03 14:20:01 +0200708static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
709{
710 dst->max_val = cpu_to_le64(src->max_val);
711 dst->min_val = cpu_to_le64(src->min_val);
712 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200713
714 /*
715 * Encode to IEEE 754 for network transfer
716 */
717 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
718 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200719}
720
721static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
722{
723 int i;
724
725 for (i = 0; i < 2; i++) {
726 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
727 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
728 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
729 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
730 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
731 dst->agg[i] = cpu_to_le64(src->agg[i]);
732 }
733
734 dst->kb_base = cpu_to_le32(src->kb_base);
735 dst->groupid = cpu_to_le32(src->groupid);
736}
737
738/*
739 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
740 * into a single payload.
741 */
742void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
743{
744 struct cmd_ts_pdu p;
745 int i, j;
746
Jens Axboe60efd142011-10-04 13:30:11 +0200747 dprint(FD_NET, "server sending end stats\n");
748
Jens Axboe317b3c82011-10-03 21:47:27 +0200749 memset(&p, 0, sizeof(p));
750
Jens Axboea64e88d2011-10-03 14:20:01 +0200751 strcpy(p.ts.name, ts->name);
752 strcpy(p.ts.verror, ts->verror);
753 strcpy(p.ts.description, ts->description);
754
Jens Axboeddcc0b62011-10-03 14:45:27 +0200755 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200756 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200757 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200758 p.ts.members = cpu_to_le32(ts->members);
759
760 for (i = 0; i < 2; i++) {
761 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
762 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
763 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
764 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
765 }
766
767 p.ts.usr_time = cpu_to_le64(ts->usr_time);
768 p.ts.sys_time = cpu_to_le64(ts->sys_time);
769 p.ts.ctx = cpu_to_le64(ts->ctx);
770 p.ts.minf = cpu_to_le64(ts->minf);
771 p.ts.majf = cpu_to_le64(ts->majf);
772 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200773
774 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200775 fio_fp64_t *src = &ts->percentile_list[i];
776 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200777
Jens Axboecfc03e42011-10-12 21:20:42 +0200778 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200779 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200780
781 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
782 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
783 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
784 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
785 }
786
787 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
788 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
789 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
790 }
791
792 for (i = 0; i < 2; i++)
793 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
794 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
795
796 for (i = 0; i < 3; i++) {
797 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200798 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200799 }
800
Jens Axboe93eee042011-10-03 21:08:48 +0200801 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200802 p.ts.total_complete = cpu_to_le64(ts->total_complete);
803
804 for (i = 0; i < 2; i++) {
805 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
806 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
807 }
808
809 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
810 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
811 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200812 p.ts.first_error = cpu_to_le32(ts->first_error);
813 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200814
815 convert_gs(&p.rs, rs);
816
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200817 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200818}
819
820void fio_server_send_gs(struct group_run_stats *rs)
821{
822 struct group_run_stats gs;
823
Jens Axboe60efd142011-10-04 13:30:11 +0200824 dprint(FD_NET, "server sending group run stats\n");
825
Jens Axboea64e88d2011-10-03 14:20:01 +0200826 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200827 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200828}
829
Jens Axboed09a64a2011-10-13 11:38:56 +0200830static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
831{
832 int i;
833
834 for (i = 0; i < 2; i++) {
835 dst->ios[i] = cpu_to_le32(src->ios[i]);
836 dst->merges[i] = cpu_to_le32(src->merges[i]);
837 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
838 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
839 }
840
841 dst->io_ticks = cpu_to_le32(src->io_ticks);
842 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
843 dst->slavecount = cpu_to_le32(src->slavecount);
844 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
845}
846
847static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
848{
849 int i;
850
851 strcpy((char *) dst->name, (char *) src->name);
852
853 for (i = 0; i < 2; i++) {
854 dst->ios[i] = cpu_to_le32(src->ios[i]);
855 dst->merges[i] = cpu_to_le32(src->merges[i]);
856 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
857 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
858 }
859
860 dst->io_ticks = cpu_to_le32(src->io_ticks);
861 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
862 dst->msec = cpu_to_le64(src->msec);
863}
864
865void fio_server_send_du(void)
866{
867 struct disk_util *du;
868 struct flist_head *entry;
869 struct cmd_du_pdu pdu;
870
871 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
872
Jens Axboe0766d922011-10-13 12:02:08 +0200873 memset(&pdu, 0, sizeof(pdu));
874
Jens Axboed09a64a2011-10-13 11:38:56 +0200875 flist_for_each(entry, &disk_list) {
876 du = flist_entry(entry, struct disk_util, list);
877
878 convert_dus(&pdu.dus, &du->dus);
879 convert_agg(&pdu.agg, &du->agg);
880
881 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
882 }
883}
884
Jens Axboe53bd8db2012-03-14 21:51:38 +0100885/*
886 * Send a command with a separate PDU, not inlined in the command
887 */
888static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
889 off_t size, uint64_t tag, uint32_t flags)
890{
891 struct fio_net_cmd cmd;
892 struct iovec iov[2];
893
894 iov[0].iov_base = &cmd;
895 iov[0].iov_len = sizeof(cmd);
896 iov[1].iov_base = (void *) buf;
897 iov[1].iov_len = size;
898
899 __fio_init_net_cmd(&cmd, opcode, size, tag);
900 cmd.flags = __cpu_to_le32(flags);
901 fio_net_cmd_crc_pdu(&cmd, buf);
902
903 return fio_sendv_data(server_fd, iov, 2);
904}
905
Jens Axboe1b427252012-03-14 15:03:03 +0100906int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
907{
Jens Axboef5ed7652012-03-14 16:28:07 +0100908 struct cmd_iolog_pdu pdu;
Jens Axboe1b427252012-03-14 15:03:03 +0100909 z_stream stream;
910 void *out_pdu;
Jens Axboe53bd8db2012-03-14 21:51:38 +0100911 int i, ret = 0;
Jens Axboe1b427252012-03-14 15:03:03 +0100912
Jens Axboef5ed7652012-03-14 16:28:07 +0100913 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
914 pdu.log_type = cpu_to_le32(log->log_type);
915 strcpy((char *) pdu.name, name);
Jens Axboe1b427252012-03-14 15:03:03 +0100916
917 for (i = 0; i < log->nr_samples; i++) {
Jens Axboef5ed7652012-03-14 16:28:07 +0100918 struct io_sample *s = &log->log[i];
Jens Axboe1b427252012-03-14 15:03:03 +0100919
Jens Axboef5ed7652012-03-14 16:28:07 +0100920 s->time = cpu_to_le64(s->time);
921 s->val = cpu_to_le64(s->val);
922 s->ddir = cpu_to_le32(s->ddir);
923 s->bs = cpu_to_le32(s->bs);
Jens Axboe1b427252012-03-14 15:03:03 +0100924 }
925
926 /*
927 * Dirty - since the log is potentially huge, compress it into
928 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
929 * side defragment it.
930 */
931 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
932
933 stream.zalloc = Z_NULL;
934 stream.zfree = Z_NULL;
935 stream.opaque = Z_NULL;
936
937 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
Jens Axboe53bd8db2012-03-14 21:51:38 +0100938 ret = 1;
939 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +0100940 }
941
942 /*
Jens Axboef5ed7652012-03-14 16:28:07 +0100943 * Send header first, it's not compressed.
Jens Axboe1b427252012-03-14 15:03:03 +0100944 */
Jens Axboe53bd8db2012-03-14 21:51:38 +0100945 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
946 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
947 if (ret)
948 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +0100949
Jens Axboef5ed7652012-03-14 16:28:07 +0100950 stream.next_in = (void *) log->log;
951 stream.avail_in = log->nr_samples * sizeof(struct io_sample);
Jens Axboe1b427252012-03-14 15:03:03 +0100952
953 do {
Jens Axboe53bd8db2012-03-14 21:51:38 +0100954 unsigned int this_len, flags = 0;
955 int ret;
Jens Axboe1b427252012-03-14 15:03:03 +0100956
957 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
958 stream.next_out = out_pdu;
Jens Axboe3c547fe2012-03-14 21:53:00 +0100959 ret = deflate(&stream, Z_FINISH);
960 /* may be Z_OK, or Z_STREAM_END */
961 if (ret < 0)
962 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +0100963
964 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
965
Jens Axboe1b427252012-03-14 15:03:03 +0100966 if (stream.avail_in)
Jens Axboe53bd8db2012-03-14 21:51:38 +0100967 flags = FIO_NET_CMD_F_MORE;
Jens Axboe1b427252012-03-14 15:03:03 +0100968
Jens Axboe53bd8db2012-03-14 21:51:38 +0100969 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
970 out_pdu, this_len, 0, flags);
971 if (ret)
972 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +0100973 } while (stream.avail_in);
974
Jens Axboe53bd8db2012-03-14 21:51:38 +0100975err_zlib:
Jens Axboe1b427252012-03-14 15:03:03 +0100976 deflateEnd(&stream);
Jens Axboe53bd8db2012-03-14 21:51:38 +0100977err:
978 free(out_pdu);
979 return ret;
Jens Axboe1b427252012-03-14 15:03:03 +0100980}
981
Jens Axboe807f9972012-03-02 10:25:24 +0100982void fio_server_send_add_job(struct thread_options *o, const char *ioengine)
983{
984 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +0100985
Jens Axboe731e30a2012-03-14 16:35:37 +0100986 memset(&pdu, 0, sizeof(pdu));
Jens Axboedcaeb602012-03-08 19:45:37 +0100987 convert_thread_options_to_net(&pdu.top, o);
Jens Axboe807f9972012-03-02 10:25:24 +0100988
989 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), 0);
990}
991
Jens Axboe87aa8f12011-10-06 21:24:13 +0200992static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200993{
Jens Axboe811826b2011-10-24 09:11:50 +0200994 struct sockaddr *addr;
995 fio_socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200996 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200997
Jens Axboe811826b2011-10-24 09:11:50 +0200998 if (use_ipv6)
999 sk = socket(AF_INET6, SOCK_STREAM, 0);
1000 else
1001 sk = socket(AF_INET, SOCK_STREAM, 0);
1002
Jens Axboe81179ee2011-10-04 12:42:06 +02001003 if (sk < 0) {
1004 log_err("fio: socket: %s\n", strerror(errno));
1005 return -1;
1006 }
1007
1008 opt = 1;
1009 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1010 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001011 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001012 return -1;
1013 }
1014#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +02001015 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001016 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001017 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001018 return -1;
1019 }
1020#endif
1021
Jens Axboe811826b2011-10-24 09:11:50 +02001022 if (use_ipv6) {
1023 addr = (struct sockaddr *) &saddr_in6;
1024 socklen = sizeof(saddr_in6);
1025 saddr_in6.sin6_family = AF_INET6;
1026 } else {
1027 addr = (struct sockaddr *) &saddr_in;
1028 socklen = sizeof(saddr_in);
1029 saddr_in.sin_family = AF_INET;
1030 }
Jens Axboe81179ee2011-10-04 12:42:06 +02001031
Jens Axboe811826b2011-10-24 09:11:50 +02001032 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001033 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001034 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001035 return -1;
1036 }
1037
Jens Axboe87aa8f12011-10-06 21:24:13 +02001038 return sk;
1039}
1040
1041static int fio_init_server_sock(void)
1042{
1043 struct sockaddr_un addr;
1044 fio_socklen_t len;
1045 mode_t mode;
1046 int sk;
1047
1048 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1049 if (sk < 0) {
1050 log_err("fio: socket: %s\n", strerror(errno));
1051 return -1;
1052 }
1053
1054 mode = umask(000);
1055
1056 memset(&addr, 0, sizeof(addr));
1057 addr.sun_family = AF_UNIX;
1058 strcpy(addr.sun_path, bind_sock);
1059 unlink(bind_sock);
1060
1061 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1062
1063 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1064 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001065 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001066 return -1;
1067 }
1068
1069 umask(mode);
1070 return sk;
1071}
1072
1073static int fio_init_server_connection(void)
1074{
Jens Axboebebe6392011-10-07 10:00:51 +02001075 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001076 int sk;
1077
1078 dprint(FD_NET, "starting server\n");
1079
1080 if (!bind_sock)
1081 sk = fio_init_server_ip();
1082 else
1083 sk = fio_init_server_sock();
1084
1085 if (sk < 0)
1086 return sk;
1087
Jens Axboe811826b2011-10-24 09:11:50 +02001088 if (!bind_sock) {
1089 char *p, port[16];
1090 const void *src;
1091 int af;
1092
1093 if (use_ipv6) {
1094 af = AF_INET6;
1095 src = &saddr_in6.sin6_addr;
1096 } else {
1097 af = AF_INET;
1098 src = &saddr_in.sin_addr;
1099 }
1100
1101 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1102
1103 sprintf(port, ",%u", fio_net_port);
1104 if (p)
1105 strcat(p, port);
1106 else
1107 strcpy(bind_str, port);
1108 } else
Jens Axboebebe6392011-10-07 10:00:51 +02001109 strcpy(bind_str, bind_sock);
1110
1111 log_info("fio: server listening on %s\n", bind_str);
1112
Jens Axboe89c17072011-10-11 10:15:51 +02001113 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001114 log_err("fio: listen: %s\n", strerror(errno));
1115 return -1;
1116 }
1117
Jens Axboe87aa8f12011-10-06 21:24:13 +02001118 return sk;
1119}
1120
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001121int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1122 struct in6_addr *inp6)
1123
1124{
1125 int ret = 0;
1126
1127 if (*ipv6)
1128 ret = inet_pton(AF_INET6, host, inp6);
1129 else
1130 ret = inet_pton(AF_INET, host, inp);
1131
1132 if (ret != 1) {
1133 struct hostent *hent;
1134
1135 hent = gethostbyname(host);
1136 if (!hent) {
1137 log_err("fio: failed to resolve <%s>\n", host);
1138 return 0;
1139 }
1140
1141 if (*ipv6) {
1142 if (hent->h_addrtype != AF_INET6) {
1143 log_info("fio: falling back to IPv4\n");
1144 *ipv6 = 0;
1145 } else
1146 memcpy(inp6, hent->h_addr_list[0], 16);
1147 }
1148 if (!*ipv6) {
1149 if (hent->h_addrtype != AF_INET) {
1150 log_err("fio: lookup type mismatch\n");
1151 return 0;
1152 }
1153 memcpy(inp, hent->h_addr_list[0], 4);
1154 }
1155 ret = 1;
1156 }
1157
1158 return !(ret == 1);
1159}
1160
Jens Axboe660a2bf2011-10-24 09:35:06 +02001161/*
1162 * Parse a host/ip/port string. Reads from 'str'.
1163 *
1164 * Outputs:
1165 *
1166 * For IPv4:
1167 * *ptr is the host, *port is the port, inp is the destination.
1168 * For IPv6:
1169 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1170 * For local domain sockets:
1171 * *ptr is the filename, *is_sock is 1.
1172 */
Jens Axboebebe6392011-10-07 10:00:51 +02001173int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001174 int *port, struct in_addr *inp,
1175 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001176{
Jens Axboe76867622011-10-25 09:52:51 +02001177 const char *host = str;
1178 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001179 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001180
Jens Axboebebe6392011-10-07 10:00:51 +02001181 *ptr = NULL;
1182 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001183 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001184 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001185
1186 if (!strncmp(str, "sock:", 5)) {
1187 *ptr = strdup(str + 5);
1188 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001189
Jens Axboe76867622011-10-25 09:52:51 +02001190 return 0;
1191 }
1192
1193 /*
1194 * Is it ip:<ip or host>:port
1195 */
1196 if (!strncmp(host, "ip:", 3))
1197 host += 3;
1198 else if (!strncmp(host, "ip4:", 4))
1199 host += 4;
1200 else if (!strncmp(host, "ip6:", 4)) {
1201 host += 4;
1202 *ipv6 = 1;
1203 } else if (host[0] == ':') {
1204 /* String is :port */
1205 host++;
1206 lport = atoi(host);
1207 if (!lport || lport > 65535) {
1208 log_err("fio: bad server port %u\n", port);
1209 return 1;
1210 }
1211 /* no hostname given, we are done */
1212 *port = lport;
1213 return 0;
1214 }
1215
1216 /*
1217 * If no port seen yet, check if there's a last ':' at the end
1218 */
1219 if (!lport) {
1220 portp = strchr(host, ',');
1221 if (portp) {
1222 *portp = '\0';
1223 portp++;
1224 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001225 if (!lport || lport > 65535) {
1226 log_err("fio: bad server port %u\n", port);
1227 return 1;
1228 }
Jens Axboe76867622011-10-25 09:52:51 +02001229 }
1230 }
1231
1232 if (lport)
1233 *port = lport;
1234
1235 if (!strlen(host))
1236 return 0;
1237
1238 *ptr = strdup(host);
1239
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001240 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1241 free(*ptr);
1242 *ptr = NULL;
1243 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001244 }
1245
1246 if (*port == 0)
1247 *port = fio_net_port;
1248
1249 return 0;
1250}
1251
Jens Axboe87aa8f12011-10-06 21:24:13 +02001252/*
1253 * Server arg should be one of:
1254 *
1255 * sock:/path/to/socket
1256 * ip:1.2.3.4
1257 * 1.2.3.4
1258 *
1259 * Where sock uses unix domain sockets, and ip binds the server to
1260 * a specific interface. If no arguments are given to the server, it
1261 * uses IP and binds to 0.0.0.0.
1262 *
1263 */
1264static int fio_handle_server_arg(void)
1265{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001266 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001267 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001268
Jens Axboe87aa8f12011-10-06 21:24:13 +02001269 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1270
1271 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001272 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001273
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001274 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001275 &port, &saddr_in.sin_addr,
1276 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001277
1278 if (!is_sock && bind_sock) {
1279 free(bind_sock);
1280 bind_sock = NULL;
1281 }
1282
Jens Axboea7de0a12011-10-07 12:55:14 +02001283out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001284 fio_net_port = port;
1285 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001286 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001287 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001288}
1289
1290static int fio_server(void)
1291{
1292 int sk, ret;
1293
1294 dprint(FD_NET, "starting server\n");
1295
1296 if (fio_handle_server_arg())
1297 return -1;
1298
1299 sk = fio_init_server_connection();
1300 if (sk < 0)
1301 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001302
1303 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001304
Jens Axboe81179ee2011-10-04 12:42:06 +02001305 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001306
1307 if (fio_server_arg) {
1308 free(fio_server_arg);
1309 fio_server_arg = NULL;
1310 }
Jens Axboebebe6392011-10-07 10:00:51 +02001311 if (bind_sock)
1312 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001313
Jens Axboe81179ee2011-10-04 12:42:06 +02001314 return ret;
1315}
1316
Jens Axboe7b821682011-10-11 12:16:32 +02001317void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001318{
Jens Axboe7b821682011-10-11 12:16:32 +02001319 if (signal == SIGPIPE)
1320 server_fd = -1;
1321 else {
1322 log_info("\nfio: terminating on signal %d\n", signal);
1323 exit_backend = 1;
1324 }
Jens Axboe9abea482011-10-04 13:21:54 +02001325}
1326
Jens Axboe13755d92011-10-10 19:51:26 +02001327static int check_existing_pidfile(const char *pidfile)
1328{
1329 struct stat sb;
1330 char buf[16];
1331 pid_t pid;
1332 FILE *f;
1333
1334 if (stat(pidfile, &sb))
1335 return 0;
1336
1337 f = fopen(pidfile, "r");
1338 if (!f)
1339 return 0;
1340
Jens Axboebfc3b172011-10-10 21:16:55 +02001341 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001342 fclose(f);
1343 return 1;
1344 }
1345 fclose(f);
1346
1347 pid = atoi(buf);
1348 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001349 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001350
1351 return 1;
1352}
1353
1354static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001355{
1356 FILE *fpid;
1357
1358 fpid = fopen(pidfile, "w");
1359 if (!fpid) {
1360 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001361 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001362 }
1363
1364 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001365 fclose(fpid);
1366 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001367}
1368
1369/*
1370 * If pidfile is specified, background us.
1371 */
1372int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001373{
1374 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001375 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001376
Bruce Cran93bcfd22012-02-20 20:18:19 +01001377#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001378 WSADATA wsd;
1379 WSAStartup(MAKEWORD(2,2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001380#endif
1381
Jens Axboe402668f2011-10-10 15:28:58 +02001382 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001383 return fio_server();
1384
Jens Axboe13755d92011-10-10 19:51:26 +02001385 if (check_existing_pidfile(pidfile)) {
1386 log_err("fio: pidfile %s exists and server appears alive\n",
1387 pidfile);
1388 return -1;
1389 }
1390
Jens Axboee46d8092011-10-03 09:11:02 +02001391 pid = fork();
1392 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001393 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001394 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001395 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001396 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001397 int ret = write_pid(pid, pidfile);
1398
1399 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001400 }
Jens Axboee46d8092011-10-03 09:11:02 +02001401
1402 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001403 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1404 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001405 close(STDIN_FILENO);
1406 close(STDOUT_FILENO);
1407 close(STDERR_FILENO);
1408 f_out = NULL;
1409 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001410
1411 ret = fio_server();
1412
1413 closelog();
1414 unlink(pidfile);
1415 free(pidfile);
1416 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001417}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001418
Jens Axboebebe6392011-10-07 10:00:51 +02001419void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001420{
1421 fio_server_arg = strdup(arg);
1422}