blob: c4ab906f74f38e579ee7eb3d838f95abbdef0fc5 [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 Axboede54cfd2014-11-10 20:34:00 -070027#include "verify.h"
28#include "smalloc.h"
Jens Axboe50d16972011-09-29 17:45:28 -060029
Stephen M. Cameron5adc2442012-02-24 08:17:31 +010030int fio_net_port = FIO_NET_PORT;
Jens Axboe50d16972011-09-29 17:45:28 -060031
Jens Axboe009b1be2011-09-29 18:27:02 -060032int exit_backend = 0;
33
Jens Axboe46c48f12011-10-01 12:50:51 -060034static int server_fd = -1;
Jens Axboe87aa8f12011-10-06 21:24:13 +020035static char *fio_server_arg;
36static char *bind_sock;
37static struct sockaddr_in saddr_in;
Jens Axboe811826b2011-10-24 09:11:50 +020038static struct sockaddr_in6 saddr_in6;
Jens Axboe811826b2011-10-24 09:11:50 +020039static int use_ipv6;
Jens Axboe3989b142013-04-12 13:13:24 +020040#ifdef CONFIG_ZLIB
41static unsigned int has_zlib = 1;
42#else
43static unsigned int has_zlib = 0;
44#endif
45static unsigned int use_zlib;
Jens Axboede54cfd2014-11-10 20:34:00 -070046static char me[128];
Jens Axboe37db14f2011-09-30 17:00:42 -060047
Jens Axboe122c7722012-03-19 09:13:15 +010048struct fio_fork_item {
49 struct flist_head list;
50 int exitval;
51 int signal;
52 int exited;
53 pid_t pid;
54};
55
Jens Axboede54cfd2014-11-10 20:34:00 -070056struct cmd_reply {
57 struct fio_mutex lock;
58 void *data;
59 size_t size;
60 int error;
61};
62
Jens Axboe89c17072011-10-11 10:15:51 +020063static const char *fio_server_ops[FIO_NET_CMD_NR] = {
64 "",
65 "QUIT",
66 "EXIT",
67 "JOB",
68 "JOBLINE",
69 "TEXT",
70 "TS",
71 "GS",
72 "SEND_ETA",
73 "ETA",
74 "PROBE",
75 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020076 "STOP",
77 "DISK_UTIL",
Jens Axboeb9d2f302012-03-08 20:36:28 +010078 "SERVER_START",
Jens Axboe807f9972012-03-02 10:25:24 +010079 "ADD_JOB",
Jens Axboede54cfd2014-11-10 20:34:00 -070080 "RUN",
81 "IOLOG",
82 "UPDATE_JOB",
83 "LOAD_FILE",
84 "VTRIGGER",
85 "SENDFILE",
Jens Axboe89c17072011-10-11 10:15:51 +020086};
87
88const char *fio_server_op(unsigned int op)
89{
90 static char buf[32];
91
92 if (op < FIO_NET_CMD_NR)
93 return fio_server_ops[op];
94
95 sprintf(buf, "UNKNOWN/%d", op);
96 return buf;
97}
98
Jens Axboe5235f622012-03-14 21:25:51 +010099static ssize_t iov_total_len(const struct iovec *iov, int count)
Jens Axboe132159a2011-09-30 15:01:32 -0600100{
Jens Axboe5235f622012-03-14 21:25:51 +0100101 ssize_t ret = 0;
102
103 while (count--) {
104 ret += iov->iov_len;
105 iov++;
106 }
107
108 return ret;
109}
110
111static int fio_sendv_data(int sk, struct iovec *iov, int count)
112{
113 ssize_t total_len = iov_total_len(iov, count);
114 ssize_t ret;
Jens Axboe794d69c2011-10-01 08:48:50 -0600115
Jens Axboe132159a2011-09-30 15:01:32 -0600116 do {
Jens Axboe5235f622012-03-14 21:25:51 +0100117 ret = writev(sk, iov, count);
Jens Axboe132159a2011-09-30 15:01:32 -0600118 if (ret > 0) {
Jens Axboe5235f622012-03-14 21:25:51 +0100119 total_len -= ret;
120 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600121 break;
Jens Axboe5235f622012-03-14 21:25:51 +0100122
123 while (ret) {
124 if (ret >= iov->iov_len) {
125 ret -= iov->iov_len;
126 iov++;
127 continue;
128 }
129 iov->iov_base += ret;
130 iov->iov_len -= ret;
131 ret = 0;
132 }
Jens Axboe132159a2011-09-30 15:01:32 -0600133 } else if (!ret)
134 break;
135 else if (errno == EAGAIN || errno == EINTR)
136 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200137 else
138 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600139 } while (!exit_backend);
140
Jens Axboe5235f622012-03-14 21:25:51 +0100141 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600142 return 0;
143
Jens Axboe8b4e61b2012-03-09 17:10:54 +0100144 if (errno)
145 return -errno;
146
Jens Axboe132159a2011-09-30 15:01:32 -0600147 return 1;
148}
149
Jens Axboe5235f622012-03-14 21:25:51 +0100150int fio_send_data(int sk, const void *p, unsigned int len)
151{
152 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
153
154 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
155
156 return fio_sendv_data(sk, &iov, 1);
157}
158
Jens Axboe132159a2011-09-30 15:01:32 -0600159int fio_recv_data(int sk, void *p, unsigned int len)
160{
161 do {
162 int ret = recv(sk, p, len, MSG_WAITALL);
163
164 if (ret > 0) {
165 len -= ret;
166 if (!len)
167 break;
168 p += ret;
169 continue;
170 } else if (!ret)
171 break;
172 else if (errno == EAGAIN || errno == EINTR)
173 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200174 else
175 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600176 } while (!exit_backend);
177
178 if (!len)
179 return 0;
180
181 return -1;
182}
183
184static int verify_convert_cmd(struct fio_net_cmd *cmd)
185{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600186 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600187
Jens Axboefcee5ff2011-09-30 22:50:39 -0600188 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
189 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600190
Jens Axboe25dfa842012-02-29 10:01:34 +0100191 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600192 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600193 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600194 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600195 return 1;
196 }
197
198 cmd->version = le16_to_cpu(cmd->version);
199 cmd->opcode = le16_to_cpu(cmd->opcode);
200 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200201 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600202 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
203
204 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200205 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600206 break;
207 default:
208 log_err("fio: bad server cmd version %d\n", cmd->version);
209 return 1;
210 }
211
Jens Axboeb9d2f302012-03-08 20:36:28 +0100212 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600213 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
214 return 1;
215 }
216
217 return 0;
218}
219
Jens Axboea64e88d2011-10-03 14:20:01 +0200220/*
221 * Read (and defragment, if necessary) incoming commands
222 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200223struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600224{
Jens Axboedfbf1f62014-04-14 11:43:47 -0600225 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
Jens Axboea64e88d2011-10-03 14:20:01 +0200226 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600227 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200228 int ret, first = 1;
229 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600230
Jens Axboea64e88d2011-10-03 14:20:01 +0200231 do {
232 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
233 if (ret)
234 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600235
Jens Axboea64e88d2011-10-03 14:20:01 +0200236 /* We have a command, verify it and swap if need be */
237 ret = verify_convert_cmd(&cmd);
238 if (ret)
239 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600240
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200241 if (first) {
242 /* if this is text, add room for \0 at the end */
243 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
244 assert(!cmdret);
245 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200246 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600247
Jens Axboedfbf1f62014-04-14 11:43:47 -0600248 if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
249 log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
250 ret = 1;
251 break;
252 }
253
254 tmp = realloc(cmdret, cmd_size);
255 if (!tmp) {
256 log_err("fio: server failed allocating cmd\n");
257 ret = 1;
258 break;
259 }
260 cmdret = tmp;
Jens Axboe132159a2011-09-30 15:01:32 -0600261
Jens Axboea64e88d2011-10-03 14:20:01 +0200262 if (first)
263 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200264 else if (cmdret->opcode != cmd.opcode) {
265 log_err("fio: fragment opcode mismatch (%d != %d)\n",
266 cmdret->opcode, cmd.opcode);
267 ret = 1;
268 break;
269 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200270
271 if (!cmd.pdu_len)
272 break;
273
274 /* There's payload, get it */
275 pdu = (void *) cmdret->payload + pdu_offset;
276 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
277 if (ret)
278 break;
279
280 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100281 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200282 if (crc != cmd.pdu_crc16) {
283 log_err("fio: server bad crc on payload ");
284 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
285 ret = 1;
286 break;
287 }
288
289 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200290 if (!first)
291 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200292 first = 0;
293 } while (cmd.flags & FIO_NET_CMD_F_MORE);
294
295 if (ret) {
296 free(cmdret);
297 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200298 } else if (cmdret) {
299 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100300 if (cmdret->pdu_len) {
301 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700302 struct cmd_text_pdu *__pdu = (struct cmd_text_pdu *) cmdret->payload;
303 char *buf = (char *) __pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200304
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700305 buf[__pdu->buf_len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100306 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700307 struct cmd_job_pdu *__pdu = (struct cmd_job_pdu *) cmdret->payload;
308 char *buf = (char *) __pdu->buf;
309 int len = le32_to_cpu(__pdu->buf_len);
Jens Axboe084d1c62012-03-03 20:28:07 +0100310
Jens Axboe46bcd492012-03-14 11:31:21 +0100311 buf[len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100312 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200313 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100314
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200315 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200316 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200317 }
Jens Axboe132159a2011-09-30 15:01:32 -0600318
Jens Axboea64e88d2011-10-03 14:20:01 +0200319 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600320}
321
Jens Axboe40c60512012-03-27 16:03:04 +0200322static void add_reply(uint64_t tag, struct flist_head *list)
323{
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200324 struct fio_net_cmd_reply *reply;
Jens Axboe40c60512012-03-27 16:03:04 +0200325
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200326 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
Jens Axboe40c60512012-03-27 16:03:04 +0200327 flist_add_tail(&reply->list, list);
328}
329
330static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
331{
332 struct fio_net_cmd_reply *reply;
333
334 reply = calloc(1, sizeof(*reply));
335 INIT_FLIST_HEAD(&reply->list);
336 gettimeofday(&reply->tv, NULL);
337 reply->saved_tag = tag;
338 reply->opcode = opcode;
339
340 return (uintptr_t) reply;
341}
342
343static void free_reply(uint64_t tag)
344{
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200345 struct fio_net_cmd_reply *reply;
Jens Axboe40c60512012-03-27 16:03:04 +0200346
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200347 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
Jens Axboe40c60512012-03-27 16:03:04 +0200348 free(reply);
349}
350
Jens Axboe53bd8db2012-03-14 21:51:38 +0100351void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
Jens Axboe132159a2011-09-30 15:01:32 -0600352{
353 uint32_t pdu_len;
354
Jens Axboe25dfa842012-02-29 10:01:34 +0100355 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600356
357 pdu_len = le32_to_cpu(cmd->pdu_len);
Jens Axboe1b427252012-03-14 15:03:03 +0100358 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
359}
360
361void fio_net_cmd_crc(struct fio_net_cmd *cmd)
362{
363 fio_net_cmd_crc_pdu(cmd, cmd->payload);
Jens Axboe132159a2011-09-30 15:01:32 -0600364}
365
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200366int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
Jens Axboe40c60512012-03-27 16:03:04 +0200367 uint64_t *tagptr, struct flist_head *list)
Jens Axboe794d69c2011-10-01 08:48:50 -0600368{
Jens Axboe7f868312011-10-10 09:55:21 +0200369 struct fio_net_cmd *cmd = NULL;
370 size_t this_len, cur_len = 0;
Jens Axboe40c60512012-03-27 16:03:04 +0200371 uint64_t tag;
Jens Axboe794d69c2011-10-01 08:48:50 -0600372 int ret;
373
Jens Axboe40c60512012-03-27 16:03:04 +0200374 if (list) {
375 assert(tagptr);
376 tag = *tagptr = alloc_reply(*tagptr, opcode);
377 } else
378 tag = tagptr ? *tagptr : 0;
379
Jens Axboe794d69c2011-10-01 08:48:50 -0600380 do {
381 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100382 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
383 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600384
Jens Axboe7f868312011-10-10 09:55:21 +0200385 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
386 if (cmd)
387 free(cmd);
388
389 cur_len = sizeof(*cmd) + this_len;
390 cmd = malloc(cur_len);
391 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600392
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200393 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600394
395 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200396 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600397
398 fio_net_cmd_crc(cmd);
399
400 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600401 size -= this_len;
402 buf += this_len;
403 } while (!ret && size);
404
Jens Axboe40c60512012-03-27 16:03:04 +0200405 if (list) {
406 if (ret)
407 free_reply(tag);
408 else
409 add_reply(tag, list);
410 }
411
Jens Axboe7f868312011-10-10 09:55:21 +0200412 if (cmd)
413 free(cmd);
414
Jens Axboe794d69c2011-10-01 08:48:50 -0600415 return ret;
416}
417
Jens Axboe89c17072011-10-11 10:15:51 +0200418static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600419{
Jens Axboe178cde92011-10-05 22:14:31 +0200420 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600421
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200422 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600423 fio_net_cmd_crc(&cmd);
424
425 return fio_send_data(sk, &cmd, sizeof(cmd));
426}
427
Jens Axboe89c17072011-10-11 10:15:51 +0200428/*
429 * If 'list' is non-NULL, then allocate and store the sent command for
430 * later verification.
431 */
432int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
433 struct flist_head *list)
434{
Jens Axboe89c17072011-10-11 10:15:51 +0200435 int ret;
436
Jens Axboe40c60512012-03-27 16:03:04 +0200437 if (list)
438 tag = alloc_reply(tag, opcode);
Jens Axboe89c17072011-10-11 10:15:51 +0200439
Jens Axboe40c60512012-03-27 16:03:04 +0200440 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
Jens Axboe89c17072011-10-11 10:15:51 +0200441 if (ret) {
Jens Axboe40c60512012-03-27 16:03:04 +0200442 if (list)
443 free_reply(tag);
444
Jens Axboe89c17072011-10-11 10:15:51 +0200445 return ret;
446 }
447
Jens Axboe40c60512012-03-27 16:03:04 +0200448 if (list)
449 add_reply(tag, list);
450
Jens Axboe89c17072011-10-11 10:15:51 +0200451 return 0;
452}
453
Jens Axboe122c7722012-03-19 09:13:15 +0100454int fio_net_send_quit(int sk)
Jens Axboe437377e2011-10-01 08:31:30 -0600455{
Jens Axboe46c48f12011-10-01 12:50:51 -0600456 dprint(FD_NET, "server: sending quit\n");
Jens Axboe122c7722012-03-19 09:13:15 +0100457
Jens Axboe8c953072012-03-20 14:35:35 +0100458 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600459}
460
Jens Axboef58bd2a2012-03-27 10:06:42 +0200461static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error,
462 int signal)
Jens Axboe122c7722012-03-19 09:13:15 +0100463{
464 struct cmd_end_pdu epdu;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200465 uint64_t tag = 0;
Jens Axboe122c7722012-03-19 09:13:15 +0100466
Jens Axboef58bd2a2012-03-27 10:06:42 +0200467 if (cmd)
468 tag = cmd->tag;
Jens Axboe122c7722012-03-19 09:13:15 +0100469
470 epdu.error = __cpu_to_le32(error);
471 epdu.signal = __cpu_to_le32(signal);
Jens Axboe40c60512012-03-27 16:03:04 +0200472 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200473}
474
475int fio_net_send_stop(int sk, int error, int signal)
476{
477 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
478 return fio_net_send_ack(sk, NULL, error, signal);
Jens Axboe122c7722012-03-19 09:13:15 +0100479}
480
481static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
482{
483 struct fio_fork_item *ffi;
484
485 ffi = malloc(sizeof(*ffi));
486 ffi->exitval = 0;
487 ffi->signal = 0;
488 ffi->exited = 0;
489 ffi->pid = pid;
490 flist_add_tail(&ffi->list, list);
491}
492
Jens Axboe6348b5d2013-11-07 13:37:09 -0700493static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
Jens Axboe122c7722012-03-19 09:13:15 +0100494{
Jens Axboea7533db2013-11-06 11:19:42 -0700495 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
Jens Axboe6348b5d2013-11-07 13:37:09 -0700496 fio_server_add_fork_item(pid, conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100497}
498
Jens Axboe6348b5d2013-11-07 13:37:09 -0700499static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
Jens Axboe122c7722012-03-19 09:13:15 +0100500{
Jens Axboea7533db2013-11-06 11:19:42 -0700501 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
Jens Axboe6348b5d2013-11-07 13:37:09 -0700502 fio_server_add_fork_item(pid, job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100503}
504
505static void fio_server_check_fork_item(struct fio_fork_item *ffi)
506{
507 int ret, status;
508
509 ret = waitpid(ffi->pid, &status, WNOHANG);
510 if (ret < 0) {
511 if (errno == ECHILD) {
Jens Axboea7533db2013-11-06 11:19:42 -0700512 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
Jens Axboe122c7722012-03-19 09:13:15 +0100513 ffi->exited = 1;
514 } else
515 log_err("fio: waitpid: %s\n", strerror(errno));
516 } else if (ret == ffi->pid) {
517 if (WIFSIGNALED(status)) {
518 ffi->signal = WTERMSIG(status);
519 ffi->exited = 1;
520 }
521 if (WIFEXITED(status)) {
522 if (WEXITSTATUS(status))
523 ffi->exitval = WEXITSTATUS(status);
524 ffi->exited = 1;
525 }
526 }
527}
528
529static void fio_server_fork_item_done(struct fio_fork_item *ffi)
530{
Jens Axboea7533db2013-11-06 11:19:42 -0700531 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 +0100532
533 /*
534 * Fold STOP and QUIT...
535 */
536 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
537 fio_net_send_quit(server_fd);
538 flist_del(&ffi->list);
539 free(ffi);
540}
541
Jens Axboe54163252012-03-23 12:25:18 +0100542static void fio_server_check_fork_items(struct flist_head *list)
Jens Axboe122c7722012-03-19 09:13:15 +0100543{
544 struct flist_head *entry, *tmp;
545 struct fio_fork_item *ffi;
546
547 flist_for_each_safe(entry, tmp, list) {
548 ffi = flist_entry(entry, struct fio_fork_item, list);
549
550 fio_server_check_fork_item(ffi);
551
552 if (ffi->exited)
553 fio_server_fork_item_done(ffi);
554 }
555}
556
Jens Axboe6348b5d2013-11-07 13:37:09 -0700557static void fio_server_check_jobs(struct flist_head *job_list)
Jens Axboe122c7722012-03-19 09:13:15 +0100558{
Jens Axboe6348b5d2013-11-07 13:37:09 -0700559 fio_server_check_fork_items(job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100560}
561
Jens Axboe6348b5d2013-11-07 13:37:09 -0700562static void fio_server_check_conns(struct flist_head *conn_list)
Jens Axboe122c7722012-03-19 09:13:15 +0100563{
Jens Axboe6348b5d2013-11-07 13:37:09 -0700564 fio_server_check_fork_items(conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100565}
566
Jens Axboe35899182014-10-07 20:56:28 -0600567static int handle_load_file_cmd(struct fio_net_cmd *cmd)
568{
569 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
570 void *file_name = pdu->file;
571 struct cmd_start_pdu spdu;
572
573 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
574
575 pdu->name_len = le16_to_cpu(pdu->name_len);
576 pdu->client_type = le16_to_cpu(pdu->client_type);
577
578 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
579 fio_net_send_quit(server_fd);
580 return -1;
581 }
582
583 spdu.jobs = cpu_to_le32(thread_number);
584 spdu.stat_outputs = cpu_to_le32(stat_number);
585 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
586 return 0;
587}
588
Jens Axboe6348b5d2013-11-07 13:37:09 -0700589static int handle_run_cmd(struct flist_head *job_list, struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600590{
Jens Axboe122c7722012-03-19 09:13:15 +0100591 pid_t pid;
Jens Axboea64e88d2011-10-03 14:20:01 +0200592 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600593
Jens Axboe386626f2014-06-09 13:25:13 -0600594 fio_time_init();
Jens Axboe122c7722012-03-19 09:13:15 +0100595 set_genesis_time();
596
597 pid = fork();
598 if (pid) {
Jens Axboe6348b5d2013-11-07 13:37:09 -0700599 fio_server_add_job_pid(job_list, pid);
Jens Axboe122c7722012-03-19 09:13:15 +0100600 return 0;
601 }
602
Jens Axboe2e1df072012-02-09 11:15:02 +0100603 ret = fio_backend();
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200604 free_threads_shm();
Jens Axboe122c7722012-03-19 09:13:15 +0100605 _exit(ret);
Jens Axboe81179ee2011-10-04 12:42:06 +0200606}
607
Jens Axboeb9d2f302012-03-08 20:36:28 +0100608static int handle_job_cmd(struct fio_net_cmd *cmd)
609{
Jens Axboe46bcd492012-03-14 11:31:21 +0100610 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
611 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100612 struct cmd_start_pdu spdu;
613
Jens Axboe46bcd492012-03-14 11:31:21 +0100614 pdu->buf_len = le32_to_cpu(pdu->buf_len);
615 pdu->client_type = le32_to_cpu(pdu->client_type);
616
617 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100618 fio_net_send_quit(server_fd);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100619 return -1;
620 }
621
622 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe108fea72012-11-14 13:09:45 -0700623 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200624 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100625 return 0;
626}
627
Jens Axboe81179ee2011-10-04 12:42:06 +0200628static int handle_jobline_cmd(struct fio_net_cmd *cmd)
629{
Jens Axboefa2ea802011-10-08 21:07:29 +0200630 void *pdu = cmd->payload;
631 struct cmd_single_line_pdu *cslp;
632 struct cmd_line_pdu *clp;
633 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100634 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200635 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100636 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200637
Jens Axboefa2ea802011-10-08 21:07:29 +0200638 clp = pdu;
639 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100640 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200641 argv = malloc(clp->lines * sizeof(char *));
642 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200643
Jens Axboefa2ea802011-10-08 21:07:29 +0200644 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200645
Jens Axboefa2ea802011-10-08 21:07:29 +0200646 for (i = 0; i < clp->lines; i++) {
647 cslp = pdu + offset;
648 argv[i] = (char *) cslp->text;
649
650 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200651 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
652 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200653
Jens Axboe46bcd492012-03-14 11:31:21 +0100654 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100655 fio_net_send_quit(server_fd);
Jens Axboefa2ea802011-10-08 21:07:29 +0200656 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200657 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200658 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200659
Jens Axboefa2ea802011-10-08 21:07:29 +0200660 free(argv);
661
Jens Axboeb9d2f302012-03-08 20:36:28 +0100662 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe1e5324e2012-11-14 14:25:31 -0700663 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200664 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100665 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600666}
667
Jens Axboec28e8e82011-10-04 08:57:39 +0200668static int handle_probe_cmd(struct fio_net_cmd *cmd)
669{
Jens Axboe3989b142013-04-12 13:13:24 +0200670 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
671 struct cmd_probe_reply_pdu probe;
Jens Axboe40c60512012-03-27 16:03:04 +0200672 uint64_t tag = cmd->tag;
Jens Axboec28e8e82011-10-04 08:57:39 +0200673
Jens Axboe89c17072011-10-11 10:15:51 +0200674 dprint(FD_NET, "server: sending probe reply\n");
675
Jens Axboede54cfd2014-11-10 20:34:00 -0700676 strcpy(me, (char *) pdu->server);
677
Jens Axboec28e8e82011-10-04 08:57:39 +0200678 memset(&probe, 0, sizeof(probe));
679 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe0dcebdf2013-01-23 15:42:16 -0700680#ifdef CONFIG_BIG_ENDIAN
Jens Axboe6eb24792011-10-04 15:00:30 +0200681 probe.bigendian = 1;
682#endif
Jens Axboe750db472012-04-16 11:44:48 +0200683 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
Jens Axboec28e8e82011-10-04 08:57:39 +0200684
Jens Axboecca84642011-10-07 12:47:57 +0200685 probe.os = FIO_OS;
686 probe.arch = FIO_ARCH;
Jens Axboe38fdef22011-10-11 14:30:06 +0200687 probe.bpp = sizeof(void *);
Jens Axboed31e26d2012-03-28 21:38:24 +0200688 probe.cpus = __cpu_to_le32(cpus_online());
Jens Axboe3989b142013-04-12 13:13:24 +0200689
690 /*
691 * If the client supports compression and we do too, then enable it
692 */
693 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
694 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
695 use_zlib = 1;
696 } else {
697 probe.flags = 0;
698 use_zlib = 0;
699 }
Jens Axboe38fdef22011-10-11 14:30:06 +0200700
Jens Axboe40c60512012-03-27 16:03:04 +0200701 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200702}
703
704static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
705{
706 struct jobs_eta *je;
Jens Axboe40c60512012-03-27 16:03:04 +0200707 uint64_t tag = cmd->tag;
Jens Axboe61f6cce2014-06-24 08:40:21 -0600708 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200709 int i;
710
Jens Axboe61f6cce2014-06-24 08:40:21 -0600711 je = get_jobs_eta(1, &size);
712 if (!je)
Jens Axboeb814fb22011-10-12 09:20:34 +0200713 return 0;
714
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200715 dprint(FD_NET, "server sending status\n");
716
717 je->nr_running = cpu_to_le32(je->nr_running);
718 je->nr_ramp = cpu_to_le32(je->nr_ramp);
719 je->nr_pending = cpu_to_le32(je->nr_pending);
Jens Axboe714e85f2013-04-15 10:21:56 +0200720 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200721 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200722
Jens Axboe298921d2012-09-24 18:47:01 +0200723 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100724 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
725 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
726 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
727 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboe16725bb2013-04-11 12:43:05 +0200728 je->rate[i] = cpu_to_le32(je->rate[i]);
729 je->iops[i] = cpu_to_le32(je->iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200730 }
731
Jens Axboec1970b92012-03-06 15:37:40 +0100732 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200733 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100734 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200735 je->is_pow2 = cpu_to_le32(je->is_pow2);
Jens Axboeed2c0a12013-04-11 12:55:16 +0200736 je->unit_base = cpu_to_le32(je->unit_base);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200737
Jens Axboe40c60512012-03-27 16:03:04 +0200738 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200739 free(je);
740 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200741}
742
Jens Axboe40c60512012-03-27 16:03:04 +0200743static int send_update_job_reply(int fd, uint64_t __tag, int error)
744{
745 uint64_t tag = __tag;
746 uint32_t pdu_error;
747
748 pdu_error = __cpu_to_le32(error);
749 return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL);
750}
751
Jens Axboef58bd2a2012-03-27 10:06:42 +0200752static int handle_update_job_cmd(struct fio_net_cmd *cmd)
753{
754 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
755 struct thread_data *td;
756 uint32_t tnumber;
757
758 tnumber = le32_to_cpu(pdu->thread_number);
759
760 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
761
Jens Axboe30ffacb2012-03-28 09:15:05 +0200762 if (!tnumber || tnumber > thread_number) {
Jens Axboe40c60512012-03-27 16:03:04 +0200763 send_update_job_reply(server_fd, cmd->tag, ENODEV);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200764 return 0;
765 }
766
Jens Axboe30ffacb2012-03-28 09:15:05 +0200767 td = &threads[tnumber - 1];
Jens Axboef58bd2a2012-03-27 10:06:42 +0200768 convert_thread_options_to_cpu(&td->o, &pdu->top);
Jens Axboe40c60512012-03-27 16:03:04 +0200769 send_update_job_reply(server_fd, cmd->tag, 0);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200770 return 0;
771}
772
Jens Axboede54cfd2014-11-10 20:34:00 -0700773static int handle_trigger_cmd(struct fio_net_cmd *cmd)
774{
775 struct cmd_vtrigger_pdu *pdu = (struct cmd_vtrigger_pdu *) cmd->payload;
776 char *buf = (char *) pdu->cmd;
777 struct all_io_list *rep;
778 size_t sz;
779
780 pdu->len = le16_to_cpu(pdu->len);
781 buf[pdu->len] = '\0';
782
783 rep = get_all_io_list(IO_LIST_ALL, &sz);
784 if (!rep) {
785 struct all_io_list state;
786
787 state.threads = cpu_to_le64((uint64_t) 0);
788 fio_net_send_cmd(server_fd, FIO_NET_CMD_VTRIGGER, &state, sizeof(state), NULL, NULL);
789 } else {
790 fio_net_send_cmd(server_fd, FIO_NET_CMD_VTRIGGER, rep, sz, NULL, NULL);
791 free(rep);
792 }
793
794 exec_trigger(buf);
795 return 0;
796}
797
Jens Axboe6348b5d2013-11-07 13:37:09 -0700798static int handle_command(struct flist_head *job_list, struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600799{
800 int ret;
801
Jens Axboe4b91ee82013-02-25 10:18:33 +0100802 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
803 fio_server_op(cmd->opcode), cmd->pdu_len,
804 (unsigned long long) cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600805
Jens Axboe132159a2011-09-30 15:01:32 -0600806 switch (cmd->opcode) {
807 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200808 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200809 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200810 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600811 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200812 return -1;
Jens Axboe35899182014-10-07 20:56:28 -0600813 case FIO_NET_CMD_LOAD_FILE:
814 ret = handle_load_file_cmd(cmd);
815 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600816 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200817 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600818 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200819 case FIO_NET_CMD_JOBLINE:
820 ret = handle_jobline_cmd(cmd);
821 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200822 case FIO_NET_CMD_PROBE:
823 ret = handle_probe_cmd(cmd);
824 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200825 case FIO_NET_CMD_SEND_ETA:
826 ret = handle_send_eta_cmd(cmd);
827 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100828 case FIO_NET_CMD_RUN:
Jens Axboe6348b5d2013-11-07 13:37:09 -0700829 ret = handle_run_cmd(job_list, cmd);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100830 break;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200831 case FIO_NET_CMD_UPDATE_JOB:
832 ret = handle_update_job_cmd(cmd);
833 break;
Jens Axboede54cfd2014-11-10 20:34:00 -0700834 case FIO_NET_CMD_VTRIGGER:
835 ret = handle_trigger_cmd(cmd);
836 break;
837 case FIO_NET_CMD_SENDFILE: {
838 struct cmd_sendfile_reply *in;
839 struct cmd_reply *rep;
840
841 rep = (struct cmd_reply *) (uintptr_t) cmd->tag;
842
843 in = (struct cmd_sendfile_reply *) cmd->payload;
844 in->size = le32_to_cpu(in->size);
845 in->error = le32_to_cpu(in->error);
846 if (in->error) {
847 ret = 1;
848 rep->error = in->error;
849 } else {
850 ret = 0;
851 rep->data = smalloc(in->size);
852 if (!rep->data) {
853 ret = 1;
854 rep->error = ENOMEM;
855 } else {
856 rep->size = in->size;
857 memcpy(rep->data, in->data, in->size);
858 }
859 }
860 fio_mutex_up(&rep->lock);
861 break;
862 }
Jens Axboe132159a2011-09-30 15:01:32 -0600863 default:
Jens Axboe3c3ed072012-03-27 09:12:39 +0200864 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600865 ret = 1;
866 }
867
868 return ret;
869}
870
Jens Axboe122c7722012-03-19 09:13:15 +0100871static int handle_connection(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600872{
873 struct fio_net_cmd *cmd = NULL;
Jens Axboe6348b5d2013-11-07 13:37:09 -0700874 FLIST_HEAD(job_list);
Jens Axboe132159a2011-09-30 15:01:32 -0600875 int ret = 0;
876
Jens Axboe122c7722012-03-19 09:13:15 +0100877 reset_fio_state();
Jens Axboe122c7722012-03-19 09:13:15 +0100878 server_fd = sk;
879
Jens Axboe132159a2011-09-30 15:01:32 -0600880 /* read forever */
881 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200882 struct pollfd pfd = {
883 .fd = sk,
884 .events = POLLIN,
885 };
886
887 ret = 0;
888 do {
Jens Axboe54163252012-03-23 12:25:18 +0100889 int timeout = 1000;
890
891 if (!flist_empty(&job_list))
892 timeout = 100;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200893
Jens Axboe54163252012-03-23 12:25:18 +0100894 ret = poll(&pfd, 1, timeout);
Jens Axboee951bdc2011-10-05 21:58:45 +0200895 if (ret < 0) {
896 if (errno == EINTR)
897 break;
898 log_err("fio: poll: %s\n", strerror(errno));
899 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200900 } else if (!ret) {
Jens Axboe6348b5d2013-11-07 13:37:09 -0700901 fio_server_check_jobs(&job_list);
Jens Axboee951bdc2011-10-05 21:58:45 +0200902 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200903 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200904
905 if (pfd.revents & POLLIN)
906 break;
907 if (pfd.revents & (POLLERR|POLLHUP)) {
908 ret = 1;
909 break;
910 }
Jens Axboe19c65172011-10-05 22:05:37 +0200911 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200912
Jens Axboe6348b5d2013-11-07 13:37:09 -0700913 fio_server_check_jobs(&job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100914
Jens Axboee951bdc2011-10-05 21:58:45 +0200915 if (ret < 0)
916 break;
917
918 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600919 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200920 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600921 break;
922 }
923
Jens Axboe6348b5d2013-11-07 13:37:09 -0700924 ret = handle_command(&job_list, cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600925 if (ret)
926 break;
927
928 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400929 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600930 }
931
932 if (cmd)
933 free(cmd);
934
Jens Axboe122c7722012-03-19 09:13:15 +0100935 close(sk);
936 _exit(ret);
Jens Axboecc0df002011-10-03 20:53:32 +0200937}
938
Jens Axboe50d16972011-09-29 17:45:28 -0600939static int accept_loop(int listen_sk)
940{
Jens Axboebb447a22011-10-04 15:06:42 +0200941 struct sockaddr_in addr;
Jens Axboe479471c2014-01-23 20:42:06 -0800942 struct sockaddr_in6 addr6;
943 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600944 struct pollfd pfd;
Jens Axboe4a851612014-04-14 10:04:21 -0600945 int ret = 0, sk, exitval = 0;
Jens Axboe6348b5d2013-11-07 13:37:09 -0700946 FLIST_HEAD(conn_list);
Jens Axboe50d16972011-09-29 17:45:28 -0600947
Jens Axboe60efd142011-10-04 13:30:11 +0200948 dprint(FD_NET, "server enter accept loop\n");
949
Jens Axboe4a851612014-04-14 10:04:21 -0600950 fio_set_fd_nonblocking(listen_sk, "server");
Jens Axboe122c7722012-03-19 09:13:15 +0100951
952 while (!exit_backend) {
Jens Axboe479471c2014-01-23 20:42:06 -0800953 const char *from;
954 char buf[64];
Jens Axboe122c7722012-03-19 09:13:15 +0100955 pid_t pid;
956
957 pfd.fd = listen_sk;
958 pfd.events = POLLIN;
959 do {
Jens Axboe54163252012-03-23 12:25:18 +0100960 int timeout = 1000;
961
962 if (!flist_empty(&conn_list))
963 timeout = 100;
964
965 ret = poll(&pfd, 1, timeout);
Jens Axboe122c7722012-03-19 09:13:15 +0100966 if (ret < 0) {
967 if (errno == EINTR)
968 break;
969 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600970 break;
Jens Axboe122c7722012-03-19 09:13:15 +0100971 } else if (!ret) {
Jens Axboe6348b5d2013-11-07 13:37:09 -0700972 fio_server_check_conns(&conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100973 continue;
974 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600975
Jens Axboe122c7722012-03-19 09:13:15 +0100976 if (pfd.revents & POLLIN)
977 break;
978 } while (!exit_backend);
979
Jens Axboe6348b5d2013-11-07 13:37:09 -0700980 fio_server_check_conns(&conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100981
982 if (exit_backend || ret < 0)
Jens Axboe009b1be2011-09-29 18:27:02 -0600983 break;
Jens Axboe009b1be2011-09-29 18:27:02 -0600984
Jens Axboe479471c2014-01-23 20:42:06 -0800985 if (use_ipv6)
986 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
987 else
988 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
989
Jens Axboe122c7722012-03-19 09:13:15 +0100990 if (sk < 0) {
991 log_err("fio: accept: %s\n", strerror(errno));
992 return -1;
993 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600994
Jens Axboe479471c2014-01-23 20:42:06 -0800995 if (use_ipv6)
996 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
997 else
998 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
999
1000 dprint(FD_NET, "server: connect from %s\n", from);
Jens Axboe122c7722012-03-19 09:13:15 +01001001
1002 pid = fork();
1003 if (pid) {
1004 close(sk);
Jens Axboe6348b5d2013-11-07 13:37:09 -07001005 fio_server_add_conn_pid(&conn_list, pid);
Jens Axboe122c7722012-03-19 09:13:15 +01001006 continue;
1007 }
1008
1009 /* exits */
1010 handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -06001011 }
1012
Jens Axboe132159a2011-09-30 15:01:32 -06001013 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -06001014}
1015
Jens Axboe084d1c62012-03-03 20:28:07 +01001016int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -06001017{
Jens Axboe084d1c62012-03-03 20:28:07 +01001018 struct cmd_text_pdu *pdu;
1019 unsigned int tlen;
1020 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -06001021
Jens Axboe084d1c62012-03-03 20:28:07 +01001022 if (server_fd == -1)
1023 return log_local_buf(buf, len);
1024
1025 tlen = sizeof(*pdu) + len;
1026 pdu = malloc(tlen);
1027
1028 pdu->level = __cpu_to_le32(level);
1029 pdu->buf_len = __cpu_to_le32(len);
1030
1031 gettimeofday(&tv, NULL);
1032 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
1033 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
1034
1035 memcpy(pdu->buf, buf, len);
1036
Jens Axboe40c60512012-03-27 16:03:04 +02001037 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL);
Jens Axboe084d1c62012-03-03 20:28:07 +01001038 free(pdu);
1039 return len;
Jens Axboe142575e2011-09-30 17:35:45 -06001040}
1041
Jens Axboea64e88d2011-10-03 14:20:01 +02001042static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
1043{
1044 dst->max_val = cpu_to_le64(src->max_val);
1045 dst->min_val = cpu_to_le64(src->min_val);
1046 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +02001047
1048 /*
1049 * Encode to IEEE 754 for network transfer
1050 */
Jens Axboe2b827fa2014-10-13 16:05:10 -06001051 dst->mean.u.i = cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
1052 dst->S.u.i = cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +02001053}
1054
1055static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1056{
1057 int i;
1058
Jens Axboe298921d2012-09-24 18:47:01 +02001059 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001060 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
1061 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
1062 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
1063 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
1064 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
1065 dst->agg[i] = cpu_to_le64(src->agg[i]);
1066 }
1067
1068 dst->kb_base = cpu_to_le32(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -07001069 dst->unit_base = cpu_to_le32(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +02001070 dst->groupid = cpu_to_le32(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +01001071 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +02001072}
1073
1074/*
1075 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1076 * into a single payload.
1077 */
1078void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1079{
1080 struct cmd_ts_pdu p;
1081 int i, j;
1082
Jens Axboe60efd142011-10-04 13:30:11 +02001083 dprint(FD_NET, "server sending end stats\n");
1084
Jens Axboe317b3c82011-10-03 21:47:27 +02001085 memset(&p, 0, sizeof(p));
1086
Jens Axboe4e59d0f2014-03-14 08:41:39 -06001087 strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1);
1088 strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1);
1089 strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1);
Jens Axboea64e88d2011-10-03 14:20:01 +02001090
Jens Axboe2f122b12012-03-15 13:10:19 +01001091 p.ts.error = cpu_to_le32(ts->error);
1092 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1093 p.ts.groupid = cpu_to_le32(ts->groupid);
1094 p.ts.pid = cpu_to_le32(ts->pid);
1095 p.ts.members = cpu_to_le32(ts->members);
Jens Axboe771e58b2013-01-30 12:56:23 +01001096 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +02001097
Jens Axboe298921d2012-09-24 18:47:01 +02001098 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001099 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1100 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1101 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1102 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1103 }
1104
1105 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1106 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1107 p.ts.ctx = cpu_to_le64(ts->ctx);
1108 p.ts.minf = cpu_to_le64(ts->minf);
1109 p.ts.majf = cpu_to_le64(ts->majf);
1110 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe623216d2014-11-07 18:47:41 -07001111 p.ts.percentile_precision = cpu_to_le64(ts->percentile_precision);
Jens Axboe802ad4a2011-10-05 09:51:58 +02001112
1113 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +02001114 fio_fp64_t *src = &ts->percentile_list[i];
1115 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +02001116
Jens Axboe2b827fa2014-10-13 16:05:10 -06001117 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +02001118 }
Jens Axboea64e88d2011-10-03 14:20:01 +02001119
1120 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1121 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1122 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1123 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1124 }
1125
1126 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1127 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1128 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1129 }
1130
Jens Axboe298921d2012-09-24 18:47:01 +02001131 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +02001132 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1133 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1134
Jens Axboe78799de2013-01-29 20:53:52 +01001135 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001136 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +02001137 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboe67e149c2014-10-09 13:27:44 -06001138 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +02001139 }
1140
Jens Axboe93eee042011-10-03 21:08:48 +02001141 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +02001142 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1143
Jens Axboe298921d2012-09-24 18:47:01 +02001144 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001145 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1146 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1147 }
1148
1149 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1150 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1151 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +02001152 p.ts.first_error = cpu_to_le32(ts->first_error);
1153 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -07001154 p.ts.unit_base = cpu_to_le32(ts->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +02001155
Jens Axboe3e260a42013-12-09 12:38:53 -07001156 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1157 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1158 p.ts.latency_window = cpu_to_le64(ts->latency_window);
Jens Axboe2b827fa2014-10-13 16:05:10 -06001159 p.ts.latency_percentile.u.i = cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
Jens Axboe3e260a42013-12-09 12:38:53 -07001160
Jens Axboea64e88d2011-10-03 14:20:01 +02001161 convert_gs(&p.rs, rs);
1162
Jens Axboe40c60512012-03-27 16:03:04 +02001163 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL);
Jens Axboea64e88d2011-10-03 14:20:01 +02001164}
1165
1166void fio_server_send_gs(struct group_run_stats *rs)
1167{
1168 struct group_run_stats gs;
1169
Jens Axboe60efd142011-10-04 13:30:11 +02001170 dprint(FD_NET, "server sending group run stats\n");
1171
Jens Axboea64e88d2011-10-03 14:20:01 +02001172 convert_gs(&gs, rs);
Jens Axboe40c60512012-03-27 16:03:04 +02001173 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL);
Jens Axboecf451d12011-10-03 16:48:30 +02001174}
1175
Jens Axboed09a64a2011-10-13 11:38:56 +02001176static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1177{
1178 int i;
1179
1180 for (i = 0; i < 2; i++) {
1181 dst->ios[i] = cpu_to_le32(src->ios[i]);
1182 dst->merges[i] = cpu_to_le32(src->merges[i]);
1183 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1184 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1185 }
1186
1187 dst->io_ticks = cpu_to_le32(src->io_ticks);
1188 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1189 dst->slavecount = cpu_to_le32(src->slavecount);
Jens Axboe2b827fa2014-10-13 16:05:10 -06001190 dst->max_util.u.i = cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
Jens Axboed09a64a2011-10-13 11:38:56 +02001191}
1192
1193static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1194{
1195 int i;
1196
Jens Axboe59140422014-04-14 08:28:08 -06001197 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1198 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
Jens Axboed09a64a2011-10-13 11:38:56 +02001199
1200 for (i = 0; i < 2; i++) {
Jens Axboea3b4cf72014-04-11 12:17:53 -06001201 dst->s.ios[i] = cpu_to_le32(src->s.ios[i]);
1202 dst->s.merges[i] = cpu_to_le32(src->s.merges[i]);
1203 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1204 dst->s.ticks[i] = cpu_to_le32(src->s.ticks[i]);
Jens Axboed09a64a2011-10-13 11:38:56 +02001205 }
1206
Jens Axboea3b4cf72014-04-11 12:17:53 -06001207 dst->s.io_ticks = cpu_to_le32(src->s.io_ticks);
1208 dst->s.time_in_queue = cpu_to_le32(src->s.time_in_queue);
1209 dst->s.msec = cpu_to_le64(src->s.msec);
Jens Axboed09a64a2011-10-13 11:38:56 +02001210}
1211
1212void fio_server_send_du(void)
1213{
1214 struct disk_util *du;
1215 struct flist_head *entry;
1216 struct cmd_du_pdu pdu;
1217
1218 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1219
Jens Axboe0766d922011-10-13 12:02:08 +02001220 memset(&pdu, 0, sizeof(pdu));
1221
Jens Axboed09a64a2011-10-13 11:38:56 +02001222 flist_for_each(entry, &disk_list) {
1223 du = flist_entry(entry, struct disk_util, list);
1224
1225 convert_dus(&pdu.dus, &du->dus);
1226 convert_agg(&pdu.agg, &du->agg);
1227
Jens Axboe40c60512012-03-27 16:03:04 +02001228 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboed09a64a2011-10-13 11:38:56 +02001229 }
1230}
1231
Jens Axboe53bd8db2012-03-14 21:51:38 +01001232/*
1233 * Send a command with a separate PDU, not inlined in the command
1234 */
1235static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1236 off_t size, uint64_t tag, uint32_t flags)
1237{
1238 struct fio_net_cmd cmd;
1239 struct iovec iov[2];
1240
Jens Axboe9f247262014-03-03 13:53:39 -07001241 iov[0].iov_base = (void *) &cmd;
Jens Axboe53bd8db2012-03-14 21:51:38 +01001242 iov[0].iov_len = sizeof(cmd);
1243 iov[1].iov_base = (void *) buf;
1244 iov[1].iov_len = size;
1245
1246 __fio_init_net_cmd(&cmd, opcode, size, tag);
1247 cmd.flags = __cpu_to_le32(flags);
1248 fio_net_cmd_crc_pdu(&cmd, buf);
1249
Jens Axboe8c953072012-03-20 14:35:35 +01001250 return fio_sendv_data(sk, iov, 2);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001251}
1252
Jens Axboe3989b142013-04-12 13:13:24 +02001253static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log)
Jens Axboe1b427252012-03-14 15:03:03 +01001254{
Jens Axboe3989b142013-04-12 13:13:24 +02001255 int ret = 0;
1256#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +01001257 z_stream stream;
1258 void *out_pdu;
Jens Axboe1b427252012-03-14 15:03:03 +01001259
1260 /*
1261 * Dirty - since the log is potentially huge, compress it into
1262 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1263 * side defragment it.
1264 */
1265 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1266
1267 stream.zalloc = Z_NULL;
1268 stream.zfree = Z_NULL;
1269 stream.opaque = Z_NULL;
1270
1271 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001272 ret = 1;
1273 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001274 }
1275
Jens Axboef5ed7652012-03-14 16:28:07 +01001276 stream.next_in = (void *) log->log;
Jens Axboeccefd5f2014-06-30 20:59:03 -06001277 stream.avail_in = log->nr_samples * log_entry_sz(log);
Jens Axboe1b427252012-03-14 15:03:03 +01001278
1279 do {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001280 unsigned int this_len, flags = 0;
Jens Axboe1b427252012-03-14 15:03:03 +01001281
1282 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1283 stream.next_out = out_pdu;
Jens Axboe3c547fe2012-03-14 21:53:00 +01001284 ret = deflate(&stream, Z_FINISH);
1285 /* may be Z_OK, or Z_STREAM_END */
1286 if (ret < 0)
1287 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001288
1289 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1290
Jens Axboe1b427252012-03-14 15:03:03 +01001291 if (stream.avail_in)
Jens Axboe53bd8db2012-03-14 21:51:38 +01001292 flags = FIO_NET_CMD_F_MORE;
Jens Axboe1b427252012-03-14 15:03:03 +01001293
Jens Axboe53bd8db2012-03-14 21:51:38 +01001294 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
1295 out_pdu, this_len, 0, flags);
1296 if (ret)
1297 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001298 } while (stream.avail_in);
1299
Jens Axboe53bd8db2012-03-14 21:51:38 +01001300err_zlib:
Jens Axboe1b427252012-03-14 15:03:03 +01001301 deflateEnd(&stream);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001302err:
1303 free(out_pdu);
Jens Axboe3989b142013-04-12 13:13:24 +02001304#endif
Jens Axboe53bd8db2012-03-14 21:51:38 +01001305 return ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001306}
1307
Jens Axboe3989b142013-04-12 13:13:24 +02001308int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1309{
1310 struct cmd_iolog_pdu pdu;
1311 int i, ret = 0;
1312
Jens Axboeccefd5f2014-06-30 20:59:03 -06001313 pdu.nr_samples = cpu_to_le64(log->nr_samples);
Jens Axboe3989b142013-04-12 13:13:24 +02001314 pdu.thread_number = cpu_to_le32(td->thread_number);
Jens Axboe3989b142013-04-12 13:13:24 +02001315 pdu.log_type = cpu_to_le32(log->log_type);
1316 pdu.compressed = cpu_to_le32(use_zlib);
Jens Axboe9bdb9262014-04-14 11:45:34 -06001317
1318 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1319 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
Jens Axboe3989b142013-04-12 13:13:24 +02001320
1321 for (i = 0; i < log->nr_samples; i++) {
Jens Axboeccefd5f2014-06-30 20:59:03 -06001322 struct io_sample *s = get_sample(log, i);
Jens Axboe3989b142013-04-12 13:13:24 +02001323
Jens Axboebac4af12014-07-03 13:42:28 -06001324 s->time = cpu_to_le64(s->time);
1325 s->val = cpu_to_le64(s->val);
1326 s->__ddir = cpu_to_le32(s->__ddir);
1327 s->bs = cpu_to_le32(s->bs);
Jens Axboeccefd5f2014-06-30 20:59:03 -06001328
1329 if (log->log_offset) {
1330 struct io_sample_offset *so = (void *) s;
1331
1332 so->offset = cpu_to_le64(so->offset);
1333 }
Jens Axboe3989b142013-04-12 13:13:24 +02001334 }
1335
1336 /*
1337 * Send header first, it's not compressed.
1338 */
1339 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1340 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1341 if (ret)
1342 return ret;
1343
1344 /*
1345 * Now send actual log, compress if we can, otherwise just plain
1346 */
1347 if (use_zlib)
1348 return fio_send_iolog_gz(&pdu, log);
1349
1350 return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log,
Jens Axboeccefd5f2014-06-30 20:59:03 -06001351 log->nr_samples * log_entry_sz(log), 0, 0);
Jens Axboe3989b142013-04-12 13:13:24 +02001352}
1353
Jens Axboe2f122b12012-03-15 13:10:19 +01001354void fio_server_send_add_job(struct thread_data *td)
Jens Axboe807f9972012-03-02 10:25:24 +01001355{
1356 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +01001357
Jens Axboe731e30a2012-03-14 16:35:37 +01001358 memset(&pdu, 0, sizeof(pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +01001359 pdu.thread_number = cpu_to_le32(td->thread_number);
1360 pdu.groupid = cpu_to_le32(td->groupid);
1361 convert_thread_options_to_net(&pdu.top, &td->o);
Jens Axboe807f9972012-03-02 10:25:24 +01001362
Jens Axboe40c60512012-03-27 16:03:04 +02001363 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboe807f9972012-03-02 10:25:24 +01001364}
1365
Jens Axboe122c7722012-03-19 09:13:15 +01001366void fio_server_send_start(struct thread_data *td)
1367{
1368 assert(server_fd != -1);
1369
1370 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1371}
1372
Jens Axboede54cfd2014-11-10 20:34:00 -07001373int fio_server_get_verify_state(const char *name, int threadnumber,
1374 void **datap)
1375{
1376 struct thread_io_list *s;
1377 struct cmd_sendfile out;
1378 struct cmd_reply *rep;
1379 uint64_t tag;
1380 void *data;
1381
1382 dprint(FD_NET, "server: request verify state\n");
1383
1384 rep = smalloc(sizeof(*rep));
1385 if (!rep) {
1386 log_err("fio: smalloc pool too small\n");
1387 return 1;
1388 }
1389
1390 __fio_mutex_init(&rep->lock, FIO_MUTEX_LOCKED);
1391 rep->data = NULL;
1392 rep->error = 0;
1393
Jens Axboe3ebcfd52014-11-11 20:51:37 -07001394 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
1395 threadnumber);
Jens Axboede54cfd2014-11-10 20:34:00 -07001396 tag = (uint64_t) (uintptr_t) rep;
1397 fio_net_send_cmd(server_fd, FIO_NET_CMD_SENDFILE, &out, sizeof(out),
1398 &tag, NULL);
1399
1400 /*
1401 * Wait for the backend to receive the reply
1402 */
1403 if (fio_mutex_down_timeout(&rep->lock, 10)) {
1404 log_err("fio: timed out waiting for reply\n");
1405 goto fail;
1406 }
1407
1408 if (rep->error) {
1409 log_err("fio: failure on receiving state file: %s\n", strerror(rep->error));
1410fail:
1411 *datap = NULL;
1412 sfree(rep);
1413 fio_net_send_quit(server_fd);
1414 return 1;
1415 }
1416
1417 /*
1418 * The format is verify_state_hdr, then thread_io_list. Verify
1419 * the header, and the thread_io_list checksum
1420 */
1421 s = rep->data + sizeof(struct verify_state_hdr);
1422 if (verify_state_hdr(rep->data, s))
1423 goto fail;
1424
1425 /*
1426 * Don't need the header from now, copy just the thread_io_list
1427 */
1428 rep->size -= sizeof(struct verify_state_hdr);
1429 data = malloc(rep->size);
1430 memcpy(data, s, rep->size);
1431 *datap = data;
1432
1433 sfree(rep->data);
1434 sfree(rep);
1435 return 0;
1436}
1437
Jens Axboe87aa8f12011-10-06 21:24:13 +02001438static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +02001439{
Jens Axboe811826b2011-10-24 09:11:50 +02001440 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001441 socklen_t socklen;
Jens Axboe17e35312014-03-25 10:50:55 -07001442 char buf[80];
1443 const char *str;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001444 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +02001445
Jens Axboe811826b2011-10-24 09:11:50 +02001446 if (use_ipv6)
1447 sk = socket(AF_INET6, SOCK_STREAM, 0);
1448 else
1449 sk = socket(AF_INET, SOCK_STREAM, 0);
1450
Jens Axboe81179ee2011-10-04 12:42:06 +02001451 if (sk < 0) {
1452 log_err("fio: socket: %s\n", strerror(errno));
1453 return -1;
1454 }
1455
1456 opt = 1;
Jens Axboe1f819912013-01-23 17:21:41 -07001457 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001458 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001459 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001460 return -1;
1461 }
1462#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +02001463 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001464 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001465 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001466 return -1;
1467 }
1468#endif
1469
Jens Axboe811826b2011-10-24 09:11:50 +02001470 if (use_ipv6) {
Jens Axboe17e35312014-03-25 10:50:55 -07001471 const void *src = &saddr_in6.sin6_addr;
1472
Jens Axboe811826b2011-10-24 09:11:50 +02001473 addr = (struct sockaddr *) &saddr_in6;
1474 socklen = sizeof(saddr_in6);
1475 saddr_in6.sin6_family = AF_INET6;
Jens Axboe17e35312014-03-25 10:50:55 -07001476 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
Jens Axboe811826b2011-10-24 09:11:50 +02001477 } else {
Jens Axboe17e35312014-03-25 10:50:55 -07001478 const void *src = &saddr_in.sin_addr;
1479
Jens Axboe811826b2011-10-24 09:11:50 +02001480 addr = (struct sockaddr *) &saddr_in;
1481 socklen = sizeof(saddr_in);
1482 saddr_in.sin_family = AF_INET;
Jens Axboe17e35312014-03-25 10:50:55 -07001483 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
Jens Axboe811826b2011-10-24 09:11:50 +02001484 }
Jens Axboe81179ee2011-10-04 12:42:06 +02001485
Jens Axboe811826b2011-10-24 09:11:50 +02001486 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001487 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboe17e35312014-03-25 10:50:55 -07001488 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
Jens Axboeb94cba42011-10-06 21:27:10 +02001489 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001490 return -1;
1491 }
1492
Jens Axboe87aa8f12011-10-06 21:24:13 +02001493 return sk;
1494}
1495
1496static int fio_init_server_sock(void)
1497{
1498 struct sockaddr_un addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001499 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001500 mode_t mode;
1501 int sk;
1502
1503 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1504 if (sk < 0) {
1505 log_err("fio: socket: %s\n", strerror(errno));
1506 return -1;
1507 }
1508
1509 mode = umask(000);
1510
1511 memset(&addr, 0, sizeof(addr));
1512 addr.sun_family = AF_UNIX;
Jens Axboea48fddb2014-04-14 08:55:13 -06001513 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001514
1515 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1516
1517 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1518 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001519 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001520 return -1;
1521 }
1522
1523 umask(mode);
1524 return sk;
1525}
1526
1527static int fio_init_server_connection(void)
1528{
Jens Axboebebe6392011-10-07 10:00:51 +02001529 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001530 int sk;
1531
1532 dprint(FD_NET, "starting server\n");
1533
1534 if (!bind_sock)
1535 sk = fio_init_server_ip();
1536 else
1537 sk = fio_init_server_sock();
1538
1539 if (sk < 0)
1540 return sk;
1541
Jens Axboeafdcad22014-04-14 08:54:09 -06001542 memset(bind_str, 0, sizeof(bind_str));
1543
Jens Axboe811826b2011-10-24 09:11:50 +02001544 if (!bind_sock) {
1545 char *p, port[16];
1546 const void *src;
1547 int af;
1548
1549 if (use_ipv6) {
1550 af = AF_INET6;
1551 src = &saddr_in6.sin6_addr;
1552 } else {
1553 af = AF_INET;
1554 src = &saddr_in.sin_addr;
1555 }
1556
1557 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1558
1559 sprintf(port, ",%u", fio_net_port);
1560 if (p)
1561 strcat(p, port);
1562 else
Jens Axboeafdcad22014-04-14 08:54:09 -06001563 strncpy(bind_str, port, sizeof(bind_str) - 1);
Jens Axboe811826b2011-10-24 09:11:50 +02001564 } else
Jens Axboeafdcad22014-04-14 08:54:09 -06001565 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
Jens Axboebebe6392011-10-07 10:00:51 +02001566
1567 log_info("fio: server listening on %s\n", bind_str);
1568
Jens Axboe89c17072011-10-11 10:15:51 +02001569 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001570 log_err("fio: listen: %s\n", strerror(errno));
Jens Axboe2fd973c2014-04-08 21:13:42 -06001571 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001572 return -1;
1573 }
1574
Jens Axboe87aa8f12011-10-06 21:24:13 +02001575 return sk;
1576}
1577
Jens Axboe3aa3cee2014-01-24 18:56:56 -08001578int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001579 struct in6_addr *inp6)
1580
1581{
1582 int ret = 0;
1583
Jens Axboe3aa3cee2014-01-24 18:56:56 -08001584 if (ipv6)
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001585 ret = inet_pton(AF_INET6, host, inp6);
1586 else
1587 ret = inet_pton(AF_INET, host, inp);
1588
1589 if (ret != 1) {
Jens Axboe479471c2014-01-23 20:42:06 -08001590 struct addrinfo hints, *res;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001591
Jens Axboe479471c2014-01-23 20:42:06 -08001592 memset(&hints, 0, sizeof(hints));
Jens Axboe3aa3cee2014-01-24 18:56:56 -08001593 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
Jens Axboe479471c2014-01-23 20:42:06 -08001594 hints.ai_socktype = SOCK_STREAM;
1595
1596 ret = getaddrinfo(host, NULL, &hints, &res);
1597 if (ret) {
1598 log_err("fio: failed to resolve <%s> (%s)\n", host,
1599 gai_strerror(ret));
Jens Axboe3caf43e2014-01-24 18:52:16 -08001600 return 1;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001601 }
1602
Jens Axboe3aa3cee2014-01-24 18:56:56 -08001603 if (ipv6)
Jens Axboe479471c2014-01-23 20:42:06 -08001604 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
1605 else
1606 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
1607
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001608 ret = 1;
Jens Axboe479471c2014-01-23 20:42:06 -08001609 freeaddrinfo(res);
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001610 }
1611
1612 return !(ret == 1);
1613}
1614
Jens Axboe660a2bf2011-10-24 09:35:06 +02001615/*
1616 * Parse a host/ip/port string. Reads from 'str'.
1617 *
1618 * Outputs:
1619 *
1620 * For IPv4:
1621 * *ptr is the host, *port is the port, inp is the destination.
1622 * For IPv6:
1623 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1624 * For local domain sockets:
1625 * *ptr is the filename, *is_sock is 1.
1626 */
Jens Axboebebe6392011-10-07 10:00:51 +02001627int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001628 int *port, struct in_addr *inp,
1629 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001630{
Jens Axboe76867622011-10-25 09:52:51 +02001631 const char *host = str;
1632 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001633 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001634
Jens Axboebebe6392011-10-07 10:00:51 +02001635 *ptr = NULL;
1636 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001637 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001638 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001639
1640 if (!strncmp(str, "sock:", 5)) {
1641 *ptr = strdup(str + 5);
1642 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001643
Jens Axboe76867622011-10-25 09:52:51 +02001644 return 0;
1645 }
1646
1647 /*
1648 * Is it ip:<ip or host>:port
1649 */
1650 if (!strncmp(host, "ip:", 3))
1651 host += 3;
1652 else if (!strncmp(host, "ip4:", 4))
1653 host += 4;
1654 else if (!strncmp(host, "ip6:", 4)) {
1655 host += 4;
1656 *ipv6 = 1;
1657 } else if (host[0] == ':') {
1658 /* String is :port */
1659 host++;
1660 lport = atoi(host);
1661 if (!lport || lport > 65535) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +02001662 log_err("fio: bad server port %u\n", lport);
Jens Axboe76867622011-10-25 09:52:51 +02001663 return 1;
1664 }
1665 /* no hostname given, we are done */
1666 *port = lport;
1667 return 0;
1668 }
1669
1670 /*
Jens Axboeb96b90c2014-03-25 10:37:56 -07001671 * If no port seen yet, check if there's a last ',' at the end
Jens Axboe76867622011-10-25 09:52:51 +02001672 */
1673 if (!lport) {
1674 portp = strchr(host, ',');
1675 if (portp) {
1676 *portp = '\0';
1677 portp++;
1678 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001679 if (!lport || lport > 65535) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +02001680 log_err("fio: bad server port %u\n", lport);
Jens Axboebebe6392011-10-07 10:00:51 +02001681 return 1;
1682 }
Jens Axboe76867622011-10-25 09:52:51 +02001683 }
1684 }
1685
1686 if (lport)
1687 *port = lport;
1688
1689 if (!strlen(host))
1690 return 0;
1691
1692 *ptr = strdup(host);
1693
Jens Axboe3aa3cee2014-01-24 18:56:56 -08001694 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001695 free(*ptr);
1696 *ptr = NULL;
1697 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001698 }
1699
1700 if (*port == 0)
1701 *port = fio_net_port;
1702
1703 return 0;
1704}
1705
Jens Axboe87aa8f12011-10-06 21:24:13 +02001706/*
1707 * Server arg should be one of:
1708 *
1709 * sock:/path/to/socket
1710 * ip:1.2.3.4
1711 * 1.2.3.4
1712 *
1713 * Where sock uses unix domain sockets, and ip binds the server to
1714 * a specific interface. If no arguments are given to the server, it
1715 * uses IP and binds to 0.0.0.0.
1716 *
1717 */
1718static int fio_handle_server_arg(void)
1719{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001720 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001721 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001722
Jens Axboe87aa8f12011-10-06 21:24:13 +02001723 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1724
1725 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001726 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001727
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001728 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001729 &port, &saddr_in.sin_addr,
1730 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001731
1732 if (!is_sock && bind_sock) {
1733 free(bind_sock);
1734 bind_sock = NULL;
1735 }
1736
Jens Axboea7de0a12011-10-07 12:55:14 +02001737out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001738 fio_net_port = port;
1739 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001740 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001741 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001742}
1743
Jens Axboe43cdea12014-03-25 15:06:17 -07001744static void sig_int(int sig)
1745{
1746 if (bind_sock)
1747 unlink(bind_sock);
1748}
1749
1750static void set_sig_handlers(void)
1751{
1752 struct sigaction act;
1753
1754 memset(&act, 0, sizeof(act));
1755 act.sa_handler = sig_int;
1756 act.sa_flags = SA_RESTART;
1757 sigaction(SIGINT, &act, NULL);
1758}
1759
Jens Axboe87aa8f12011-10-06 21:24:13 +02001760static int fio_server(void)
1761{
1762 int sk, ret;
1763
1764 dprint(FD_NET, "starting server\n");
1765
1766 if (fio_handle_server_arg())
1767 return -1;
1768
1769 sk = fio_init_server_connection();
1770 if (sk < 0)
1771 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001772
Jens Axboe43cdea12014-03-25 15:06:17 -07001773 set_sig_handlers();
1774
Jens Axboe81179ee2011-10-04 12:42:06 +02001775 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001776
Jens Axboe81179ee2011-10-04 12:42:06 +02001777 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001778
1779 if (fio_server_arg) {
1780 free(fio_server_arg);
1781 fio_server_arg = NULL;
1782 }
Jens Axboebebe6392011-10-07 10:00:51 +02001783 if (bind_sock)
1784 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001785
Jens Axboe81179ee2011-10-04 12:42:06 +02001786 return ret;
1787}
1788
Jens Axboe7b821682011-10-11 12:16:32 +02001789void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001790{
Jens Axboe7b821682011-10-11 12:16:32 +02001791 if (signal == SIGPIPE)
1792 server_fd = -1;
1793 else {
1794 log_info("\nfio: terminating on signal %d\n", signal);
1795 exit_backend = 1;
1796 }
Jens Axboe9abea482011-10-04 13:21:54 +02001797}
1798
Jens Axboe13755d92011-10-10 19:51:26 +02001799static int check_existing_pidfile(const char *pidfile)
1800{
1801 struct stat sb;
1802 char buf[16];
1803 pid_t pid;
1804 FILE *f;
1805
1806 if (stat(pidfile, &sb))
1807 return 0;
1808
1809 f = fopen(pidfile, "r");
1810 if (!f)
1811 return 0;
1812
Jens Axboebfc3b172011-10-10 21:16:55 +02001813 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001814 fclose(f);
1815 return 1;
1816 }
1817 fclose(f);
1818
1819 pid = atoi(buf);
1820 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001821 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001822
1823 return 1;
1824}
1825
1826static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001827{
1828 FILE *fpid;
1829
1830 fpid = fopen(pidfile, "w");
1831 if (!fpid) {
1832 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001833 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001834 }
1835
1836 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001837 fclose(fpid);
1838 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001839}
1840
1841/*
1842 * If pidfile is specified, background us.
1843 */
1844int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001845{
1846 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001847 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001848
Bruce Cran93bcfd22012-02-20 20:18:19 +01001849#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001850 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +02001851 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001852#endif
1853
Jens Axboe402668f2011-10-10 15:28:58 +02001854 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001855 return fio_server();
1856
Jens Axboe13755d92011-10-10 19:51:26 +02001857 if (check_existing_pidfile(pidfile)) {
1858 log_err("fio: pidfile %s exists and server appears alive\n",
1859 pidfile);
Jens Axboeb8ba87a2014-04-11 11:37:34 -06001860 free(pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001861 return -1;
1862 }
1863
Jens Axboee46d8092011-10-03 09:11:02 +02001864 pid = fork();
1865 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001866 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001867 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001868 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001869 } else if (pid) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -07001870 ret = write_pid(pid, pidfile);
Jens Axboeb8ba87a2014-04-11 11:37:34 -06001871 free(pidfile);
Jens Axboe5ea73552014-09-11 17:05:58 -06001872 _exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001873 }
Jens Axboee46d8092011-10-03 09:11:02 +02001874
1875 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001876 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1877 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001878 close(STDIN_FILENO);
1879 close(STDOUT_FILENO);
1880 close(STDERR_FILENO);
1881 f_out = NULL;
1882 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001883
1884 ret = fio_server();
1885
1886 closelog();
1887 unlink(pidfile);
1888 free(pidfile);
1889 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001890}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001891
Jens Axboebebe6392011-10-07 10:00:51 +02001892void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001893{
1894 fio_server_arg = strdup(arg);
1895}