blob: 33c512c78f96a5d19cd0f110b6d418d581ad1357 [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>
Jens Axboe50d16972011-09-29 17:45:28 -06007#include <sys/poll.h>
Jens Axboe50d16972011-09-29 17:45:28 -06008#include <sys/types.h>
9#include <sys/wait.h>
Jens Axboed05c4a02011-10-05 00:16:11 +020010#include <sys/socket.h>
Jens Axboe87aa8f12011-10-06 21:24:13 +020011#include <sys/stat.h>
12#include <sys/un.h>
Bruce Cran1f51bba2013-04-12 15:40:21 +020013#include <sys/uio.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 Axboe3989b142013-04-12 13:13:24 +020019#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +010020#include <zlib.h>
Jens Axboe3989b142013-04-12 13:13:24 +020021#endif
Jens Axboe50d16972011-09-29 17:45:28 -060022
23#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060024#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060025#include "crc/crc16.h"
Jens Axboec7c6cb42011-10-13 14:12:40 +020026#include "lib/ieee754.h"
Jens Axboe50d16972011-09-29 17:45:28 -060027
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 Axboe811826b2011-10-24 09:11:50 +020037static int use_ipv6;
Jens Axboe3989b142013-04-12 13:13:24 +020038#ifdef CONFIG_ZLIB
39static unsigned int has_zlib = 1;
40#else
41static unsigned int has_zlib = 0;
42#endif
43static unsigned int use_zlib;
Jens Axboe37db14f2011-09-30 17:00:42 -060044
Jens Axboe122c7722012-03-19 09:13:15 +010045struct fio_fork_item {
46 struct flist_head list;
47 int exitval;
48 int signal;
49 int exited;
50 pid_t pid;
51};
52
Jens Axboe89c17072011-10-11 10:15:51 +020053static const char *fio_server_ops[FIO_NET_CMD_NR] = {
54 "",
55 "QUIT",
56 "EXIT",
57 "JOB",
58 "JOBLINE",
59 "TEXT",
60 "TS",
61 "GS",
62 "SEND_ETA",
63 "ETA",
64 "PROBE",
65 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020066 "STOP",
67 "DISK_UTIL",
Jens Axboeb9d2f302012-03-08 20:36:28 +010068 "SERVER_START",
Jens Axboe807f9972012-03-02 10:25:24 +010069 "ADD_JOB",
Jens Axboe8d7b6182014-04-14 09:07:30 -060070 "CMD_RUN",
Jens Axboec70e63e2012-03-15 08:26:18 +010071 "CMD_IOLOG",
Jens Axboe35899182014-10-07 20:56:28 -060072 "CMD_UPDATE_JOB",
73 "CMD_LOAD_FILE",
Jens Axboe89c17072011-10-11 10:15:51 +020074};
75
76const char *fio_server_op(unsigned int op)
77{
78 static char buf[32];
79
80 if (op < FIO_NET_CMD_NR)
81 return fio_server_ops[op];
82
83 sprintf(buf, "UNKNOWN/%d", op);
84 return buf;
85}
86
Jens Axboe5235f622012-03-14 21:25:51 +010087static ssize_t iov_total_len(const struct iovec *iov, int count)
Jens Axboe132159a2011-09-30 15:01:32 -060088{
Jens Axboe5235f622012-03-14 21:25:51 +010089 ssize_t ret = 0;
90
91 while (count--) {
92 ret += iov->iov_len;
93 iov++;
94 }
95
96 return ret;
97}
98
99static int fio_sendv_data(int sk, struct iovec *iov, int count)
100{
101 ssize_t total_len = iov_total_len(iov, count);
102 ssize_t ret;
Jens Axboe794d69c2011-10-01 08:48:50 -0600103
Jens Axboe132159a2011-09-30 15:01:32 -0600104 do {
Jens Axboe5235f622012-03-14 21:25:51 +0100105 ret = writev(sk, iov, count);
Jens Axboe132159a2011-09-30 15:01:32 -0600106 if (ret > 0) {
Jens Axboe5235f622012-03-14 21:25:51 +0100107 total_len -= ret;
108 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600109 break;
Jens Axboe5235f622012-03-14 21:25:51 +0100110
111 while (ret) {
112 if (ret >= iov->iov_len) {
113 ret -= iov->iov_len;
114 iov++;
115 continue;
116 }
117 iov->iov_base += ret;
118 iov->iov_len -= ret;
119 ret = 0;
120 }
Jens Axboe132159a2011-09-30 15:01:32 -0600121 } else if (!ret)
122 break;
123 else if (errno == EAGAIN || errno == EINTR)
124 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200125 else
126 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600127 } while (!exit_backend);
128
Jens Axboe5235f622012-03-14 21:25:51 +0100129 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600130 return 0;
131
Jens Axboe8b4e61b2012-03-09 17:10:54 +0100132 if (errno)
133 return -errno;
134
Jens Axboe132159a2011-09-30 15:01:32 -0600135 return 1;
136}
137
Jens Axboe5235f622012-03-14 21:25:51 +0100138int fio_send_data(int sk, const void *p, unsigned int len)
139{
140 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
141
142 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
143
144 return fio_sendv_data(sk, &iov, 1);
145}
146
Jens Axboe132159a2011-09-30 15:01:32 -0600147int fio_recv_data(int sk, void *p, unsigned int len)
148{
149 do {
150 int ret = recv(sk, p, len, MSG_WAITALL);
151
152 if (ret > 0) {
153 len -= ret;
154 if (!len)
155 break;
156 p += ret;
157 continue;
158 } else if (!ret)
159 break;
160 else if (errno == EAGAIN || errno == EINTR)
161 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200162 else
163 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600164 } while (!exit_backend);
165
166 if (!len)
167 return 0;
168
169 return -1;
170}
171
172static int verify_convert_cmd(struct fio_net_cmd *cmd)
173{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600174 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600175
Jens Axboefcee5ff2011-09-30 22:50:39 -0600176 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
177 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600178
Jens Axboe25dfa842012-02-29 10:01:34 +0100179 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600180 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600181 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600182 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600183 return 1;
184 }
185
186 cmd->version = le16_to_cpu(cmd->version);
187 cmd->opcode = le16_to_cpu(cmd->opcode);
188 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200189 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600190 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
191
192 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200193 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600194 break;
195 default:
196 log_err("fio: bad server cmd version %d\n", cmd->version);
197 return 1;
198 }
199
Jens Axboeb9d2f302012-03-08 20:36:28 +0100200 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600201 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
202 return 1;
203 }
204
205 return 0;
206}
207
Jens Axboea64e88d2011-10-03 14:20:01 +0200208/*
209 * Read (and defragment, if necessary) incoming commands
210 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200211struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600212{
Jens Axboedfbf1f62014-04-14 11:43:47 -0600213 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
Jens Axboea64e88d2011-10-03 14:20:01 +0200214 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600215 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200216 int ret, first = 1;
217 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600218
Jens Axboea64e88d2011-10-03 14:20:01 +0200219 do {
220 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
221 if (ret)
222 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600223
Jens Axboea64e88d2011-10-03 14:20:01 +0200224 /* We have a command, verify it and swap if need be */
225 ret = verify_convert_cmd(&cmd);
226 if (ret)
227 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600228
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200229 if (first) {
230 /* if this is text, add room for \0 at the end */
231 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
232 assert(!cmdret);
233 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200234 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600235
Jens Axboedfbf1f62014-04-14 11:43:47 -0600236 if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
237 log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
238 ret = 1;
239 break;
240 }
241
242 tmp = realloc(cmdret, cmd_size);
243 if (!tmp) {
244 log_err("fio: server failed allocating cmd\n");
245 ret = 1;
246 break;
247 }
248 cmdret = tmp;
Jens Axboe132159a2011-09-30 15:01:32 -0600249
Jens Axboea64e88d2011-10-03 14:20:01 +0200250 if (first)
251 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200252 else if (cmdret->opcode != cmd.opcode) {
253 log_err("fio: fragment opcode mismatch (%d != %d)\n",
254 cmdret->opcode, cmd.opcode);
255 ret = 1;
256 break;
257 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200258
259 if (!cmd.pdu_len)
260 break;
261
262 /* There's payload, get it */
263 pdu = (void *) cmdret->payload + pdu_offset;
264 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
265 if (ret)
266 break;
267
268 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100269 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200270 if (crc != cmd.pdu_crc16) {
271 log_err("fio: server bad crc on payload ");
272 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
273 ret = 1;
274 break;
275 }
276
277 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200278 if (!first)
279 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200280 first = 0;
281 } while (cmd.flags & FIO_NET_CMD_F_MORE);
282
283 if (ret) {
284 free(cmdret);
285 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200286 } else if (cmdret) {
287 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100288 if (cmdret->pdu_len) {
289 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
290 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
291 char *buf = (char *) pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200292
Jens Axboe3c3ed072012-03-27 09:12:39 +0200293 buf[pdu->buf_len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100294 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
Jens Axboe46bcd492012-03-14 11:31:21 +0100295 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
296 char *buf = (char *) pdu->buf;
297 int len = le32_to_cpu(pdu->buf_len);
Jens Axboe084d1c62012-03-03 20:28:07 +0100298
Jens Axboe46bcd492012-03-14 11:31:21 +0100299 buf[len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100300 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200301 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100302
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200303 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200304 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200305 }
Jens Axboe132159a2011-09-30 15:01:32 -0600306
Jens Axboea64e88d2011-10-03 14:20:01 +0200307 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600308}
309
Jens Axboe40c60512012-03-27 16:03:04 +0200310static void add_reply(uint64_t tag, struct flist_head *list)
311{
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200312 struct fio_net_cmd_reply *reply;
Jens Axboe40c60512012-03-27 16:03:04 +0200313
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200314 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
Jens Axboe40c60512012-03-27 16:03:04 +0200315 flist_add_tail(&reply->list, list);
316}
317
318static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
319{
320 struct fio_net_cmd_reply *reply;
321
322 reply = calloc(1, sizeof(*reply));
323 INIT_FLIST_HEAD(&reply->list);
324 gettimeofday(&reply->tv, NULL);
325 reply->saved_tag = tag;
326 reply->opcode = opcode;
327
328 return (uintptr_t) reply;
329}
330
331static void free_reply(uint64_t tag)
332{
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200333 struct fio_net_cmd_reply *reply;
Jens Axboe40c60512012-03-27 16:03:04 +0200334
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200335 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
Jens Axboe40c60512012-03-27 16:03:04 +0200336 free(reply);
337}
338
Jens Axboe53bd8db2012-03-14 21:51:38 +0100339void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
Jens Axboe132159a2011-09-30 15:01:32 -0600340{
341 uint32_t pdu_len;
342
Jens Axboe25dfa842012-02-29 10:01:34 +0100343 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600344
345 pdu_len = le32_to_cpu(cmd->pdu_len);
Jens Axboe1b427252012-03-14 15:03:03 +0100346 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
347}
348
349void fio_net_cmd_crc(struct fio_net_cmd *cmd)
350{
351 fio_net_cmd_crc_pdu(cmd, cmd->payload);
Jens Axboe132159a2011-09-30 15:01:32 -0600352}
353
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200354int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
Jens Axboe40c60512012-03-27 16:03:04 +0200355 uint64_t *tagptr, struct flist_head *list)
Jens Axboe794d69c2011-10-01 08:48:50 -0600356{
Jens Axboe7f868312011-10-10 09:55:21 +0200357 struct fio_net_cmd *cmd = NULL;
358 size_t this_len, cur_len = 0;
Jens Axboe40c60512012-03-27 16:03:04 +0200359 uint64_t tag;
Jens Axboe794d69c2011-10-01 08:48:50 -0600360 int ret;
361
Jens Axboe40c60512012-03-27 16:03:04 +0200362 if (list) {
363 assert(tagptr);
364 tag = *tagptr = alloc_reply(*tagptr, opcode);
365 } else
366 tag = tagptr ? *tagptr : 0;
367
Jens Axboe794d69c2011-10-01 08:48:50 -0600368 do {
369 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100370 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
371 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600372
Jens Axboe7f868312011-10-10 09:55:21 +0200373 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
374 if (cmd)
375 free(cmd);
376
377 cur_len = sizeof(*cmd) + this_len;
378 cmd = malloc(cur_len);
379 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600380
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200381 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600382
383 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200384 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600385
386 fio_net_cmd_crc(cmd);
387
388 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600389 size -= this_len;
390 buf += this_len;
391 } while (!ret && size);
392
Jens Axboe40c60512012-03-27 16:03:04 +0200393 if (list) {
394 if (ret)
395 free_reply(tag);
396 else
397 add_reply(tag, list);
398 }
399
Jens Axboe7f868312011-10-10 09:55:21 +0200400 if (cmd)
401 free(cmd);
402
Jens Axboe794d69c2011-10-01 08:48:50 -0600403 return ret;
404}
405
Jens Axboe89c17072011-10-11 10:15:51 +0200406static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600407{
Jens Axboe178cde92011-10-05 22:14:31 +0200408 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600409
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200410 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600411 fio_net_cmd_crc(&cmd);
412
413 return fio_send_data(sk, &cmd, sizeof(cmd));
414}
415
Jens Axboe89c17072011-10-11 10:15:51 +0200416/*
417 * If 'list' is non-NULL, then allocate and store the sent command for
418 * later verification.
419 */
420int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
421 struct flist_head *list)
422{
Jens Axboe89c17072011-10-11 10:15:51 +0200423 int ret;
424
Jens Axboe40c60512012-03-27 16:03:04 +0200425 if (list)
426 tag = alloc_reply(tag, opcode);
Jens Axboe89c17072011-10-11 10:15:51 +0200427
Jens Axboe40c60512012-03-27 16:03:04 +0200428 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
Jens Axboe89c17072011-10-11 10:15:51 +0200429 if (ret) {
Jens Axboe40c60512012-03-27 16:03:04 +0200430 if (list)
431 free_reply(tag);
432
Jens Axboe89c17072011-10-11 10:15:51 +0200433 return ret;
434 }
435
Jens Axboe40c60512012-03-27 16:03:04 +0200436 if (list)
437 add_reply(tag, list);
438
Jens Axboe89c17072011-10-11 10:15:51 +0200439 return 0;
440}
441
Jens Axboe122c7722012-03-19 09:13:15 +0100442int fio_net_send_quit(int sk)
Jens Axboe437377e2011-10-01 08:31:30 -0600443{
Jens Axboe46c48f12011-10-01 12:50:51 -0600444 dprint(FD_NET, "server: sending quit\n");
Jens Axboe122c7722012-03-19 09:13:15 +0100445
Jens Axboe8c953072012-03-20 14:35:35 +0100446 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600447}
448
Jens Axboef58bd2a2012-03-27 10:06:42 +0200449static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error,
450 int signal)
Jens Axboe122c7722012-03-19 09:13:15 +0100451{
452 struct cmd_end_pdu epdu;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200453 uint64_t tag = 0;
Jens Axboe122c7722012-03-19 09:13:15 +0100454
Jens Axboef58bd2a2012-03-27 10:06:42 +0200455 if (cmd)
456 tag = cmd->tag;
Jens Axboe122c7722012-03-19 09:13:15 +0100457
458 epdu.error = __cpu_to_le32(error);
459 epdu.signal = __cpu_to_le32(signal);
Jens Axboe40c60512012-03-27 16:03:04 +0200460 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200461}
462
463int fio_net_send_stop(int sk, int error, int signal)
464{
465 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
466 return fio_net_send_ack(sk, NULL, error, signal);
Jens Axboe122c7722012-03-19 09:13:15 +0100467}
468
469static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
470{
471 struct fio_fork_item *ffi;
472
473 ffi = malloc(sizeof(*ffi));
474 ffi->exitval = 0;
475 ffi->signal = 0;
476 ffi->exited = 0;
477 ffi->pid = pid;
478 flist_add_tail(&ffi->list, list);
479}
480
Jens Axboe6348b5d2013-11-07 13:37:09 -0700481static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
Jens Axboe122c7722012-03-19 09:13:15 +0100482{
Jens Axboea7533db2013-11-06 11:19:42 -0700483 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
Jens Axboe6348b5d2013-11-07 13:37:09 -0700484 fio_server_add_fork_item(pid, conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100485}
486
Jens Axboe6348b5d2013-11-07 13:37:09 -0700487static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
Jens Axboe122c7722012-03-19 09:13:15 +0100488{
Jens Axboea7533db2013-11-06 11:19:42 -0700489 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
Jens Axboe6348b5d2013-11-07 13:37:09 -0700490 fio_server_add_fork_item(pid, job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100491}
492
493static void fio_server_check_fork_item(struct fio_fork_item *ffi)
494{
495 int ret, status;
496
497 ret = waitpid(ffi->pid, &status, WNOHANG);
498 if (ret < 0) {
499 if (errno == ECHILD) {
Jens Axboea7533db2013-11-06 11:19:42 -0700500 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
Jens Axboe122c7722012-03-19 09:13:15 +0100501 ffi->exited = 1;
502 } else
503 log_err("fio: waitpid: %s\n", strerror(errno));
504 } else if (ret == ffi->pid) {
505 if (WIFSIGNALED(status)) {
506 ffi->signal = WTERMSIG(status);
507 ffi->exited = 1;
508 }
509 if (WIFEXITED(status)) {
510 if (WEXITSTATUS(status))
511 ffi->exitval = WEXITSTATUS(status);
512 ffi->exited = 1;
513 }
514 }
515}
516
517static void fio_server_fork_item_done(struct fio_fork_item *ffi)
518{
Jens Axboea7533db2013-11-06 11:19:42 -0700519 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
Jens Axboe122c7722012-03-19 09:13:15 +0100520
521 /*
522 * Fold STOP and QUIT...
523 */
524 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
525 fio_net_send_quit(server_fd);
526 flist_del(&ffi->list);
527 free(ffi);
528}
529
Jens Axboe54163252012-03-23 12:25:18 +0100530static void fio_server_check_fork_items(struct flist_head *list)
Jens Axboe122c7722012-03-19 09:13:15 +0100531{
532 struct flist_head *entry, *tmp;
533 struct fio_fork_item *ffi;
534
535 flist_for_each_safe(entry, tmp, list) {
536 ffi = flist_entry(entry, struct fio_fork_item, list);
537
538 fio_server_check_fork_item(ffi);
539
540 if (ffi->exited)
541 fio_server_fork_item_done(ffi);
542 }
543}
544
Jens Axboe6348b5d2013-11-07 13:37:09 -0700545static void fio_server_check_jobs(struct flist_head *job_list)
Jens Axboe122c7722012-03-19 09:13:15 +0100546{
Jens Axboe6348b5d2013-11-07 13:37:09 -0700547 fio_server_check_fork_items(job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100548}
549
Jens Axboe6348b5d2013-11-07 13:37:09 -0700550static void fio_server_check_conns(struct flist_head *conn_list)
Jens Axboe122c7722012-03-19 09:13:15 +0100551{
Jens Axboe6348b5d2013-11-07 13:37:09 -0700552 fio_server_check_fork_items(conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100553}
554
Jens Axboe35899182014-10-07 20:56:28 -0600555static int handle_load_file_cmd(struct fio_net_cmd *cmd)
556{
557 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
558 void *file_name = pdu->file;
559 struct cmd_start_pdu spdu;
560
561 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
562
563 pdu->name_len = le16_to_cpu(pdu->name_len);
564 pdu->client_type = le16_to_cpu(pdu->client_type);
565
566 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
567 fio_net_send_quit(server_fd);
568 return -1;
569 }
570
571 spdu.jobs = cpu_to_le32(thread_number);
572 spdu.stat_outputs = cpu_to_le32(stat_number);
573 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
574 return 0;
575}
576
Jens Axboe6348b5d2013-11-07 13:37:09 -0700577static int handle_run_cmd(struct flist_head *job_list, struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600578{
Jens Axboe122c7722012-03-19 09:13:15 +0100579 pid_t pid;
Jens Axboea64e88d2011-10-03 14:20:01 +0200580 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600581
Jens Axboe386626f2014-06-09 13:25:13 -0600582 fio_time_init();
Jens Axboe122c7722012-03-19 09:13:15 +0100583 set_genesis_time();
584
585 pid = fork();
586 if (pid) {
Jens Axboe6348b5d2013-11-07 13:37:09 -0700587 fio_server_add_job_pid(job_list, pid);
Jens Axboe122c7722012-03-19 09:13:15 +0100588 return 0;
589 }
590
Jens Axboe2e1df072012-02-09 11:15:02 +0100591 ret = fio_backend();
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200592 free_threads_shm();
Jens Axboe122c7722012-03-19 09:13:15 +0100593 _exit(ret);
Jens Axboe81179ee2011-10-04 12:42:06 +0200594}
595
Jens Axboeb9d2f302012-03-08 20:36:28 +0100596static int handle_job_cmd(struct fio_net_cmd *cmd)
597{
Jens Axboe46bcd492012-03-14 11:31:21 +0100598 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
599 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100600 struct cmd_start_pdu spdu;
601
Jens Axboe46bcd492012-03-14 11:31:21 +0100602 pdu->buf_len = le32_to_cpu(pdu->buf_len);
603 pdu->client_type = le32_to_cpu(pdu->client_type);
604
605 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100606 fio_net_send_quit(server_fd);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100607 return -1;
608 }
609
610 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe108fea72012-11-14 13:09:45 -0700611 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200612 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100613 return 0;
614}
615
Jens Axboe81179ee2011-10-04 12:42:06 +0200616static int handle_jobline_cmd(struct fio_net_cmd *cmd)
617{
Jens Axboefa2ea802011-10-08 21:07:29 +0200618 void *pdu = cmd->payload;
619 struct cmd_single_line_pdu *cslp;
620 struct cmd_line_pdu *clp;
621 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100622 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200623 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100624 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200625
Jens Axboefa2ea802011-10-08 21:07:29 +0200626 clp = pdu;
627 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100628 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200629 argv = malloc(clp->lines * sizeof(char *));
630 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200631
Jens Axboefa2ea802011-10-08 21:07:29 +0200632 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200633
Jens Axboefa2ea802011-10-08 21:07:29 +0200634 for (i = 0; i < clp->lines; i++) {
635 cslp = pdu + offset;
636 argv[i] = (char *) cslp->text;
637
638 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200639 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
640 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200641
Jens Axboe46bcd492012-03-14 11:31:21 +0100642 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100643 fio_net_send_quit(server_fd);
Jens Axboefa2ea802011-10-08 21:07:29 +0200644 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200645 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200646 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200647
Jens Axboefa2ea802011-10-08 21:07:29 +0200648 free(argv);
649
Jens Axboeb9d2f302012-03-08 20:36:28 +0100650 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe1e5324e2012-11-14 14:25:31 -0700651 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200652 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100653 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600654}
655
Jens Axboec28e8e82011-10-04 08:57:39 +0200656static int handle_probe_cmd(struct fio_net_cmd *cmd)
657{
Jens Axboe3989b142013-04-12 13:13:24 +0200658 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
659 struct cmd_probe_reply_pdu probe;
Jens Axboe40c60512012-03-27 16:03:04 +0200660 uint64_t tag = cmd->tag;
Jens Axboec28e8e82011-10-04 08:57:39 +0200661
Jens Axboe89c17072011-10-11 10:15:51 +0200662 dprint(FD_NET, "server: sending probe reply\n");
663
Jens Axboec28e8e82011-10-04 08:57:39 +0200664 memset(&probe, 0, sizeof(probe));
665 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe0dcebdf2013-01-23 15:42:16 -0700666#ifdef CONFIG_BIG_ENDIAN
Jens Axboe6eb24792011-10-04 15:00:30 +0200667 probe.bigendian = 1;
668#endif
Jens Axboe750db472012-04-16 11:44:48 +0200669 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
Jens Axboec28e8e82011-10-04 08:57:39 +0200670
Jens Axboecca84642011-10-07 12:47:57 +0200671 probe.os = FIO_OS;
672 probe.arch = FIO_ARCH;
Jens Axboe38fdef22011-10-11 14:30:06 +0200673 probe.bpp = sizeof(void *);
Jens Axboed31e26d2012-03-28 21:38:24 +0200674 probe.cpus = __cpu_to_le32(cpus_online());
Jens Axboe3989b142013-04-12 13:13:24 +0200675
676 /*
677 * If the client supports compression and we do too, then enable it
678 */
679 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
680 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
681 use_zlib = 1;
682 } else {
683 probe.flags = 0;
684 use_zlib = 0;
685 }
Jens Axboe38fdef22011-10-11 14:30:06 +0200686
Jens Axboe40c60512012-03-27 16:03:04 +0200687 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200688}
689
690static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
691{
692 struct jobs_eta *je;
Jens Axboe40c60512012-03-27 16:03:04 +0200693 uint64_t tag = cmd->tag;
Jens Axboe61f6cce2014-06-24 08:40:21 -0600694 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200695 int i;
696
Jens Axboe61f6cce2014-06-24 08:40:21 -0600697 je = get_jobs_eta(1, &size);
698 if (!je)
Jens Axboeb814fb22011-10-12 09:20:34 +0200699 return 0;
700
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200701 dprint(FD_NET, "server sending status\n");
702
703 je->nr_running = cpu_to_le32(je->nr_running);
704 je->nr_ramp = cpu_to_le32(je->nr_ramp);
705 je->nr_pending = cpu_to_le32(je->nr_pending);
Jens Axboe714e85f2013-04-15 10:21:56 +0200706 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200707 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200708
Jens Axboe298921d2012-09-24 18:47:01 +0200709 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100710 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
711 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
712 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
713 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboe16725bb2013-04-11 12:43:05 +0200714 je->rate[i] = cpu_to_le32(je->rate[i]);
715 je->iops[i] = cpu_to_le32(je->iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200716 }
717
Jens Axboec1970b92012-03-06 15:37:40 +0100718 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200719 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100720 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200721 je->is_pow2 = cpu_to_le32(je->is_pow2);
Jens Axboeed2c0a12013-04-11 12:55:16 +0200722 je->unit_base = cpu_to_le32(je->unit_base);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200723
Jens Axboe40c60512012-03-27 16:03:04 +0200724 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200725 free(je);
726 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200727}
728
Jens Axboe40c60512012-03-27 16:03:04 +0200729static int send_update_job_reply(int fd, uint64_t __tag, int error)
730{
731 uint64_t tag = __tag;
732 uint32_t pdu_error;
733
734 pdu_error = __cpu_to_le32(error);
735 return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL);
736}
737
Jens Axboef58bd2a2012-03-27 10:06:42 +0200738static int handle_update_job_cmd(struct fio_net_cmd *cmd)
739{
740 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
741 struct thread_data *td;
742 uint32_t tnumber;
743
744 tnumber = le32_to_cpu(pdu->thread_number);
745
746 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
747
Jens Axboe30ffacb2012-03-28 09:15:05 +0200748 if (!tnumber || tnumber > thread_number) {
Jens Axboe40c60512012-03-27 16:03:04 +0200749 send_update_job_reply(server_fd, cmd->tag, ENODEV);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200750 return 0;
751 }
752
Jens Axboe30ffacb2012-03-28 09:15:05 +0200753 td = &threads[tnumber - 1];
Jens Axboef58bd2a2012-03-27 10:06:42 +0200754 convert_thread_options_to_cpu(&td->o, &pdu->top);
Jens Axboe40c60512012-03-27 16:03:04 +0200755 send_update_job_reply(server_fd, cmd->tag, 0);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200756 return 0;
757}
758
Jens Axboe6348b5d2013-11-07 13:37:09 -0700759static int handle_command(struct flist_head *job_list, struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600760{
761 int ret;
762
Jens Axboe4b91ee82013-02-25 10:18:33 +0100763 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
764 fio_server_op(cmd->opcode), cmd->pdu_len,
765 (unsigned long long) cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600766
Jens Axboe132159a2011-09-30 15:01:32 -0600767 switch (cmd->opcode) {
768 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200769 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200770 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200771 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600772 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200773 return -1;
Jens Axboe35899182014-10-07 20:56:28 -0600774 case FIO_NET_CMD_LOAD_FILE:
775 ret = handle_load_file_cmd(cmd);
776 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600777 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200778 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600779 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200780 case FIO_NET_CMD_JOBLINE:
781 ret = handle_jobline_cmd(cmd);
782 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200783 case FIO_NET_CMD_PROBE:
784 ret = handle_probe_cmd(cmd);
785 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200786 case FIO_NET_CMD_SEND_ETA:
787 ret = handle_send_eta_cmd(cmd);
788 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100789 case FIO_NET_CMD_RUN:
Jens Axboe6348b5d2013-11-07 13:37:09 -0700790 ret = handle_run_cmd(job_list, cmd);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100791 break;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200792 case FIO_NET_CMD_UPDATE_JOB:
793 ret = handle_update_job_cmd(cmd);
794 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600795 default:
Jens Axboe3c3ed072012-03-27 09:12:39 +0200796 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600797 ret = 1;
798 }
799
800 return ret;
801}
802
Jens Axboe122c7722012-03-19 09:13:15 +0100803static int handle_connection(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600804{
805 struct fio_net_cmd *cmd = NULL;
Jens Axboe6348b5d2013-11-07 13:37:09 -0700806 FLIST_HEAD(job_list);
Jens Axboe132159a2011-09-30 15:01:32 -0600807 int ret = 0;
808
Jens Axboe122c7722012-03-19 09:13:15 +0100809 reset_fio_state();
Jens Axboe122c7722012-03-19 09:13:15 +0100810 server_fd = sk;
811
Jens Axboe132159a2011-09-30 15:01:32 -0600812 /* read forever */
813 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200814 struct pollfd pfd = {
815 .fd = sk,
816 .events = POLLIN,
817 };
818
819 ret = 0;
820 do {
Jens Axboe54163252012-03-23 12:25:18 +0100821 int timeout = 1000;
822
823 if (!flist_empty(&job_list))
824 timeout = 100;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200825
Jens Axboe54163252012-03-23 12:25:18 +0100826 ret = poll(&pfd, 1, timeout);
Jens Axboee951bdc2011-10-05 21:58:45 +0200827 if (ret < 0) {
828 if (errno == EINTR)
829 break;
830 log_err("fio: poll: %s\n", strerror(errno));
831 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200832 } else if (!ret) {
Jens Axboe6348b5d2013-11-07 13:37:09 -0700833 fio_server_check_jobs(&job_list);
Jens Axboee951bdc2011-10-05 21:58:45 +0200834 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200835 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200836
837 if (pfd.revents & POLLIN)
838 break;
839 if (pfd.revents & (POLLERR|POLLHUP)) {
840 ret = 1;
841 break;
842 }
Jens Axboe19c65172011-10-05 22:05:37 +0200843 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200844
Jens Axboe6348b5d2013-11-07 13:37:09 -0700845 fio_server_check_jobs(&job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100846
Jens Axboee951bdc2011-10-05 21:58:45 +0200847 if (ret < 0)
848 break;
849
850 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600851 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200852 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600853 break;
854 }
855
Jens Axboe6348b5d2013-11-07 13:37:09 -0700856 ret = handle_command(&job_list, cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600857 if (ret)
858 break;
859
860 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400861 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600862 }
863
864 if (cmd)
865 free(cmd);
866
Jens Axboe122c7722012-03-19 09:13:15 +0100867 close(sk);
868 _exit(ret);
Jens Axboecc0df002011-10-03 20:53:32 +0200869}
870
Jens Axboe50d16972011-09-29 17:45:28 -0600871static int accept_loop(int listen_sk)
872{
Jens Axboebb447a22011-10-04 15:06:42 +0200873 struct sockaddr_in addr;
Jens Axboe479471c2014-01-23 20:42:06 -0800874 struct sockaddr_in6 addr6;
875 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600876 struct pollfd pfd;
Jens Axboe4a851612014-04-14 10:04:21 -0600877 int ret = 0, sk, exitval = 0;
Jens Axboe6348b5d2013-11-07 13:37:09 -0700878 FLIST_HEAD(conn_list);
Jens Axboe50d16972011-09-29 17:45:28 -0600879
Jens Axboe60efd142011-10-04 13:30:11 +0200880 dprint(FD_NET, "server enter accept loop\n");
881
Jens Axboe4a851612014-04-14 10:04:21 -0600882 fio_set_fd_nonblocking(listen_sk, "server");
Jens Axboe122c7722012-03-19 09:13:15 +0100883
884 while (!exit_backend) {
Jens Axboe479471c2014-01-23 20:42:06 -0800885 const char *from;
886 char buf[64];
Jens Axboe122c7722012-03-19 09:13:15 +0100887 pid_t pid;
888
889 pfd.fd = listen_sk;
890 pfd.events = POLLIN;
891 do {
Jens Axboe54163252012-03-23 12:25:18 +0100892 int timeout = 1000;
893
894 if (!flist_empty(&conn_list))
895 timeout = 100;
896
897 ret = poll(&pfd, 1, timeout);
Jens Axboe122c7722012-03-19 09:13:15 +0100898 if (ret < 0) {
899 if (errno == EINTR)
900 break;
901 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600902 break;
Jens Axboe122c7722012-03-19 09:13:15 +0100903 } else if (!ret) {
Jens Axboe6348b5d2013-11-07 13:37:09 -0700904 fio_server_check_conns(&conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100905 continue;
906 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600907
Jens Axboe122c7722012-03-19 09:13:15 +0100908 if (pfd.revents & POLLIN)
909 break;
910 } while (!exit_backend);
911
Jens Axboe6348b5d2013-11-07 13:37:09 -0700912 fio_server_check_conns(&conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100913
914 if (exit_backend || ret < 0)
Jens Axboe009b1be2011-09-29 18:27:02 -0600915 break;
Jens Axboe009b1be2011-09-29 18:27:02 -0600916
Jens Axboe479471c2014-01-23 20:42:06 -0800917 if (use_ipv6)
918 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
919 else
920 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
921
Jens Axboe122c7722012-03-19 09:13:15 +0100922 if (sk < 0) {
923 log_err("fio: accept: %s\n", strerror(errno));
924 return -1;
925 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600926
Jens Axboe479471c2014-01-23 20:42:06 -0800927 if (use_ipv6)
928 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
929 else
930 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
931
932 dprint(FD_NET, "server: connect from %s\n", from);
Jens Axboe122c7722012-03-19 09:13:15 +0100933
934 pid = fork();
935 if (pid) {
936 close(sk);
Jens Axboe6348b5d2013-11-07 13:37:09 -0700937 fio_server_add_conn_pid(&conn_list, pid);
Jens Axboe122c7722012-03-19 09:13:15 +0100938 continue;
939 }
940
941 /* exits */
942 handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -0600943 }
944
Jens Axboe132159a2011-09-30 15:01:32 -0600945 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600946}
947
Jens Axboe084d1c62012-03-03 20:28:07 +0100948int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600949{
Jens Axboe084d1c62012-03-03 20:28:07 +0100950 struct cmd_text_pdu *pdu;
951 unsigned int tlen;
952 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600953
Jens Axboe084d1c62012-03-03 20:28:07 +0100954 if (server_fd == -1)
955 return log_local_buf(buf, len);
956
957 tlen = sizeof(*pdu) + len;
958 pdu = malloc(tlen);
959
960 pdu->level = __cpu_to_le32(level);
961 pdu->buf_len = __cpu_to_le32(len);
962
963 gettimeofday(&tv, NULL);
964 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
965 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
966
967 memcpy(pdu->buf, buf, len);
968
Jens Axboe40c60512012-03-27 16:03:04 +0200969 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL);
Jens Axboe084d1c62012-03-03 20:28:07 +0100970 free(pdu);
971 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600972}
973
Jens Axboea64e88d2011-10-03 14:20:01 +0200974static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
975{
976 dst->max_val = cpu_to_le64(src->max_val);
977 dst->min_val = cpu_to_le64(src->min_val);
978 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200979
980 /*
981 * Encode to IEEE 754 for network transfer
982 */
983 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
984 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200985}
986
987static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
988{
989 int i;
990
Jens Axboe298921d2012-09-24 18:47:01 +0200991 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200992 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
993 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
994 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
995 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
996 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
997 dst->agg[i] = cpu_to_le64(src->agg[i]);
998 }
999
1000 dst->kb_base = cpu_to_le32(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -07001001 dst->unit_base = cpu_to_le32(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +02001002 dst->groupid = cpu_to_le32(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +01001003 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +02001004}
1005
1006/*
1007 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1008 * into a single payload.
1009 */
1010void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1011{
1012 struct cmd_ts_pdu p;
1013 int i, j;
1014
Jens Axboe60efd142011-10-04 13:30:11 +02001015 dprint(FD_NET, "server sending end stats\n");
1016
Jens Axboe317b3c82011-10-03 21:47:27 +02001017 memset(&p, 0, sizeof(p));
1018
Jens Axboe4e59d0f2014-03-14 08:41:39 -06001019 strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1);
1020 strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1);
1021 strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1);
Jens Axboea64e88d2011-10-03 14:20:01 +02001022
Jens Axboe2f122b12012-03-15 13:10:19 +01001023 p.ts.error = cpu_to_le32(ts->error);
1024 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1025 p.ts.groupid = cpu_to_le32(ts->groupid);
1026 p.ts.pid = cpu_to_le32(ts->pid);
1027 p.ts.members = cpu_to_le32(ts->members);
Jens Axboe771e58b2013-01-30 12:56:23 +01001028 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +02001029
Jens Axboe298921d2012-09-24 18:47:01 +02001030 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001031 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1032 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1033 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1034 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1035 }
1036
1037 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1038 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1039 p.ts.ctx = cpu_to_le64(ts->ctx);
1040 p.ts.minf = cpu_to_le64(ts->minf);
1041 p.ts.majf = cpu_to_le64(ts->majf);
1042 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +02001043
1044 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +02001045 fio_fp64_t *src = &ts->percentile_list[i];
1046 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +02001047
Jens Axboecfc03e42011-10-12 21:20:42 +02001048 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +02001049 }
Jens Axboea64e88d2011-10-03 14:20:01 +02001050
1051 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1052 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1053 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1054 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1055 }
1056
1057 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1058 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1059 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1060 }
1061
Jens Axboe298921d2012-09-24 18:47:01 +02001062 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +02001063 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1064 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1065
Jens Axboe78799de2013-01-29 20:53:52 +01001066 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001067 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +02001068 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboe67e149c2014-10-09 13:27:44 -06001069 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +02001070 }
1071
Jens Axboe93eee042011-10-03 21:08:48 +02001072 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +02001073 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1074
Jens Axboe298921d2012-09-24 18:47:01 +02001075 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001076 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1077 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1078 }
1079
1080 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1081 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1082 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +02001083 p.ts.first_error = cpu_to_le32(ts->first_error);
1084 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -07001085 p.ts.unit_base = cpu_to_le32(ts->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +02001086
Jens Axboe3e260a42013-12-09 12:38:53 -07001087 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1088 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1089 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1090 p.ts.latency_percentile.u.i = __cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1091
Jens Axboea64e88d2011-10-03 14:20:01 +02001092 convert_gs(&p.rs, rs);
1093
Jens Axboe40c60512012-03-27 16:03:04 +02001094 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL);
Jens Axboea64e88d2011-10-03 14:20:01 +02001095}
1096
1097void fio_server_send_gs(struct group_run_stats *rs)
1098{
1099 struct group_run_stats gs;
1100
Jens Axboe60efd142011-10-04 13:30:11 +02001101 dprint(FD_NET, "server sending group run stats\n");
1102
Jens Axboea64e88d2011-10-03 14:20:01 +02001103 convert_gs(&gs, rs);
Jens Axboe40c60512012-03-27 16:03:04 +02001104 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL);
Jens Axboecf451d12011-10-03 16:48:30 +02001105}
1106
Jens Axboed09a64a2011-10-13 11:38:56 +02001107static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1108{
1109 int i;
1110
1111 for (i = 0; i < 2; i++) {
1112 dst->ios[i] = cpu_to_le32(src->ios[i]);
1113 dst->merges[i] = cpu_to_le32(src->merges[i]);
1114 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1115 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1116 }
1117
1118 dst->io_ticks = cpu_to_le32(src->io_ticks);
1119 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1120 dst->slavecount = cpu_to_le32(src->slavecount);
1121 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1122}
1123
1124static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1125{
1126 int i;
1127
Jens Axboe59140422014-04-14 08:28:08 -06001128 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1129 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
Jens Axboed09a64a2011-10-13 11:38:56 +02001130
1131 for (i = 0; i < 2; i++) {
Jens Axboea3b4cf72014-04-11 12:17:53 -06001132 dst->s.ios[i] = cpu_to_le32(src->s.ios[i]);
1133 dst->s.merges[i] = cpu_to_le32(src->s.merges[i]);
1134 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1135 dst->s.ticks[i] = cpu_to_le32(src->s.ticks[i]);
Jens Axboed09a64a2011-10-13 11:38:56 +02001136 }
1137
Jens Axboea3b4cf72014-04-11 12:17:53 -06001138 dst->s.io_ticks = cpu_to_le32(src->s.io_ticks);
1139 dst->s.time_in_queue = cpu_to_le32(src->s.time_in_queue);
1140 dst->s.msec = cpu_to_le64(src->s.msec);
Jens Axboed09a64a2011-10-13 11:38:56 +02001141}
1142
1143void fio_server_send_du(void)
1144{
1145 struct disk_util *du;
1146 struct flist_head *entry;
1147 struct cmd_du_pdu pdu;
1148
1149 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1150
Jens Axboe0766d922011-10-13 12:02:08 +02001151 memset(&pdu, 0, sizeof(pdu));
1152
Jens Axboed09a64a2011-10-13 11:38:56 +02001153 flist_for_each(entry, &disk_list) {
1154 du = flist_entry(entry, struct disk_util, list);
1155
1156 convert_dus(&pdu.dus, &du->dus);
1157 convert_agg(&pdu.agg, &du->agg);
1158
Jens Axboe40c60512012-03-27 16:03:04 +02001159 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboed09a64a2011-10-13 11:38:56 +02001160 }
1161}
1162
Jens Axboe53bd8db2012-03-14 21:51:38 +01001163/*
1164 * Send a command with a separate PDU, not inlined in the command
1165 */
1166static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1167 off_t size, uint64_t tag, uint32_t flags)
1168{
1169 struct fio_net_cmd cmd;
1170 struct iovec iov[2];
1171
Jens Axboe9f247262014-03-03 13:53:39 -07001172 iov[0].iov_base = (void *) &cmd;
Jens Axboe53bd8db2012-03-14 21:51:38 +01001173 iov[0].iov_len = sizeof(cmd);
1174 iov[1].iov_base = (void *) buf;
1175 iov[1].iov_len = size;
1176
1177 __fio_init_net_cmd(&cmd, opcode, size, tag);
1178 cmd.flags = __cpu_to_le32(flags);
1179 fio_net_cmd_crc_pdu(&cmd, buf);
1180
Jens Axboe8c953072012-03-20 14:35:35 +01001181 return fio_sendv_data(sk, iov, 2);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001182}
1183
Jens Axboe3989b142013-04-12 13:13:24 +02001184static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log)
Jens Axboe1b427252012-03-14 15:03:03 +01001185{
Jens Axboe3989b142013-04-12 13:13:24 +02001186 int ret = 0;
1187#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +01001188 z_stream stream;
1189 void *out_pdu;
Jens Axboe1b427252012-03-14 15:03:03 +01001190
1191 /*
1192 * Dirty - since the log is potentially huge, compress it into
1193 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1194 * side defragment it.
1195 */
1196 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1197
1198 stream.zalloc = Z_NULL;
1199 stream.zfree = Z_NULL;
1200 stream.opaque = Z_NULL;
1201
1202 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001203 ret = 1;
1204 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001205 }
1206
Jens Axboef5ed7652012-03-14 16:28:07 +01001207 stream.next_in = (void *) log->log;
Jens Axboeccefd5f2014-06-30 20:59:03 -06001208 stream.avail_in = log->nr_samples * log_entry_sz(log);
Jens Axboe1b427252012-03-14 15:03:03 +01001209
1210 do {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001211 unsigned int this_len, flags = 0;
1212 int ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001213
1214 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1215 stream.next_out = out_pdu;
Jens Axboe3c547fe2012-03-14 21:53:00 +01001216 ret = deflate(&stream, Z_FINISH);
1217 /* may be Z_OK, or Z_STREAM_END */
1218 if (ret < 0)
1219 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001220
1221 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1222
Jens Axboe1b427252012-03-14 15:03:03 +01001223 if (stream.avail_in)
Jens Axboe53bd8db2012-03-14 21:51:38 +01001224 flags = FIO_NET_CMD_F_MORE;
Jens Axboe1b427252012-03-14 15:03:03 +01001225
Jens Axboe53bd8db2012-03-14 21:51:38 +01001226 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
1227 out_pdu, this_len, 0, flags);
1228 if (ret)
1229 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001230 } while (stream.avail_in);
1231
Jens Axboe53bd8db2012-03-14 21:51:38 +01001232err_zlib:
Jens Axboe1b427252012-03-14 15:03:03 +01001233 deflateEnd(&stream);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001234err:
1235 free(out_pdu);
Jens Axboe3989b142013-04-12 13:13:24 +02001236#endif
Jens Axboe53bd8db2012-03-14 21:51:38 +01001237 return ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001238}
1239
Jens Axboe3989b142013-04-12 13:13:24 +02001240int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1241{
1242 struct cmd_iolog_pdu pdu;
1243 int i, ret = 0;
1244
Jens Axboeccefd5f2014-06-30 20:59:03 -06001245 pdu.nr_samples = cpu_to_le64(log->nr_samples);
Jens Axboe3989b142013-04-12 13:13:24 +02001246 pdu.thread_number = cpu_to_le32(td->thread_number);
Jens Axboe3989b142013-04-12 13:13:24 +02001247 pdu.log_type = cpu_to_le32(log->log_type);
1248 pdu.compressed = cpu_to_le32(use_zlib);
Jens Axboe9bdb9262014-04-14 11:45:34 -06001249
1250 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1251 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
Jens Axboe3989b142013-04-12 13:13:24 +02001252
1253 for (i = 0; i < log->nr_samples; i++) {
Jens Axboeccefd5f2014-06-30 20:59:03 -06001254 struct io_sample *s = get_sample(log, i);
Jens Axboe3989b142013-04-12 13:13:24 +02001255
Jens Axboebac4af12014-07-03 13:42:28 -06001256 s->time = cpu_to_le64(s->time);
1257 s->val = cpu_to_le64(s->val);
1258 s->__ddir = cpu_to_le32(s->__ddir);
1259 s->bs = cpu_to_le32(s->bs);
Jens Axboeccefd5f2014-06-30 20:59:03 -06001260
1261 if (log->log_offset) {
1262 struct io_sample_offset *so = (void *) s;
1263
1264 so->offset = cpu_to_le64(so->offset);
1265 }
Jens Axboe3989b142013-04-12 13:13:24 +02001266 }
1267
1268 /*
1269 * Send header first, it's not compressed.
1270 */
1271 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1272 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1273 if (ret)
1274 return ret;
1275
1276 /*
1277 * Now send actual log, compress if we can, otherwise just plain
1278 */
1279 if (use_zlib)
1280 return fio_send_iolog_gz(&pdu, log);
1281
1282 return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log,
Jens Axboeccefd5f2014-06-30 20:59:03 -06001283 log->nr_samples * log_entry_sz(log), 0, 0);
Jens Axboe3989b142013-04-12 13:13:24 +02001284}
1285
Jens Axboe2f122b12012-03-15 13:10:19 +01001286void fio_server_send_add_job(struct thread_data *td)
Jens Axboe807f9972012-03-02 10:25:24 +01001287{
1288 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +01001289
Jens Axboe731e30a2012-03-14 16:35:37 +01001290 memset(&pdu, 0, sizeof(pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +01001291 pdu.thread_number = cpu_to_le32(td->thread_number);
1292 pdu.groupid = cpu_to_le32(td->groupid);
1293 convert_thread_options_to_net(&pdu.top, &td->o);
Jens Axboe807f9972012-03-02 10:25:24 +01001294
Jens Axboe40c60512012-03-27 16:03:04 +02001295 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboe807f9972012-03-02 10:25:24 +01001296}
1297
Jens Axboe122c7722012-03-19 09:13:15 +01001298void fio_server_send_start(struct thread_data *td)
1299{
1300 assert(server_fd != -1);
1301
1302 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1303}
1304
Jens Axboe87aa8f12011-10-06 21:24:13 +02001305static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +02001306{
Jens Axboe811826b2011-10-24 09:11:50 +02001307 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001308 socklen_t socklen;
Jens Axboe17e35312014-03-25 10:50:55 -07001309 char buf[80];
1310 const char *str;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001311 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +02001312
Jens Axboe811826b2011-10-24 09:11:50 +02001313 if (use_ipv6)
1314 sk = socket(AF_INET6, SOCK_STREAM, 0);
1315 else
1316 sk = socket(AF_INET, SOCK_STREAM, 0);
1317
Jens Axboe81179ee2011-10-04 12:42:06 +02001318 if (sk < 0) {
1319 log_err("fio: socket: %s\n", strerror(errno));
1320 return -1;
1321 }
1322
1323 opt = 1;
Jens Axboe1f819912013-01-23 17:21:41 -07001324 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001325 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001326 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001327 return -1;
1328 }
1329#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +02001330 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001331 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001332 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001333 return -1;
1334 }
1335#endif
1336
Jens Axboe811826b2011-10-24 09:11:50 +02001337 if (use_ipv6) {
Jens Axboe17e35312014-03-25 10:50:55 -07001338 const void *src = &saddr_in6.sin6_addr;
1339
Jens Axboe811826b2011-10-24 09:11:50 +02001340 addr = (struct sockaddr *) &saddr_in6;
1341 socklen = sizeof(saddr_in6);
1342 saddr_in6.sin6_family = AF_INET6;
Jens Axboe17e35312014-03-25 10:50:55 -07001343 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
Jens Axboe811826b2011-10-24 09:11:50 +02001344 } else {
Jens Axboe17e35312014-03-25 10:50:55 -07001345 const void *src = &saddr_in.sin_addr;
1346
Jens Axboe811826b2011-10-24 09:11:50 +02001347 addr = (struct sockaddr *) &saddr_in;
1348 socklen = sizeof(saddr_in);
1349 saddr_in.sin_family = AF_INET;
Jens Axboe17e35312014-03-25 10:50:55 -07001350 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
Jens Axboe811826b2011-10-24 09:11:50 +02001351 }
Jens Axboe81179ee2011-10-04 12:42:06 +02001352
Jens Axboe811826b2011-10-24 09:11:50 +02001353 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001354 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboe17e35312014-03-25 10:50:55 -07001355 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
Jens Axboeb94cba42011-10-06 21:27:10 +02001356 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001357 return -1;
1358 }
1359
Jens Axboe87aa8f12011-10-06 21:24:13 +02001360 return sk;
1361}
1362
1363static int fio_init_server_sock(void)
1364{
1365 struct sockaddr_un addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001366 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001367 mode_t mode;
1368 int sk;
1369
1370 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1371 if (sk < 0) {
1372 log_err("fio: socket: %s\n", strerror(errno));
1373 return -1;
1374 }
1375
1376 mode = umask(000);
1377
1378 memset(&addr, 0, sizeof(addr));
1379 addr.sun_family = AF_UNIX;
Jens Axboea48fddb2014-04-14 08:55:13 -06001380 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001381
1382 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1383
1384 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1385 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001386 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001387 return -1;
1388 }
1389
1390 umask(mode);
1391 return sk;
1392}
1393
1394static int fio_init_server_connection(void)
1395{
Jens Axboebebe6392011-10-07 10:00:51 +02001396 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001397 int sk;
1398
1399 dprint(FD_NET, "starting server\n");
1400
1401 if (!bind_sock)
1402 sk = fio_init_server_ip();
1403 else
1404 sk = fio_init_server_sock();
1405
1406 if (sk < 0)
1407 return sk;
1408
Jens Axboeafdcad22014-04-14 08:54:09 -06001409 memset(bind_str, 0, sizeof(bind_str));
1410
Jens Axboe811826b2011-10-24 09:11:50 +02001411 if (!bind_sock) {
1412 char *p, port[16];
1413 const void *src;
1414 int af;
1415
1416 if (use_ipv6) {
1417 af = AF_INET6;
1418 src = &saddr_in6.sin6_addr;
1419 } else {
1420 af = AF_INET;
1421 src = &saddr_in.sin_addr;
1422 }
1423
1424 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1425
1426 sprintf(port, ",%u", fio_net_port);
1427 if (p)
1428 strcat(p, port);
1429 else
Jens Axboeafdcad22014-04-14 08:54:09 -06001430 strncpy(bind_str, port, sizeof(bind_str) - 1);
Jens Axboe811826b2011-10-24 09:11:50 +02001431 } else
Jens Axboeafdcad22014-04-14 08:54:09 -06001432 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
Jens Axboebebe6392011-10-07 10:00:51 +02001433
1434 log_info("fio: server listening on %s\n", bind_str);
1435
Jens Axboe89c17072011-10-11 10:15:51 +02001436 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001437 log_err("fio: listen: %s\n", strerror(errno));
Jens Axboe2fd973c2014-04-08 21:13:42 -06001438 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001439 return -1;
1440 }
1441
Jens Axboe87aa8f12011-10-06 21:24:13 +02001442 return sk;
1443}
1444
Jens Axboe3aa3cee2014-01-24 18:56:56 -08001445int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001446 struct in6_addr *inp6)
1447
1448{
1449 int ret = 0;
1450
Jens Axboe3aa3cee2014-01-24 18:56:56 -08001451 if (ipv6)
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001452 ret = inet_pton(AF_INET6, host, inp6);
1453 else
1454 ret = inet_pton(AF_INET, host, inp);
1455
1456 if (ret != 1) {
Jens Axboe479471c2014-01-23 20:42:06 -08001457 struct addrinfo hints, *res;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001458
Jens Axboe479471c2014-01-23 20:42:06 -08001459 memset(&hints, 0, sizeof(hints));
Jens Axboe3aa3cee2014-01-24 18:56:56 -08001460 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
Jens Axboe479471c2014-01-23 20:42:06 -08001461 hints.ai_socktype = SOCK_STREAM;
1462
1463 ret = getaddrinfo(host, NULL, &hints, &res);
1464 if (ret) {
1465 log_err("fio: failed to resolve <%s> (%s)\n", host,
1466 gai_strerror(ret));
Jens Axboe3caf43e2014-01-24 18:52:16 -08001467 return 1;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001468 }
1469
Jens Axboe3aa3cee2014-01-24 18:56:56 -08001470 if (ipv6)
Jens Axboe479471c2014-01-23 20:42:06 -08001471 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
1472 else
1473 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
1474
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001475 ret = 1;
Jens Axboe479471c2014-01-23 20:42:06 -08001476 freeaddrinfo(res);
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001477 }
1478
1479 return !(ret == 1);
1480}
1481
Jens Axboe660a2bf2011-10-24 09:35:06 +02001482/*
1483 * Parse a host/ip/port string. Reads from 'str'.
1484 *
1485 * Outputs:
1486 *
1487 * For IPv4:
1488 * *ptr is the host, *port is the port, inp is the destination.
1489 * For IPv6:
1490 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1491 * For local domain sockets:
1492 * *ptr is the filename, *is_sock is 1.
1493 */
Jens Axboebebe6392011-10-07 10:00:51 +02001494int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001495 int *port, struct in_addr *inp,
1496 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001497{
Jens Axboe76867622011-10-25 09:52:51 +02001498 const char *host = str;
1499 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001500 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001501
Jens Axboebebe6392011-10-07 10:00:51 +02001502 *ptr = NULL;
1503 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001504 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001505 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001506
1507 if (!strncmp(str, "sock:", 5)) {
1508 *ptr = strdup(str + 5);
1509 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001510
Jens Axboe76867622011-10-25 09:52:51 +02001511 return 0;
1512 }
1513
1514 /*
1515 * Is it ip:<ip or host>:port
1516 */
1517 if (!strncmp(host, "ip:", 3))
1518 host += 3;
1519 else if (!strncmp(host, "ip4:", 4))
1520 host += 4;
1521 else if (!strncmp(host, "ip6:", 4)) {
1522 host += 4;
1523 *ipv6 = 1;
1524 } else if (host[0] == ':') {
1525 /* String is :port */
1526 host++;
1527 lport = atoi(host);
1528 if (!lport || lport > 65535) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +02001529 log_err("fio: bad server port %u\n", lport);
Jens Axboe76867622011-10-25 09:52:51 +02001530 return 1;
1531 }
1532 /* no hostname given, we are done */
1533 *port = lport;
1534 return 0;
1535 }
1536
1537 /*
Jens Axboeb96b90c2014-03-25 10:37:56 -07001538 * If no port seen yet, check if there's a last ',' at the end
Jens Axboe76867622011-10-25 09:52:51 +02001539 */
1540 if (!lport) {
1541 portp = strchr(host, ',');
1542 if (portp) {
1543 *portp = '\0';
1544 portp++;
1545 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001546 if (!lport || lport > 65535) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +02001547 log_err("fio: bad server port %u\n", lport);
Jens Axboebebe6392011-10-07 10:00:51 +02001548 return 1;
1549 }
Jens Axboe76867622011-10-25 09:52:51 +02001550 }
1551 }
1552
1553 if (lport)
1554 *port = lport;
1555
1556 if (!strlen(host))
1557 return 0;
1558
1559 *ptr = strdup(host);
1560
Jens Axboe3aa3cee2014-01-24 18:56:56 -08001561 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001562 free(*ptr);
1563 *ptr = NULL;
1564 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001565 }
1566
1567 if (*port == 0)
1568 *port = fio_net_port;
1569
1570 return 0;
1571}
1572
Jens Axboe87aa8f12011-10-06 21:24:13 +02001573/*
1574 * Server arg should be one of:
1575 *
1576 * sock:/path/to/socket
1577 * ip:1.2.3.4
1578 * 1.2.3.4
1579 *
1580 * Where sock uses unix domain sockets, and ip binds the server to
1581 * a specific interface. If no arguments are given to the server, it
1582 * uses IP and binds to 0.0.0.0.
1583 *
1584 */
1585static int fio_handle_server_arg(void)
1586{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001587 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001588 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001589
Jens Axboe87aa8f12011-10-06 21:24:13 +02001590 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1591
1592 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001593 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001594
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001595 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001596 &port, &saddr_in.sin_addr,
1597 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001598
1599 if (!is_sock && bind_sock) {
1600 free(bind_sock);
1601 bind_sock = NULL;
1602 }
1603
Jens Axboea7de0a12011-10-07 12:55:14 +02001604out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001605 fio_net_port = port;
1606 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001607 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001608 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001609}
1610
Jens Axboe43cdea12014-03-25 15:06:17 -07001611static void sig_int(int sig)
1612{
1613 if (bind_sock)
1614 unlink(bind_sock);
1615}
1616
1617static void set_sig_handlers(void)
1618{
1619 struct sigaction act;
1620
1621 memset(&act, 0, sizeof(act));
1622 act.sa_handler = sig_int;
1623 act.sa_flags = SA_RESTART;
1624 sigaction(SIGINT, &act, NULL);
1625}
1626
Jens Axboe87aa8f12011-10-06 21:24:13 +02001627static int fio_server(void)
1628{
1629 int sk, ret;
1630
1631 dprint(FD_NET, "starting server\n");
1632
1633 if (fio_handle_server_arg())
1634 return -1;
1635
1636 sk = fio_init_server_connection();
1637 if (sk < 0)
1638 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001639
Jens Axboe43cdea12014-03-25 15:06:17 -07001640 set_sig_handlers();
1641
Jens Axboe81179ee2011-10-04 12:42:06 +02001642 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001643
Jens Axboe81179ee2011-10-04 12:42:06 +02001644 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001645
1646 if (fio_server_arg) {
1647 free(fio_server_arg);
1648 fio_server_arg = NULL;
1649 }
Jens Axboebebe6392011-10-07 10:00:51 +02001650 if (bind_sock)
1651 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001652
Jens Axboe81179ee2011-10-04 12:42:06 +02001653 return ret;
1654}
1655
Jens Axboe7b821682011-10-11 12:16:32 +02001656void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001657{
Jens Axboe7b821682011-10-11 12:16:32 +02001658 if (signal == SIGPIPE)
1659 server_fd = -1;
1660 else {
1661 log_info("\nfio: terminating on signal %d\n", signal);
1662 exit_backend = 1;
1663 }
Jens Axboe9abea482011-10-04 13:21:54 +02001664}
1665
Jens Axboe13755d92011-10-10 19:51:26 +02001666static int check_existing_pidfile(const char *pidfile)
1667{
1668 struct stat sb;
1669 char buf[16];
1670 pid_t pid;
1671 FILE *f;
1672
1673 if (stat(pidfile, &sb))
1674 return 0;
1675
1676 f = fopen(pidfile, "r");
1677 if (!f)
1678 return 0;
1679
Jens Axboebfc3b172011-10-10 21:16:55 +02001680 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001681 fclose(f);
1682 return 1;
1683 }
1684 fclose(f);
1685
1686 pid = atoi(buf);
1687 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001688 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001689
1690 return 1;
1691}
1692
1693static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001694{
1695 FILE *fpid;
1696
1697 fpid = fopen(pidfile, "w");
1698 if (!fpid) {
1699 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001700 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001701 }
1702
1703 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001704 fclose(fpid);
1705 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001706}
1707
1708/*
1709 * If pidfile is specified, background us.
1710 */
1711int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001712{
1713 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001714 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001715
Bruce Cran93bcfd22012-02-20 20:18:19 +01001716#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001717 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +02001718 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001719#endif
1720
Jens Axboe402668f2011-10-10 15:28:58 +02001721 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001722 return fio_server();
1723
Jens Axboe13755d92011-10-10 19:51:26 +02001724 if (check_existing_pidfile(pidfile)) {
1725 log_err("fio: pidfile %s exists and server appears alive\n",
1726 pidfile);
Jens Axboeb8ba87a2014-04-11 11:37:34 -06001727 free(pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001728 return -1;
1729 }
1730
Jens Axboee46d8092011-10-03 09:11:02 +02001731 pid = fork();
1732 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001733 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001734 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001735 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001736 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001737 int ret = write_pid(pid, pidfile);
1738
Jens Axboeb8ba87a2014-04-11 11:37:34 -06001739 free(pidfile);
Jens Axboe5ea73552014-09-11 17:05:58 -06001740 _exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001741 }
Jens Axboee46d8092011-10-03 09:11:02 +02001742
1743 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001744 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1745 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001746 close(STDIN_FILENO);
1747 close(STDOUT_FILENO);
1748 close(STDERR_FILENO);
1749 f_out = NULL;
1750 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001751
1752 ret = fio_server();
1753
1754 closelog();
1755 unlink(pidfile);
1756 free(pidfile);
1757 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001758}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001759
Jens Axboebebe6392011-10-07 10:00:51 +02001760void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001761{
1762 fio_server_arg = strdup(arg);
1763}