blob: 899b230274b1133dd54c3aacdb0020acf70056ba [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 Axboe2f122b12012-03-15 13:10:19 +0100755 p.ts.error = cpu_to_le32(ts->error);
756 p.ts.thread_number = cpu_to_le32(ts->thread_number);
757 p.ts.groupid = cpu_to_le32(ts->groupid);
758 p.ts.pid = cpu_to_le32(ts->pid);
759 p.ts.members = cpu_to_le32(ts->members);
Jens Axboea64e88d2011-10-03 14:20:01 +0200760
761 for (i = 0; i < 2; i++) {
762 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
763 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
764 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
765 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
766 }
767
768 p.ts.usr_time = cpu_to_le64(ts->usr_time);
769 p.ts.sys_time = cpu_to_le64(ts->sys_time);
770 p.ts.ctx = cpu_to_le64(ts->ctx);
771 p.ts.minf = cpu_to_le64(ts->minf);
772 p.ts.majf = cpu_to_le64(ts->majf);
773 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200774
775 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200776 fio_fp64_t *src = &ts->percentile_list[i];
777 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200778
Jens Axboecfc03e42011-10-12 21:20:42 +0200779 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200780 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200781
782 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
783 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
784 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
785 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
786 }
787
788 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
789 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
790 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
791 }
792
793 for (i = 0; i < 2; i++)
794 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
795 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
796
797 for (i = 0; i < 3; i++) {
798 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200799 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200800 }
801
Jens Axboe93eee042011-10-03 21:08:48 +0200802 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200803 p.ts.total_complete = cpu_to_le64(ts->total_complete);
804
805 for (i = 0; i < 2; i++) {
806 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
807 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
808 }
809
810 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
811 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
812 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200813 p.ts.first_error = cpu_to_le32(ts->first_error);
814 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200815
816 convert_gs(&p.rs, rs);
817
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200818 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200819}
820
821void fio_server_send_gs(struct group_run_stats *rs)
822{
823 struct group_run_stats gs;
824
Jens Axboe60efd142011-10-04 13:30:11 +0200825 dprint(FD_NET, "server sending group run stats\n");
826
Jens Axboea64e88d2011-10-03 14:20:01 +0200827 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200828 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200829}
830
Jens Axboed09a64a2011-10-13 11:38:56 +0200831static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
832{
833 int i;
834
835 for (i = 0; i < 2; i++) {
836 dst->ios[i] = cpu_to_le32(src->ios[i]);
837 dst->merges[i] = cpu_to_le32(src->merges[i]);
838 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
839 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
840 }
841
842 dst->io_ticks = cpu_to_le32(src->io_ticks);
843 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
844 dst->slavecount = cpu_to_le32(src->slavecount);
845 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
846}
847
848static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
849{
850 int i;
851
852 strcpy((char *) dst->name, (char *) src->name);
853
854 for (i = 0; i < 2; i++) {
855 dst->ios[i] = cpu_to_le32(src->ios[i]);
856 dst->merges[i] = cpu_to_le32(src->merges[i]);
857 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
858 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
859 }
860
861 dst->io_ticks = cpu_to_le32(src->io_ticks);
862 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
863 dst->msec = cpu_to_le64(src->msec);
864}
865
866void fio_server_send_du(void)
867{
868 struct disk_util *du;
869 struct flist_head *entry;
870 struct cmd_du_pdu pdu;
871
872 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
873
Jens Axboe0766d922011-10-13 12:02:08 +0200874 memset(&pdu, 0, sizeof(pdu));
875
Jens Axboed09a64a2011-10-13 11:38:56 +0200876 flist_for_each(entry, &disk_list) {
877 du = flist_entry(entry, struct disk_util, list);
878
879 convert_dus(&pdu.dus, &du->dus);
880 convert_agg(&pdu.agg, &du->agg);
881
882 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
883 }
884}
885
Jens Axboe53bd8db2012-03-14 21:51:38 +0100886/*
887 * Send a command with a separate PDU, not inlined in the command
888 */
889static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
890 off_t size, uint64_t tag, uint32_t flags)
891{
892 struct fio_net_cmd cmd;
893 struct iovec iov[2];
894
895 iov[0].iov_base = &cmd;
896 iov[0].iov_len = sizeof(cmd);
897 iov[1].iov_base = (void *) buf;
898 iov[1].iov_len = size;
899
900 __fio_init_net_cmd(&cmd, opcode, size, tag);
901 cmd.flags = __cpu_to_le32(flags);
902 fio_net_cmd_crc_pdu(&cmd, buf);
903
904 return fio_sendv_data(server_fd, iov, 2);
905}
906
Jens Axboe1b427252012-03-14 15:03:03 +0100907int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
908{
Jens Axboef5ed7652012-03-14 16:28:07 +0100909 struct cmd_iolog_pdu pdu;
Jens Axboe1b427252012-03-14 15:03:03 +0100910 z_stream stream;
911 void *out_pdu;
Jens Axboe53bd8db2012-03-14 21:51:38 +0100912 int i, ret = 0;
Jens Axboe1b427252012-03-14 15:03:03 +0100913
Jens Axboe2f122b12012-03-15 13:10:19 +0100914 pdu.thread_number = cpu_to_le32(td->thread_number);
Jens Axboef5ed7652012-03-14 16:28:07 +0100915 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
916 pdu.log_type = cpu_to_le32(log->log_type);
917 strcpy((char *) pdu.name, name);
Jens Axboe1b427252012-03-14 15:03:03 +0100918
919 for (i = 0; i < log->nr_samples; i++) {
Jens Axboef5ed7652012-03-14 16:28:07 +0100920 struct io_sample *s = &log->log[i];
Jens Axboe1b427252012-03-14 15:03:03 +0100921
Jens Axboef5ed7652012-03-14 16:28:07 +0100922 s->time = cpu_to_le64(s->time);
923 s->val = cpu_to_le64(s->val);
924 s->ddir = cpu_to_le32(s->ddir);
925 s->bs = cpu_to_le32(s->bs);
Jens Axboe1b427252012-03-14 15:03:03 +0100926 }
927
928 /*
929 * Dirty - since the log is potentially huge, compress it into
930 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
931 * side defragment it.
932 */
933 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
934
935 stream.zalloc = Z_NULL;
936 stream.zfree = Z_NULL;
937 stream.opaque = Z_NULL;
938
939 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
Jens Axboe53bd8db2012-03-14 21:51:38 +0100940 ret = 1;
941 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +0100942 }
943
944 /*
Jens Axboef5ed7652012-03-14 16:28:07 +0100945 * Send header first, it's not compressed.
Jens Axboe1b427252012-03-14 15:03:03 +0100946 */
Jens Axboe53bd8db2012-03-14 21:51:38 +0100947 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
948 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
949 if (ret)
950 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +0100951
Jens Axboef5ed7652012-03-14 16:28:07 +0100952 stream.next_in = (void *) log->log;
953 stream.avail_in = log->nr_samples * sizeof(struct io_sample);
Jens Axboe1b427252012-03-14 15:03:03 +0100954
955 do {
Jens Axboe53bd8db2012-03-14 21:51:38 +0100956 unsigned int this_len, flags = 0;
957 int ret;
Jens Axboe1b427252012-03-14 15:03:03 +0100958
959 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
960 stream.next_out = out_pdu;
Jens Axboe3c547fe2012-03-14 21:53:00 +0100961 ret = deflate(&stream, Z_FINISH);
962 /* may be Z_OK, or Z_STREAM_END */
963 if (ret < 0)
964 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +0100965
966 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
967
Jens Axboe1b427252012-03-14 15:03:03 +0100968 if (stream.avail_in)
Jens Axboe53bd8db2012-03-14 21:51:38 +0100969 flags = FIO_NET_CMD_F_MORE;
Jens Axboe1b427252012-03-14 15:03:03 +0100970
Jens Axboe53bd8db2012-03-14 21:51:38 +0100971 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
972 out_pdu, this_len, 0, flags);
973 if (ret)
974 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +0100975 } while (stream.avail_in);
976
Jens Axboe53bd8db2012-03-14 21:51:38 +0100977err_zlib:
Jens Axboe1b427252012-03-14 15:03:03 +0100978 deflateEnd(&stream);
Jens Axboe53bd8db2012-03-14 21:51:38 +0100979err:
980 free(out_pdu);
981 return ret;
Jens Axboe1b427252012-03-14 15:03:03 +0100982}
983
Jens Axboe2f122b12012-03-15 13:10:19 +0100984void fio_server_send_add_job(struct thread_data *td)
Jens Axboe807f9972012-03-02 10:25:24 +0100985{
986 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +0100987
Jens Axboe731e30a2012-03-14 16:35:37 +0100988 memset(&pdu, 0, sizeof(pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +0100989 pdu.thread_number = cpu_to_le32(td->thread_number);
990 pdu.groupid = cpu_to_le32(td->groupid);
991 convert_thread_options_to_net(&pdu.top, &td->o);
Jens Axboe807f9972012-03-02 10:25:24 +0100992
993 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), 0);
994}
995
Jens Axboe87aa8f12011-10-06 21:24:13 +0200996static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200997{
Jens Axboe811826b2011-10-24 09:11:50 +0200998 struct sockaddr *addr;
999 fio_socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001000 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +02001001
Jens Axboe811826b2011-10-24 09:11:50 +02001002 if (use_ipv6)
1003 sk = socket(AF_INET6, SOCK_STREAM, 0);
1004 else
1005 sk = socket(AF_INET, SOCK_STREAM, 0);
1006
Jens Axboe81179ee2011-10-04 12:42:06 +02001007 if (sk < 0) {
1008 log_err("fio: socket: %s\n", strerror(errno));
1009 return -1;
1010 }
1011
1012 opt = 1;
1013 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1014 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001015 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001016 return -1;
1017 }
1018#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +02001019 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001020 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001021 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001022 return -1;
1023 }
1024#endif
1025
Jens Axboe811826b2011-10-24 09:11:50 +02001026 if (use_ipv6) {
1027 addr = (struct sockaddr *) &saddr_in6;
1028 socklen = sizeof(saddr_in6);
1029 saddr_in6.sin6_family = AF_INET6;
1030 } else {
1031 addr = (struct sockaddr *) &saddr_in;
1032 socklen = sizeof(saddr_in);
1033 saddr_in.sin_family = AF_INET;
1034 }
Jens Axboe81179ee2011-10-04 12:42:06 +02001035
Jens Axboe811826b2011-10-24 09:11:50 +02001036 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001037 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001038 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001039 return -1;
1040 }
1041
Jens Axboe87aa8f12011-10-06 21:24:13 +02001042 return sk;
1043}
1044
1045static int fio_init_server_sock(void)
1046{
1047 struct sockaddr_un addr;
1048 fio_socklen_t len;
1049 mode_t mode;
1050 int sk;
1051
1052 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1053 if (sk < 0) {
1054 log_err("fio: socket: %s\n", strerror(errno));
1055 return -1;
1056 }
1057
1058 mode = umask(000);
1059
1060 memset(&addr, 0, sizeof(addr));
1061 addr.sun_family = AF_UNIX;
1062 strcpy(addr.sun_path, bind_sock);
1063 unlink(bind_sock);
1064
1065 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1066
1067 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1068 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001069 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001070 return -1;
1071 }
1072
1073 umask(mode);
1074 return sk;
1075}
1076
1077static int fio_init_server_connection(void)
1078{
Jens Axboebebe6392011-10-07 10:00:51 +02001079 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001080 int sk;
1081
1082 dprint(FD_NET, "starting server\n");
1083
1084 if (!bind_sock)
1085 sk = fio_init_server_ip();
1086 else
1087 sk = fio_init_server_sock();
1088
1089 if (sk < 0)
1090 return sk;
1091
Jens Axboe811826b2011-10-24 09:11:50 +02001092 if (!bind_sock) {
1093 char *p, port[16];
1094 const void *src;
1095 int af;
1096
1097 if (use_ipv6) {
1098 af = AF_INET6;
1099 src = &saddr_in6.sin6_addr;
1100 } else {
1101 af = AF_INET;
1102 src = &saddr_in.sin_addr;
1103 }
1104
1105 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1106
1107 sprintf(port, ",%u", fio_net_port);
1108 if (p)
1109 strcat(p, port);
1110 else
1111 strcpy(bind_str, port);
1112 } else
Jens Axboebebe6392011-10-07 10:00:51 +02001113 strcpy(bind_str, bind_sock);
1114
1115 log_info("fio: server listening on %s\n", bind_str);
1116
Jens Axboe89c17072011-10-11 10:15:51 +02001117 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001118 log_err("fio: listen: %s\n", strerror(errno));
1119 return -1;
1120 }
1121
Jens Axboe87aa8f12011-10-06 21:24:13 +02001122 return sk;
1123}
1124
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001125int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1126 struct in6_addr *inp6)
1127
1128{
1129 int ret = 0;
1130
1131 if (*ipv6)
1132 ret = inet_pton(AF_INET6, host, inp6);
1133 else
1134 ret = inet_pton(AF_INET, host, inp);
1135
1136 if (ret != 1) {
1137 struct hostent *hent;
1138
1139 hent = gethostbyname(host);
1140 if (!hent) {
1141 log_err("fio: failed to resolve <%s>\n", host);
1142 return 0;
1143 }
1144
1145 if (*ipv6) {
1146 if (hent->h_addrtype != AF_INET6) {
1147 log_info("fio: falling back to IPv4\n");
1148 *ipv6 = 0;
1149 } else
1150 memcpy(inp6, hent->h_addr_list[0], 16);
1151 }
1152 if (!*ipv6) {
1153 if (hent->h_addrtype != AF_INET) {
1154 log_err("fio: lookup type mismatch\n");
1155 return 0;
1156 }
1157 memcpy(inp, hent->h_addr_list[0], 4);
1158 }
1159 ret = 1;
1160 }
1161
1162 return !(ret == 1);
1163}
1164
Jens Axboe660a2bf2011-10-24 09:35:06 +02001165/*
1166 * Parse a host/ip/port string. Reads from 'str'.
1167 *
1168 * Outputs:
1169 *
1170 * For IPv4:
1171 * *ptr is the host, *port is the port, inp is the destination.
1172 * For IPv6:
1173 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1174 * For local domain sockets:
1175 * *ptr is the filename, *is_sock is 1.
1176 */
Jens Axboebebe6392011-10-07 10:00:51 +02001177int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001178 int *port, struct in_addr *inp,
1179 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001180{
Jens Axboe76867622011-10-25 09:52:51 +02001181 const char *host = str;
1182 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001183 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001184
Jens Axboebebe6392011-10-07 10:00:51 +02001185 *ptr = NULL;
1186 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001187 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001188 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001189
1190 if (!strncmp(str, "sock:", 5)) {
1191 *ptr = strdup(str + 5);
1192 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001193
Jens Axboe76867622011-10-25 09:52:51 +02001194 return 0;
1195 }
1196
1197 /*
1198 * Is it ip:<ip or host>:port
1199 */
1200 if (!strncmp(host, "ip:", 3))
1201 host += 3;
1202 else if (!strncmp(host, "ip4:", 4))
1203 host += 4;
1204 else if (!strncmp(host, "ip6:", 4)) {
1205 host += 4;
1206 *ipv6 = 1;
1207 } else if (host[0] == ':') {
1208 /* String is :port */
1209 host++;
1210 lport = atoi(host);
1211 if (!lport || lport > 65535) {
1212 log_err("fio: bad server port %u\n", port);
1213 return 1;
1214 }
1215 /* no hostname given, we are done */
1216 *port = lport;
1217 return 0;
1218 }
1219
1220 /*
1221 * If no port seen yet, check if there's a last ':' at the end
1222 */
1223 if (!lport) {
1224 portp = strchr(host, ',');
1225 if (portp) {
1226 *portp = '\0';
1227 portp++;
1228 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001229 if (!lport || lport > 65535) {
1230 log_err("fio: bad server port %u\n", port);
1231 return 1;
1232 }
Jens Axboe76867622011-10-25 09:52:51 +02001233 }
1234 }
1235
1236 if (lport)
1237 *port = lport;
1238
1239 if (!strlen(host))
1240 return 0;
1241
1242 *ptr = strdup(host);
1243
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001244 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1245 free(*ptr);
1246 *ptr = NULL;
1247 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001248 }
1249
1250 if (*port == 0)
1251 *port = fio_net_port;
1252
1253 return 0;
1254}
1255
Jens Axboe87aa8f12011-10-06 21:24:13 +02001256/*
1257 * Server arg should be one of:
1258 *
1259 * sock:/path/to/socket
1260 * ip:1.2.3.4
1261 * 1.2.3.4
1262 *
1263 * Where sock uses unix domain sockets, and ip binds the server to
1264 * a specific interface. If no arguments are given to the server, it
1265 * uses IP and binds to 0.0.0.0.
1266 *
1267 */
1268static int fio_handle_server_arg(void)
1269{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001270 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001271 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001272
Jens Axboe87aa8f12011-10-06 21:24:13 +02001273 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1274
1275 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001276 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001277
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001278 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001279 &port, &saddr_in.sin_addr,
1280 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001281
1282 if (!is_sock && bind_sock) {
1283 free(bind_sock);
1284 bind_sock = NULL;
1285 }
1286
Jens Axboea7de0a12011-10-07 12:55:14 +02001287out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001288 fio_net_port = port;
1289 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001290 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001291 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001292}
1293
1294static int fio_server(void)
1295{
1296 int sk, ret;
1297
1298 dprint(FD_NET, "starting server\n");
1299
1300 if (fio_handle_server_arg())
1301 return -1;
1302
1303 sk = fio_init_server_connection();
1304 if (sk < 0)
1305 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001306
1307 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001308
Jens Axboe81179ee2011-10-04 12:42:06 +02001309 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001310
1311 if (fio_server_arg) {
1312 free(fio_server_arg);
1313 fio_server_arg = NULL;
1314 }
Jens Axboebebe6392011-10-07 10:00:51 +02001315 if (bind_sock)
1316 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001317
Jens Axboe81179ee2011-10-04 12:42:06 +02001318 return ret;
1319}
1320
Jens Axboe7b821682011-10-11 12:16:32 +02001321void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001322{
Jens Axboe7b821682011-10-11 12:16:32 +02001323 if (signal == SIGPIPE)
1324 server_fd = -1;
1325 else {
1326 log_info("\nfio: terminating on signal %d\n", signal);
1327 exit_backend = 1;
1328 }
Jens Axboe9abea482011-10-04 13:21:54 +02001329}
1330
Jens Axboe13755d92011-10-10 19:51:26 +02001331static int check_existing_pidfile(const char *pidfile)
1332{
1333 struct stat sb;
1334 char buf[16];
1335 pid_t pid;
1336 FILE *f;
1337
1338 if (stat(pidfile, &sb))
1339 return 0;
1340
1341 f = fopen(pidfile, "r");
1342 if (!f)
1343 return 0;
1344
Jens Axboebfc3b172011-10-10 21:16:55 +02001345 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001346 fclose(f);
1347 return 1;
1348 }
1349 fclose(f);
1350
1351 pid = atoi(buf);
1352 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001353 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001354
1355 return 1;
1356}
1357
1358static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001359{
1360 FILE *fpid;
1361
1362 fpid = fopen(pidfile, "w");
1363 if (!fpid) {
1364 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001365 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001366 }
1367
1368 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001369 fclose(fpid);
1370 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001371}
1372
1373/*
1374 * If pidfile is specified, background us.
1375 */
1376int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001377{
1378 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001379 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001380
Bruce Cran93bcfd22012-02-20 20:18:19 +01001381#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001382 WSADATA wsd;
1383 WSAStartup(MAKEWORD(2,2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001384#endif
1385
Jens Axboe402668f2011-10-10 15:28:58 +02001386 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001387 return fio_server();
1388
Jens Axboe13755d92011-10-10 19:51:26 +02001389 if (check_existing_pidfile(pidfile)) {
1390 log_err("fio: pidfile %s exists and server appears alive\n",
1391 pidfile);
1392 return -1;
1393 }
1394
Jens Axboee46d8092011-10-03 09:11:02 +02001395 pid = fork();
1396 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001397 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001398 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001399 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001400 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001401 int ret = write_pid(pid, pidfile);
1402
1403 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001404 }
Jens Axboee46d8092011-10-03 09:11:02 +02001405
1406 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001407 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1408 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001409 close(STDIN_FILENO);
1410 close(STDOUT_FILENO);
1411 close(STDERR_FILENO);
1412 f_out = NULL;
1413 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001414
1415 ret = fio_server();
1416
1417 closelog();
1418 unlink(pidfile);
1419 free(pidfile);
1420 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001421}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001422
Jens Axboebebe6392011-10-07 10:00:51 +02001423void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001424{
1425 fio_server_arg = strdup(arg);
1426}