blob: 6e96b77a46432cc3371d9cf1fb4f28e8555a39c9 [file] [log] [blame]
Jens Axboe50d16972011-09-29 17:45:28 -06001#include <stdio.h>
2#include <stdlib.h>
Jens Axboe142575e2011-09-30 17:35:45 -06003#include <stdarg.h>
Jens Axboe50d16972011-09-29 17:45:28 -06004#include <unistd.h>
5#include <limits.h>
Jens Axboe50d16972011-09-29 17:45:28 -06006#include <errno.h>
7#include <fcntl.h>
8#include <sys/poll.h>
Jens Axboe50d16972011-09-29 17:45:28 -06009#include <sys/types.h>
10#include <sys/wait.h>
Jens Axboed05c4a02011-10-05 00:16:11 +020011#include <sys/socket.h>
Jens Axboe87aa8f12011-10-06 21:24:13 +020012#include <sys/stat.h>
13#include <sys/un.h>
Bruce Cran1f51bba2013-04-12 15:40:21 +020014#include <sys/uio.h>
Jens Axboe50d16972011-09-29 17:45:28 -060015#include <netinet/in.h>
16#include <arpa/inet.h>
17#include <netdb.h>
Jens Axboee46d8092011-10-03 09:11:02 +020018#include <syslog.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020019#include <signal.h>
Jens Axboe3989b142013-04-12 13:13:24 +020020#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +010021#include <zlib.h>
Jens Axboe3989b142013-04-12 13:13:24 +020022#endif
Jens Axboe50d16972011-09-29 17:45:28 -060023
24#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060025#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060026#include "crc/crc16.h"
Jens Axboec7c6cb42011-10-13 14:12:40 +020027#include "lib/ieee754.h"
Jens Axboe50d16972011-09-29 17:45:28 -060028
Stephen M. Cameron5adc2442012-02-24 08:17:31 +010029int fio_net_port = FIO_NET_PORT;
Jens Axboe50d16972011-09-29 17:45:28 -060030
Jens Axboe009b1be2011-09-29 18:27:02 -060031int exit_backend = 0;
32
Jens Axboe46c48f12011-10-01 12:50:51 -060033static int server_fd = -1;
Jens Axboe87aa8f12011-10-06 21:24:13 +020034static char *fio_server_arg;
35static char *bind_sock;
36static struct sockaddr_in saddr_in;
Jens Axboe811826b2011-10-24 09:11:50 +020037static struct sockaddr_in6 saddr_in6;
Jens Axboe811826b2011-10-24 09:11:50 +020038static int use_ipv6;
Jens Axboe3989b142013-04-12 13:13:24 +020039#ifdef CONFIG_ZLIB
40static unsigned int has_zlib = 1;
41#else
42static unsigned int has_zlib = 0;
43#endif
44static unsigned int use_zlib;
Jens Axboe37db14f2011-09-30 17:00:42 -060045
Jens Axboe122c7722012-03-19 09:13:15 +010046struct fio_fork_item {
47 struct flist_head list;
48 int exitval;
49 int signal;
50 int exited;
51 pid_t pid;
52};
53
Jens Axboe89c17072011-10-11 10:15:51 +020054static const char *fio_server_ops[FIO_NET_CMD_NR] = {
55 "",
56 "QUIT",
57 "EXIT",
58 "JOB",
59 "JOBLINE",
60 "TEXT",
61 "TS",
62 "GS",
63 "SEND_ETA",
64 "ETA",
65 "PROBE",
66 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020067 "STOP",
68 "DISK_UTIL",
Jens Axboeb9d2f302012-03-08 20:36:28 +010069 "SERVER_START",
Jens Axboe807f9972012-03-02 10:25:24 +010070 "ADD_JOB",
Jens Axboeb9d2f302012-03-08 20:36:28 +010071 "CMD_RUN"
Jens Axboec70e63e2012-03-15 08:26:18 +010072 "CMD_IOLOG",
Jens Axboe89c17072011-10-11 10:15:51 +020073};
74
75const char *fio_server_op(unsigned int op)
76{
77 static char buf[32];
78
79 if (op < FIO_NET_CMD_NR)
80 return fio_server_ops[op];
81
82 sprintf(buf, "UNKNOWN/%d", op);
83 return buf;
84}
85
Jens Axboe5235f622012-03-14 21:25:51 +010086static ssize_t iov_total_len(const struct iovec *iov, int count)
Jens Axboe132159a2011-09-30 15:01:32 -060087{
Jens Axboe5235f622012-03-14 21:25:51 +010088 ssize_t ret = 0;
89
90 while (count--) {
91 ret += iov->iov_len;
92 iov++;
93 }
94
95 return ret;
96}
97
98static int fio_sendv_data(int sk, struct iovec *iov, int count)
99{
100 ssize_t total_len = iov_total_len(iov, count);
101 ssize_t ret;
Jens Axboe794d69c2011-10-01 08:48:50 -0600102
Jens Axboe132159a2011-09-30 15:01:32 -0600103 do {
Jens Axboe5235f622012-03-14 21:25:51 +0100104 ret = writev(sk, iov, count);
Jens Axboe132159a2011-09-30 15:01:32 -0600105 if (ret > 0) {
Jens Axboe5235f622012-03-14 21:25:51 +0100106 total_len -= ret;
107 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600108 break;
Jens Axboe5235f622012-03-14 21:25:51 +0100109
110 while (ret) {
111 if (ret >= iov->iov_len) {
112 ret -= iov->iov_len;
113 iov++;
114 continue;
115 }
116 iov->iov_base += ret;
117 iov->iov_len -= ret;
118 ret = 0;
119 }
Jens Axboe132159a2011-09-30 15:01:32 -0600120 } else if (!ret)
121 break;
122 else if (errno == EAGAIN || errno == EINTR)
123 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200124 else
125 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600126 } while (!exit_backend);
127
Jens Axboe5235f622012-03-14 21:25:51 +0100128 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600129 return 0;
130
Jens Axboe8b4e61b2012-03-09 17:10:54 +0100131 if (errno)
132 return -errno;
133
Jens Axboe132159a2011-09-30 15:01:32 -0600134 return 1;
135}
136
Jens Axboe5235f622012-03-14 21:25:51 +0100137int fio_send_data(int sk, const void *p, unsigned int len)
138{
139 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
140
141 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
142
143 return fio_sendv_data(sk, &iov, 1);
144}
145
Jens Axboe132159a2011-09-30 15:01:32 -0600146int fio_recv_data(int sk, void *p, unsigned int len)
147{
148 do {
149 int ret = recv(sk, p, len, MSG_WAITALL);
150
151 if (ret > 0) {
152 len -= ret;
153 if (!len)
154 break;
155 p += ret;
156 continue;
157 } else if (!ret)
158 break;
159 else if (errno == EAGAIN || errno == EINTR)
160 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200161 else
162 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600163 } while (!exit_backend);
164
165 if (!len)
166 return 0;
167
168 return -1;
169}
170
171static int verify_convert_cmd(struct fio_net_cmd *cmd)
172{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600173 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600174
Jens Axboefcee5ff2011-09-30 22:50:39 -0600175 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
176 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600177
Jens Axboe25dfa842012-02-29 10:01:34 +0100178 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600179 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600180 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600181 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600182 return 1;
183 }
184
185 cmd->version = le16_to_cpu(cmd->version);
186 cmd->opcode = le16_to_cpu(cmd->opcode);
187 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200188 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600189 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
190
191 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200192 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600193 break;
194 default:
195 log_err("fio: bad server cmd version %d\n", cmd->version);
196 return 1;
197 }
198
Jens Axboeb9d2f302012-03-08 20:36:28 +0100199 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600200 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
201 return 1;
202 }
203
204 return 0;
205}
206
Jens Axboea64e88d2011-10-03 14:20:01 +0200207/*
208 * Read (and defragment, if necessary) incoming commands
209 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200210struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600211{
Jens Axboea64e88d2011-10-03 14:20:01 +0200212 struct fio_net_cmd cmd, *cmdret = NULL;
213 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600214 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200215 int ret, first = 1;
216 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600217
Jens Axboea64e88d2011-10-03 14:20:01 +0200218 do {
219 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
220 if (ret)
221 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600222
Jens Axboea64e88d2011-10-03 14:20:01 +0200223 /* We have a command, verify it and swap if need be */
224 ret = verify_convert_cmd(&cmd);
225 if (ret)
226 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600227
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200228 if (first) {
229 /* if this is text, add room for \0 at the end */
230 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
231 assert(!cmdret);
232 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200233 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600234
Jens Axboea64e88d2011-10-03 14:20:01 +0200235 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600236
Jens Axboea64e88d2011-10-03 14:20:01 +0200237 if (first)
238 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200239 else if (cmdret->opcode != cmd.opcode) {
240 log_err("fio: fragment opcode mismatch (%d != %d)\n",
241 cmdret->opcode, cmd.opcode);
242 ret = 1;
243 break;
244 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200245
246 if (!cmd.pdu_len)
247 break;
248
249 /* There's payload, get it */
250 pdu = (void *) cmdret->payload + pdu_offset;
251 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
252 if (ret)
253 break;
254
255 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100256 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200257 if (crc != cmd.pdu_crc16) {
258 log_err("fio: server bad crc on payload ");
259 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
260 ret = 1;
261 break;
262 }
263
264 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200265 if (!first)
266 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200267 first = 0;
268 } while (cmd.flags & FIO_NET_CMD_F_MORE);
269
270 if (ret) {
271 free(cmdret);
272 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200273 } else if (cmdret) {
274 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100275 if (cmdret->pdu_len) {
276 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
277 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
278 char *buf = (char *) pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200279
Jens Axboe3c3ed072012-03-27 09:12:39 +0200280 buf[pdu->buf_len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100281 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
Jens Axboe46bcd492012-03-14 11:31:21 +0100282 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
283 char *buf = (char *) pdu->buf;
284 int len = le32_to_cpu(pdu->buf_len);
Jens Axboe084d1c62012-03-03 20:28:07 +0100285
Jens Axboe46bcd492012-03-14 11:31:21 +0100286 buf[len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100287 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200288 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100289
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200290 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200291 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200292 }
Jens Axboe132159a2011-09-30 15:01:32 -0600293
Jens Axboea64e88d2011-10-03 14:20:01 +0200294 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600295}
296
Jens Axboe40c60512012-03-27 16:03:04 +0200297static void add_reply(uint64_t tag, struct flist_head *list)
298{
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200299 struct fio_net_cmd_reply *reply;
Jens Axboe40c60512012-03-27 16:03:04 +0200300
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200301 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
Jens Axboe40c60512012-03-27 16:03:04 +0200302 flist_add_tail(&reply->list, list);
303}
304
305static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
306{
307 struct fio_net_cmd_reply *reply;
308
309 reply = calloc(1, sizeof(*reply));
310 INIT_FLIST_HEAD(&reply->list);
311 gettimeofday(&reply->tv, NULL);
312 reply->saved_tag = tag;
313 reply->opcode = opcode;
314
315 return (uintptr_t) reply;
316}
317
318static void free_reply(uint64_t tag)
319{
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200320 struct fio_net_cmd_reply *reply;
Jens Axboe40c60512012-03-27 16:03:04 +0200321
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200322 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
Jens Axboe40c60512012-03-27 16:03:04 +0200323 free(reply);
324}
325
Jens Axboe53bd8db2012-03-14 21:51:38 +0100326void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
Jens Axboe132159a2011-09-30 15:01:32 -0600327{
328 uint32_t pdu_len;
329
Jens Axboe25dfa842012-02-29 10:01:34 +0100330 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600331
332 pdu_len = le32_to_cpu(cmd->pdu_len);
Jens Axboe1b427252012-03-14 15:03:03 +0100333 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
334}
335
336void fio_net_cmd_crc(struct fio_net_cmd *cmd)
337{
338 fio_net_cmd_crc_pdu(cmd, cmd->payload);
Jens Axboe132159a2011-09-30 15:01:32 -0600339}
340
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200341int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
Jens Axboe40c60512012-03-27 16:03:04 +0200342 uint64_t *tagptr, struct flist_head *list)
Jens Axboe794d69c2011-10-01 08:48:50 -0600343{
Jens Axboe7f868312011-10-10 09:55:21 +0200344 struct fio_net_cmd *cmd = NULL;
345 size_t this_len, cur_len = 0;
Jens Axboe40c60512012-03-27 16:03:04 +0200346 uint64_t tag;
Jens Axboe794d69c2011-10-01 08:48:50 -0600347 int ret;
348
Jens Axboe40c60512012-03-27 16:03:04 +0200349 if (list) {
350 assert(tagptr);
351 tag = *tagptr = alloc_reply(*tagptr, opcode);
352 } else
353 tag = tagptr ? *tagptr : 0;
354
Jens Axboe794d69c2011-10-01 08:48:50 -0600355 do {
356 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100357 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
358 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600359
Jens Axboe7f868312011-10-10 09:55:21 +0200360 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
361 if (cmd)
362 free(cmd);
363
364 cur_len = sizeof(*cmd) + this_len;
365 cmd = malloc(cur_len);
366 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600367
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200368 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600369
370 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200371 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600372
373 fio_net_cmd_crc(cmd);
374
375 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600376 size -= this_len;
377 buf += this_len;
378 } while (!ret && size);
379
Jens Axboe40c60512012-03-27 16:03:04 +0200380 if (list) {
381 if (ret)
382 free_reply(tag);
383 else
384 add_reply(tag, list);
385 }
386
Jens Axboe7f868312011-10-10 09:55:21 +0200387 if (cmd)
388 free(cmd);
389
Jens Axboe794d69c2011-10-01 08:48:50 -0600390 return ret;
391}
392
Jens Axboe89c17072011-10-11 10:15:51 +0200393static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600394{
Jens Axboe178cde92011-10-05 22:14:31 +0200395 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600396
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200397 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600398 fio_net_cmd_crc(&cmd);
399
400 return fio_send_data(sk, &cmd, sizeof(cmd));
401}
402
Jens Axboe89c17072011-10-11 10:15:51 +0200403/*
404 * If 'list' is non-NULL, then allocate and store the sent command for
405 * later verification.
406 */
407int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
408 struct flist_head *list)
409{
Jens Axboe89c17072011-10-11 10:15:51 +0200410 int ret;
411
Jens Axboe40c60512012-03-27 16:03:04 +0200412 if (list)
413 tag = alloc_reply(tag, opcode);
Jens Axboe89c17072011-10-11 10:15:51 +0200414
Jens Axboe40c60512012-03-27 16:03:04 +0200415 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
Jens Axboe89c17072011-10-11 10:15:51 +0200416 if (ret) {
Jens Axboe40c60512012-03-27 16:03:04 +0200417 if (list)
418 free_reply(tag);
419
Jens Axboe89c17072011-10-11 10:15:51 +0200420 return ret;
421 }
422
Jens Axboe40c60512012-03-27 16:03:04 +0200423 if (list)
424 add_reply(tag, list);
425
Jens Axboe89c17072011-10-11 10:15:51 +0200426 return 0;
427}
428
Jens Axboe122c7722012-03-19 09:13:15 +0100429int fio_net_send_quit(int sk)
Jens Axboe437377e2011-10-01 08:31:30 -0600430{
Jens Axboe46c48f12011-10-01 12:50:51 -0600431 dprint(FD_NET, "server: sending quit\n");
Jens Axboe122c7722012-03-19 09:13:15 +0100432
Jens Axboe8c953072012-03-20 14:35:35 +0100433 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600434}
435
Jens Axboef58bd2a2012-03-27 10:06:42 +0200436static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error,
437 int signal)
Jens Axboe122c7722012-03-19 09:13:15 +0100438{
439 struct cmd_end_pdu epdu;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200440 uint64_t tag = 0;
Jens Axboe122c7722012-03-19 09:13:15 +0100441
Jens Axboef58bd2a2012-03-27 10:06:42 +0200442 if (cmd)
443 tag = cmd->tag;
Jens Axboe122c7722012-03-19 09:13:15 +0100444
445 epdu.error = __cpu_to_le32(error);
446 epdu.signal = __cpu_to_le32(signal);
Jens Axboe40c60512012-03-27 16:03:04 +0200447 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200448}
449
450int fio_net_send_stop(int sk, int error, int signal)
451{
452 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
453 return fio_net_send_ack(sk, NULL, error, signal);
Jens Axboe122c7722012-03-19 09:13:15 +0100454}
455
456static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
457{
458 struct fio_fork_item *ffi;
459
460 ffi = malloc(sizeof(*ffi));
461 ffi->exitval = 0;
462 ffi->signal = 0;
463 ffi->exited = 0;
464 ffi->pid = pid;
465 flist_add_tail(&ffi->list, list);
466}
467
Jens Axboe6348b5d2013-11-07 13:37:09 -0700468static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
Jens Axboe122c7722012-03-19 09:13:15 +0100469{
Jens Axboea7533db2013-11-06 11:19:42 -0700470 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
Jens Axboe6348b5d2013-11-07 13:37:09 -0700471 fio_server_add_fork_item(pid, conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100472}
473
Jens Axboe6348b5d2013-11-07 13:37:09 -0700474static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
Jens Axboe122c7722012-03-19 09:13:15 +0100475{
Jens Axboea7533db2013-11-06 11:19:42 -0700476 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
Jens Axboe6348b5d2013-11-07 13:37:09 -0700477 fio_server_add_fork_item(pid, job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100478}
479
480static void fio_server_check_fork_item(struct fio_fork_item *ffi)
481{
482 int ret, status;
483
484 ret = waitpid(ffi->pid, &status, WNOHANG);
485 if (ret < 0) {
486 if (errno == ECHILD) {
Jens Axboea7533db2013-11-06 11:19:42 -0700487 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
Jens Axboe122c7722012-03-19 09:13:15 +0100488 ffi->exited = 1;
489 } else
490 log_err("fio: waitpid: %s\n", strerror(errno));
491 } else if (ret == ffi->pid) {
492 if (WIFSIGNALED(status)) {
493 ffi->signal = WTERMSIG(status);
494 ffi->exited = 1;
495 }
496 if (WIFEXITED(status)) {
497 if (WEXITSTATUS(status))
498 ffi->exitval = WEXITSTATUS(status);
499 ffi->exited = 1;
500 }
501 }
502}
503
504static void fio_server_fork_item_done(struct fio_fork_item *ffi)
505{
Jens Axboea7533db2013-11-06 11:19:42 -0700506 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 +0100507
508 /*
509 * Fold STOP and QUIT...
510 */
511 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
512 fio_net_send_quit(server_fd);
513 flist_del(&ffi->list);
514 free(ffi);
515}
516
Jens Axboe54163252012-03-23 12:25:18 +0100517static void fio_server_check_fork_items(struct flist_head *list)
Jens Axboe122c7722012-03-19 09:13:15 +0100518{
519 struct flist_head *entry, *tmp;
520 struct fio_fork_item *ffi;
521
522 flist_for_each_safe(entry, tmp, list) {
523 ffi = flist_entry(entry, struct fio_fork_item, list);
524
525 fio_server_check_fork_item(ffi);
526
527 if (ffi->exited)
528 fio_server_fork_item_done(ffi);
529 }
530}
531
Jens Axboe6348b5d2013-11-07 13:37:09 -0700532static void fio_server_check_jobs(struct flist_head *job_list)
Jens Axboe122c7722012-03-19 09:13:15 +0100533{
Jens Axboe6348b5d2013-11-07 13:37:09 -0700534 fio_server_check_fork_items(job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100535}
536
Jens Axboe6348b5d2013-11-07 13:37:09 -0700537static void fio_server_check_conns(struct flist_head *conn_list)
Jens Axboe122c7722012-03-19 09:13:15 +0100538{
Jens Axboe6348b5d2013-11-07 13:37:09 -0700539 fio_server_check_fork_items(conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100540}
541
Jens Axboe6348b5d2013-11-07 13:37:09 -0700542static int handle_run_cmd(struct flist_head *job_list, struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600543{
Jens Axboe122c7722012-03-19 09:13:15 +0100544 pid_t pid;
Jens Axboea64e88d2011-10-03 14:20:01 +0200545 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600546
Jens Axboe122c7722012-03-19 09:13:15 +0100547 set_genesis_time();
548
549 pid = fork();
550 if (pid) {
Jens Axboe6348b5d2013-11-07 13:37:09 -0700551 fio_server_add_job_pid(job_list, pid);
Jens Axboe122c7722012-03-19 09:13:15 +0100552 return 0;
553 }
554
Jens Axboe2e1df072012-02-09 11:15:02 +0100555 ret = fio_backend();
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200556 free_threads_shm();
Jens Axboe122c7722012-03-19 09:13:15 +0100557 _exit(ret);
Jens Axboe81179ee2011-10-04 12:42:06 +0200558}
559
Jens Axboeb9d2f302012-03-08 20:36:28 +0100560static int handle_job_cmd(struct fio_net_cmd *cmd)
561{
Jens Axboe46bcd492012-03-14 11:31:21 +0100562 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
563 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100564 struct cmd_start_pdu spdu;
565
Jens Axboe46bcd492012-03-14 11:31:21 +0100566 pdu->buf_len = le32_to_cpu(pdu->buf_len);
567 pdu->client_type = le32_to_cpu(pdu->client_type);
568
569 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100570 fio_net_send_quit(server_fd);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100571 return -1;
572 }
573
574 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe108fea72012-11-14 13:09:45 -0700575 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200576 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100577 return 0;
578}
579
Jens Axboe81179ee2011-10-04 12:42:06 +0200580static int handle_jobline_cmd(struct fio_net_cmd *cmd)
581{
Jens Axboefa2ea802011-10-08 21:07:29 +0200582 void *pdu = cmd->payload;
583 struct cmd_single_line_pdu *cslp;
584 struct cmd_line_pdu *clp;
585 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100586 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200587 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100588 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200589
Jens Axboefa2ea802011-10-08 21:07:29 +0200590 clp = pdu;
591 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100592 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200593 argv = malloc(clp->lines * sizeof(char *));
594 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200595
Jens Axboefa2ea802011-10-08 21:07:29 +0200596 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200597
Jens Axboefa2ea802011-10-08 21:07:29 +0200598 for (i = 0; i < clp->lines; i++) {
599 cslp = pdu + offset;
600 argv[i] = (char *) cslp->text;
601
602 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200603 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
604 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200605
Jens Axboe46bcd492012-03-14 11:31:21 +0100606 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100607 fio_net_send_quit(server_fd);
Jens Axboefa2ea802011-10-08 21:07:29 +0200608 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200609 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200610 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200611
Jens Axboefa2ea802011-10-08 21:07:29 +0200612 free(argv);
613
Jens Axboeb9d2f302012-03-08 20:36:28 +0100614 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe1e5324e2012-11-14 14:25:31 -0700615 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200616 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100617 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600618}
619
Jens Axboec28e8e82011-10-04 08:57:39 +0200620static int handle_probe_cmd(struct fio_net_cmd *cmd)
621{
Jens Axboe3989b142013-04-12 13:13:24 +0200622 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
623 struct cmd_probe_reply_pdu probe;
Jens Axboe40c60512012-03-27 16:03:04 +0200624 uint64_t tag = cmd->tag;
Jens Axboec28e8e82011-10-04 08:57:39 +0200625
Jens Axboe89c17072011-10-11 10:15:51 +0200626 dprint(FD_NET, "server: sending probe reply\n");
627
Jens Axboec28e8e82011-10-04 08:57:39 +0200628 memset(&probe, 0, sizeof(probe));
629 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe0dcebdf2013-01-23 15:42:16 -0700630#ifdef CONFIG_BIG_ENDIAN
Jens Axboe6eb24792011-10-04 15:00:30 +0200631 probe.bigendian = 1;
632#endif
Jens Axboe750db472012-04-16 11:44:48 +0200633 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
Jens Axboec28e8e82011-10-04 08:57:39 +0200634
Jens Axboecca84642011-10-07 12:47:57 +0200635 probe.os = FIO_OS;
636 probe.arch = FIO_ARCH;
Jens Axboe38fdef22011-10-11 14:30:06 +0200637 probe.bpp = sizeof(void *);
Jens Axboed31e26d2012-03-28 21:38:24 +0200638 probe.cpus = __cpu_to_le32(cpus_online());
Jens Axboe3989b142013-04-12 13:13:24 +0200639
640 /*
641 * If the client supports compression and we do too, then enable it
642 */
643 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
644 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
645 use_zlib = 1;
646 } else {
647 probe.flags = 0;
648 use_zlib = 0;
649 }
Jens Axboe38fdef22011-10-11 14:30:06 +0200650
Jens Axboe40c60512012-03-27 16:03:04 +0200651 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200652}
653
654static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
655{
656 struct jobs_eta *je;
657 size_t size;
Jens Axboe40c60512012-03-27 16:03:04 +0200658 uint64_t tag = cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200659 int i;
660
Jens Axboeb814fb22011-10-12 09:20:34 +0200661 if (!thread_number)
662 return 0;
663
664 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200665 je = malloc(size);
666 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200667
668 if (!calc_thread_status(je, 1)) {
669 free(je);
670 return 0;
671 }
672
673 dprint(FD_NET, "server sending status\n");
674
675 je->nr_running = cpu_to_le32(je->nr_running);
676 je->nr_ramp = cpu_to_le32(je->nr_ramp);
677 je->nr_pending = cpu_to_le32(je->nr_pending);
Jens Axboe714e85f2013-04-15 10:21:56 +0200678 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200679 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200680
Jens Axboe298921d2012-09-24 18:47:01 +0200681 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100682 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
683 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
684 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
685 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboe16725bb2013-04-11 12:43:05 +0200686 je->rate[i] = cpu_to_le32(je->rate[i]);
687 je->iops[i] = cpu_to_le32(je->iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200688 }
689
Jens Axboec1970b92012-03-06 15:37:40 +0100690 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200691 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100692 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200693 je->is_pow2 = cpu_to_le32(je->is_pow2);
Jens Axboeed2c0a12013-04-11 12:55:16 +0200694 je->unit_base = cpu_to_le32(je->unit_base);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200695
Jens Axboe40c60512012-03-27 16:03:04 +0200696 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200697 free(je);
698 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200699}
700
Jens Axboe40c60512012-03-27 16:03:04 +0200701static int send_update_job_reply(int fd, uint64_t __tag, int error)
702{
703 uint64_t tag = __tag;
704 uint32_t pdu_error;
705
706 pdu_error = __cpu_to_le32(error);
707 return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL);
708}
709
Jens Axboef58bd2a2012-03-27 10:06:42 +0200710static int handle_update_job_cmd(struct fio_net_cmd *cmd)
711{
712 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
713 struct thread_data *td;
714 uint32_t tnumber;
715
716 tnumber = le32_to_cpu(pdu->thread_number);
717
718 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
719
Jens Axboe30ffacb2012-03-28 09:15:05 +0200720 if (!tnumber || tnumber > thread_number) {
Jens Axboe40c60512012-03-27 16:03:04 +0200721 send_update_job_reply(server_fd, cmd->tag, ENODEV);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200722 return 0;
723 }
724
Jens Axboe30ffacb2012-03-28 09:15:05 +0200725 td = &threads[tnumber - 1];
Jens Axboef58bd2a2012-03-27 10:06:42 +0200726 convert_thread_options_to_cpu(&td->o, &pdu->top);
Jens Axboe40c60512012-03-27 16:03:04 +0200727 send_update_job_reply(server_fd, cmd->tag, 0);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200728 return 0;
729}
730
Jens Axboe6348b5d2013-11-07 13:37:09 -0700731static int handle_command(struct flist_head *job_list, struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600732{
733 int ret;
734
Jens Axboe4b91ee82013-02-25 10:18:33 +0100735 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
736 fio_server_op(cmd->opcode), cmd->pdu_len,
737 (unsigned long long) cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600738
Jens Axboe132159a2011-09-30 15:01:32 -0600739 switch (cmd->opcode) {
740 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200741 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200742 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200743 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600744 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200745 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600746 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200747 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600748 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200749 case FIO_NET_CMD_JOBLINE:
750 ret = handle_jobline_cmd(cmd);
751 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200752 case FIO_NET_CMD_PROBE:
753 ret = handle_probe_cmd(cmd);
754 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200755 case FIO_NET_CMD_SEND_ETA:
756 ret = handle_send_eta_cmd(cmd);
757 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100758 case FIO_NET_CMD_RUN:
Jens Axboe6348b5d2013-11-07 13:37:09 -0700759 ret = handle_run_cmd(job_list, cmd);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100760 break;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200761 case FIO_NET_CMD_UPDATE_JOB:
762 ret = handle_update_job_cmd(cmd);
763 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600764 default:
Jens Axboe3c3ed072012-03-27 09:12:39 +0200765 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600766 ret = 1;
767 }
768
769 return ret;
770}
771
Jens Axboe122c7722012-03-19 09:13:15 +0100772static int handle_connection(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600773{
774 struct fio_net_cmd *cmd = NULL;
Jens Axboe6348b5d2013-11-07 13:37:09 -0700775 FLIST_HEAD(job_list);
Jens Axboe132159a2011-09-30 15:01:32 -0600776 int ret = 0;
777
Jens Axboe122c7722012-03-19 09:13:15 +0100778 reset_fio_state();
Jens Axboe122c7722012-03-19 09:13:15 +0100779 server_fd = sk;
780
Jens Axboe132159a2011-09-30 15:01:32 -0600781 /* read forever */
782 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200783 struct pollfd pfd = {
784 .fd = sk,
785 .events = POLLIN,
786 };
787
788 ret = 0;
789 do {
Jens Axboe54163252012-03-23 12:25:18 +0100790 int timeout = 1000;
791
792 if (!flist_empty(&job_list))
793 timeout = 100;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200794
Jens Axboe54163252012-03-23 12:25:18 +0100795 ret = poll(&pfd, 1, timeout);
Jens Axboee951bdc2011-10-05 21:58:45 +0200796 if (ret < 0) {
797 if (errno == EINTR)
798 break;
799 log_err("fio: poll: %s\n", strerror(errno));
800 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200801 } else if (!ret) {
Jens Axboe6348b5d2013-11-07 13:37:09 -0700802 fio_server_check_jobs(&job_list);
Jens Axboee951bdc2011-10-05 21:58:45 +0200803 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200804 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200805
806 if (pfd.revents & POLLIN)
807 break;
808 if (pfd.revents & (POLLERR|POLLHUP)) {
809 ret = 1;
810 break;
811 }
Jens Axboe19c65172011-10-05 22:05:37 +0200812 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200813
Jens Axboe6348b5d2013-11-07 13:37:09 -0700814 fio_server_check_jobs(&job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100815
Jens Axboee951bdc2011-10-05 21:58:45 +0200816 if (ret < 0)
817 break;
818
819 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600820 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200821 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600822 break;
823 }
824
Jens Axboe6348b5d2013-11-07 13:37:09 -0700825 ret = handle_command(&job_list, cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600826 if (ret)
827 break;
828
829 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400830 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600831 }
832
833 if (cmd)
834 free(cmd);
835
Jens Axboe122c7722012-03-19 09:13:15 +0100836 close(sk);
837 _exit(ret);
Jens Axboecc0df002011-10-03 20:53:32 +0200838}
839
Jens Axboe50d16972011-09-29 17:45:28 -0600840static int accept_loop(int listen_sk)
841{
Jens Axboebb447a22011-10-04 15:06:42 +0200842 struct sockaddr_in addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100843 socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600844 struct pollfd pfd;
Jens Axboe122c7722012-03-19 09:13:15 +0100845 int ret = 0, sk, flags, exitval = 0;
Jens Axboe6348b5d2013-11-07 13:37:09 -0700846 FLIST_HEAD(conn_list);
Jens Axboe50d16972011-09-29 17:45:28 -0600847
Jens Axboe60efd142011-10-04 13:30:11 +0200848 dprint(FD_NET, "server enter accept loop\n");
849
Jens Axboe009b1be2011-09-29 18:27:02 -0600850 flags = fcntl(listen_sk, F_GETFL);
851 flags |= O_NONBLOCK;
852 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe122c7722012-03-19 09:13:15 +0100853
854 while (!exit_backend) {
855 pid_t pid;
856
857 pfd.fd = listen_sk;
858 pfd.events = POLLIN;
859 do {
Jens Axboe54163252012-03-23 12:25:18 +0100860 int timeout = 1000;
861
862 if (!flist_empty(&conn_list))
863 timeout = 100;
864
865 ret = poll(&pfd, 1, timeout);
Jens Axboe122c7722012-03-19 09:13:15 +0100866 if (ret < 0) {
867 if (errno == EINTR)
868 break;
869 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600870 break;
Jens Axboe122c7722012-03-19 09:13:15 +0100871 } else if (!ret) {
Jens Axboe6348b5d2013-11-07 13:37:09 -0700872 fio_server_check_conns(&conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100873 continue;
874 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600875
Jens Axboe122c7722012-03-19 09:13:15 +0100876 if (pfd.revents & POLLIN)
877 break;
878 } while (!exit_backend);
879
Jens Axboe6348b5d2013-11-07 13:37:09 -0700880 fio_server_check_conns(&conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100881
882 if (exit_backend || ret < 0)
Jens Axboe009b1be2011-09-29 18:27:02 -0600883 break;
Jens Axboe009b1be2011-09-29 18:27:02 -0600884
Jens Axboe122c7722012-03-19 09:13:15 +0100885 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
886 if (sk < 0) {
887 log_err("fio: accept: %s\n", strerror(errno));
888 return -1;
889 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600890
Jens Axboe122c7722012-03-19 09:13:15 +0100891 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
892
893 pid = fork();
894 if (pid) {
895 close(sk);
Jens Axboe6348b5d2013-11-07 13:37:09 -0700896 fio_server_add_conn_pid(&conn_list, pid);
Jens Axboe122c7722012-03-19 09:13:15 +0100897 continue;
898 }
899
900 /* exits */
901 handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -0600902 }
903
Jens Axboe132159a2011-09-30 15:01:32 -0600904 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600905}
906
Jens Axboe084d1c62012-03-03 20:28:07 +0100907int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600908{
Jens Axboe084d1c62012-03-03 20:28:07 +0100909 struct cmd_text_pdu *pdu;
910 unsigned int tlen;
911 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600912
Jens Axboe084d1c62012-03-03 20:28:07 +0100913 if (server_fd == -1)
914 return log_local_buf(buf, len);
915
916 tlen = sizeof(*pdu) + len;
917 pdu = malloc(tlen);
918
919 pdu->level = __cpu_to_le32(level);
920 pdu->buf_len = __cpu_to_le32(len);
921
922 gettimeofday(&tv, NULL);
923 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
924 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
925
926 memcpy(pdu->buf, buf, len);
927
Jens Axboe40c60512012-03-27 16:03:04 +0200928 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL);
Jens Axboe084d1c62012-03-03 20:28:07 +0100929 free(pdu);
930 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600931}
932
Jens Axboea64e88d2011-10-03 14:20:01 +0200933static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
934{
935 dst->max_val = cpu_to_le64(src->max_val);
936 dst->min_val = cpu_to_le64(src->min_val);
937 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200938
939 /*
940 * Encode to IEEE 754 for network transfer
941 */
942 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
943 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200944}
945
946static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
947{
948 int i;
949
Jens Axboe298921d2012-09-24 18:47:01 +0200950 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200951 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
952 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
953 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
954 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
955 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
956 dst->agg[i] = cpu_to_le64(src->agg[i]);
957 }
958
959 dst->kb_base = cpu_to_le32(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700960 dst->unit_base = cpu_to_le32(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200961 dst->groupid = cpu_to_le32(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +0100962 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200963}
964
965/*
966 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
967 * into a single payload.
968 */
969void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
970{
971 struct cmd_ts_pdu p;
972 int i, j;
973
Jens Axboe60efd142011-10-04 13:30:11 +0200974 dprint(FD_NET, "server sending end stats\n");
975
Jens Axboe317b3c82011-10-03 21:47:27 +0200976 memset(&p, 0, sizeof(p));
977
Jens Axboea64e88d2011-10-03 14:20:01 +0200978 strcpy(p.ts.name, ts->name);
979 strcpy(p.ts.verror, ts->verror);
980 strcpy(p.ts.description, ts->description);
981
Jens Axboe2f122b12012-03-15 13:10:19 +0100982 p.ts.error = cpu_to_le32(ts->error);
983 p.ts.thread_number = cpu_to_le32(ts->thread_number);
984 p.ts.groupid = cpu_to_le32(ts->groupid);
985 p.ts.pid = cpu_to_le32(ts->pid);
986 p.ts.members = cpu_to_le32(ts->members);
Jens Axboe771e58b2013-01-30 12:56:23 +0100987 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200988
Jens Axboe298921d2012-09-24 18:47:01 +0200989 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200990 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
991 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
992 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
993 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
994 }
995
996 p.ts.usr_time = cpu_to_le64(ts->usr_time);
997 p.ts.sys_time = cpu_to_le64(ts->sys_time);
998 p.ts.ctx = cpu_to_le64(ts->ctx);
999 p.ts.minf = cpu_to_le64(ts->minf);
1000 p.ts.majf = cpu_to_le64(ts->majf);
1001 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +02001002
1003 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +02001004 fio_fp64_t *src = &ts->percentile_list[i];
1005 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +02001006
Jens Axboecfc03e42011-10-12 21:20:42 +02001007 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +02001008 }
Jens Axboea64e88d2011-10-03 14:20:01 +02001009
1010 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1011 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1012 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1013 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1014 }
1015
1016 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1017 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1018 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1019 }
1020
Jens Axboe298921d2012-09-24 18:47:01 +02001021 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +02001022 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1023 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1024
Jens Axboe78799de2013-01-29 20:53:52 +01001025 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001026 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +02001027 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +02001028 }
1029
Jens Axboe93eee042011-10-03 21:08:48 +02001030 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +02001031 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1032
Jens Axboe298921d2012-09-24 18:47:01 +02001033 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001034 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1035 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1036 }
1037
1038 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1039 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1040 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +02001041 p.ts.first_error = cpu_to_le32(ts->first_error);
1042 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -07001043 p.ts.unit_base = cpu_to_le32(ts->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +02001044
Jens Axboe3e260a42013-12-09 12:38:53 -07001045 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1046 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1047 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1048 p.ts.latency_percentile.u.i = __cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1049
Jens Axboea64e88d2011-10-03 14:20:01 +02001050 convert_gs(&p.rs, rs);
1051
Jens Axboe40c60512012-03-27 16:03:04 +02001052 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL);
Jens Axboea64e88d2011-10-03 14:20:01 +02001053}
1054
1055void fio_server_send_gs(struct group_run_stats *rs)
1056{
1057 struct group_run_stats gs;
1058
Jens Axboe60efd142011-10-04 13:30:11 +02001059 dprint(FD_NET, "server sending group run stats\n");
1060
Jens Axboea64e88d2011-10-03 14:20:01 +02001061 convert_gs(&gs, rs);
Jens Axboe40c60512012-03-27 16:03:04 +02001062 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL);
Jens Axboecf451d12011-10-03 16:48:30 +02001063}
1064
Jens Axboed09a64a2011-10-13 11:38:56 +02001065static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1066{
1067 int i;
1068
1069 for (i = 0; i < 2; i++) {
1070 dst->ios[i] = cpu_to_le32(src->ios[i]);
1071 dst->merges[i] = cpu_to_le32(src->merges[i]);
1072 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1073 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1074 }
1075
1076 dst->io_ticks = cpu_to_le32(src->io_ticks);
1077 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1078 dst->slavecount = cpu_to_le32(src->slavecount);
1079 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1080}
1081
1082static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1083{
1084 int i;
1085
1086 strcpy((char *) dst->name, (char *) src->name);
1087
1088 for (i = 0; i < 2; i++) {
1089 dst->ios[i] = cpu_to_le32(src->ios[i]);
1090 dst->merges[i] = cpu_to_le32(src->merges[i]);
1091 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1092 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1093 }
1094
1095 dst->io_ticks = cpu_to_le32(src->io_ticks);
1096 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1097 dst->msec = cpu_to_le64(src->msec);
1098}
1099
1100void fio_server_send_du(void)
1101{
1102 struct disk_util *du;
1103 struct flist_head *entry;
1104 struct cmd_du_pdu pdu;
1105
1106 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1107
Jens Axboe0766d922011-10-13 12:02:08 +02001108 memset(&pdu, 0, sizeof(pdu));
1109
Jens Axboed09a64a2011-10-13 11:38:56 +02001110 flist_for_each(entry, &disk_list) {
1111 du = flist_entry(entry, struct disk_util, list);
1112
1113 convert_dus(&pdu.dus, &du->dus);
1114 convert_agg(&pdu.agg, &du->agg);
1115
Jens Axboe40c60512012-03-27 16:03:04 +02001116 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboed09a64a2011-10-13 11:38:56 +02001117 }
1118}
1119
Jens Axboe53bd8db2012-03-14 21:51:38 +01001120/*
1121 * Send a command with a separate PDU, not inlined in the command
1122 */
1123static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1124 off_t size, uint64_t tag, uint32_t flags)
1125{
1126 struct fio_net_cmd cmd;
1127 struct iovec iov[2];
1128
1129 iov[0].iov_base = &cmd;
1130 iov[0].iov_len = sizeof(cmd);
1131 iov[1].iov_base = (void *) buf;
1132 iov[1].iov_len = size;
1133
1134 __fio_init_net_cmd(&cmd, opcode, size, tag);
1135 cmd.flags = __cpu_to_le32(flags);
1136 fio_net_cmd_crc_pdu(&cmd, buf);
1137
Jens Axboe8c953072012-03-20 14:35:35 +01001138 return fio_sendv_data(sk, iov, 2);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001139}
1140
Jens Axboe3989b142013-04-12 13:13:24 +02001141static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log)
Jens Axboe1b427252012-03-14 15:03:03 +01001142{
Jens Axboe3989b142013-04-12 13:13:24 +02001143 int ret = 0;
1144#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +01001145 z_stream stream;
1146 void *out_pdu;
Jens Axboe1b427252012-03-14 15:03:03 +01001147
1148 /*
1149 * Dirty - since the log is potentially huge, compress it into
1150 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1151 * side defragment it.
1152 */
1153 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1154
1155 stream.zalloc = Z_NULL;
1156 stream.zfree = Z_NULL;
1157 stream.opaque = Z_NULL;
1158
1159 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001160 ret = 1;
1161 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001162 }
1163
Jens Axboef5ed7652012-03-14 16:28:07 +01001164 stream.next_in = (void *) log->log;
1165 stream.avail_in = log->nr_samples * sizeof(struct io_sample);
Jens Axboe1b427252012-03-14 15:03:03 +01001166
1167 do {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001168 unsigned int this_len, flags = 0;
1169 int ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001170
1171 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1172 stream.next_out = out_pdu;
Jens Axboe3c547fe2012-03-14 21:53:00 +01001173 ret = deflate(&stream, Z_FINISH);
1174 /* may be Z_OK, or Z_STREAM_END */
1175 if (ret < 0)
1176 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001177
1178 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1179
Jens Axboe1b427252012-03-14 15:03:03 +01001180 if (stream.avail_in)
Jens Axboe53bd8db2012-03-14 21:51:38 +01001181 flags = FIO_NET_CMD_F_MORE;
Jens Axboe1b427252012-03-14 15:03:03 +01001182
Jens Axboe53bd8db2012-03-14 21:51:38 +01001183 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
1184 out_pdu, this_len, 0, flags);
1185 if (ret)
1186 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001187 } while (stream.avail_in);
1188
Jens Axboe53bd8db2012-03-14 21:51:38 +01001189err_zlib:
Jens Axboe1b427252012-03-14 15:03:03 +01001190 deflateEnd(&stream);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001191err:
1192 free(out_pdu);
Jens Axboe3989b142013-04-12 13:13:24 +02001193#endif
Jens Axboe53bd8db2012-03-14 21:51:38 +01001194 return ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001195}
1196
Jens Axboe3989b142013-04-12 13:13:24 +02001197int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1198{
1199 struct cmd_iolog_pdu pdu;
1200 int i, ret = 0;
1201
1202 pdu.thread_number = cpu_to_le32(td->thread_number);
1203 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
1204 pdu.log_type = cpu_to_le32(log->log_type);
1205 pdu.compressed = cpu_to_le32(use_zlib);
1206 strcpy((char *) pdu.name, name);
1207
1208 for (i = 0; i < log->nr_samples; i++) {
1209 struct io_sample *s = &log->log[i];
1210
1211 s->time = cpu_to_le64(s->time);
1212 s->val = cpu_to_le64(s->val);
1213 s->ddir = cpu_to_le32(s->ddir);
1214 s->bs = cpu_to_le32(s->bs);
1215 }
1216
1217 /*
1218 * Send header first, it's not compressed.
1219 */
1220 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1221 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1222 if (ret)
1223 return ret;
1224
1225 /*
1226 * Now send actual log, compress if we can, otherwise just plain
1227 */
1228 if (use_zlib)
1229 return fio_send_iolog_gz(&pdu, log);
1230
1231 return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log,
1232 log->nr_samples * sizeof(struct io_sample), 0, 0);
1233}
1234
Jens Axboe2f122b12012-03-15 13:10:19 +01001235void fio_server_send_add_job(struct thread_data *td)
Jens Axboe807f9972012-03-02 10:25:24 +01001236{
1237 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +01001238
Jens Axboe731e30a2012-03-14 16:35:37 +01001239 memset(&pdu, 0, sizeof(pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +01001240 pdu.thread_number = cpu_to_le32(td->thread_number);
1241 pdu.groupid = cpu_to_le32(td->groupid);
1242 convert_thread_options_to_net(&pdu.top, &td->o);
Jens Axboe807f9972012-03-02 10:25:24 +01001243
Jens Axboe40c60512012-03-27 16:03:04 +02001244 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboe807f9972012-03-02 10:25:24 +01001245}
1246
Jens Axboe122c7722012-03-19 09:13:15 +01001247void fio_server_send_start(struct thread_data *td)
1248{
1249 assert(server_fd != -1);
1250
1251 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1252}
1253
Jens Axboe87aa8f12011-10-06 21:24:13 +02001254static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +02001255{
Jens Axboe811826b2011-10-24 09:11:50 +02001256 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001257 socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001258 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +02001259
Jens Axboe811826b2011-10-24 09:11:50 +02001260 if (use_ipv6)
1261 sk = socket(AF_INET6, SOCK_STREAM, 0);
1262 else
1263 sk = socket(AF_INET, SOCK_STREAM, 0);
1264
Jens Axboe81179ee2011-10-04 12:42:06 +02001265 if (sk < 0) {
1266 log_err("fio: socket: %s\n", strerror(errno));
1267 return -1;
1268 }
1269
1270 opt = 1;
Jens Axboe1f819912013-01-23 17:21:41 -07001271 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001272 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001273 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001274 return -1;
1275 }
1276#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +02001277 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001278 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001279 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001280 return -1;
1281 }
1282#endif
1283
Jens Axboe811826b2011-10-24 09:11:50 +02001284 if (use_ipv6) {
1285 addr = (struct sockaddr *) &saddr_in6;
1286 socklen = sizeof(saddr_in6);
1287 saddr_in6.sin6_family = AF_INET6;
1288 } else {
1289 addr = (struct sockaddr *) &saddr_in;
1290 socklen = sizeof(saddr_in);
1291 saddr_in.sin_family = AF_INET;
1292 }
Jens Axboe81179ee2011-10-04 12:42:06 +02001293
Jens Axboe811826b2011-10-24 09:11:50 +02001294 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001295 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001296 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001297 return -1;
1298 }
1299
Jens Axboe87aa8f12011-10-06 21:24:13 +02001300 return sk;
1301}
1302
1303static int fio_init_server_sock(void)
1304{
1305 struct sockaddr_un addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001306 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001307 mode_t mode;
1308 int sk;
1309
1310 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1311 if (sk < 0) {
1312 log_err("fio: socket: %s\n", strerror(errno));
1313 return -1;
1314 }
1315
1316 mode = umask(000);
1317
1318 memset(&addr, 0, sizeof(addr));
1319 addr.sun_family = AF_UNIX;
1320 strcpy(addr.sun_path, bind_sock);
1321 unlink(bind_sock);
1322
1323 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1324
1325 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1326 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001327 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001328 return -1;
1329 }
1330
1331 umask(mode);
1332 return sk;
1333}
1334
1335static int fio_init_server_connection(void)
1336{
Jens Axboebebe6392011-10-07 10:00:51 +02001337 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001338 int sk;
1339
1340 dprint(FD_NET, "starting server\n");
1341
1342 if (!bind_sock)
1343 sk = fio_init_server_ip();
1344 else
1345 sk = fio_init_server_sock();
1346
1347 if (sk < 0)
1348 return sk;
1349
Jens Axboe811826b2011-10-24 09:11:50 +02001350 if (!bind_sock) {
1351 char *p, port[16];
1352 const void *src;
1353 int af;
1354
1355 if (use_ipv6) {
1356 af = AF_INET6;
1357 src = &saddr_in6.sin6_addr;
1358 } else {
1359 af = AF_INET;
1360 src = &saddr_in.sin_addr;
1361 }
1362
1363 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1364
1365 sprintf(port, ",%u", fio_net_port);
1366 if (p)
1367 strcat(p, port);
1368 else
1369 strcpy(bind_str, port);
1370 } else
Jens Axboebebe6392011-10-07 10:00:51 +02001371 strcpy(bind_str, bind_sock);
1372
1373 log_info("fio: server listening on %s\n", bind_str);
1374
Jens Axboe89c17072011-10-11 10:15:51 +02001375 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001376 log_err("fio: listen: %s\n", strerror(errno));
1377 return -1;
1378 }
1379
Jens Axboe87aa8f12011-10-06 21:24:13 +02001380 return sk;
1381}
1382
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001383int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1384 struct in6_addr *inp6)
1385
1386{
1387 int ret = 0;
1388
1389 if (*ipv6)
1390 ret = inet_pton(AF_INET6, host, inp6);
1391 else
1392 ret = inet_pton(AF_INET, host, inp);
1393
1394 if (ret != 1) {
1395 struct hostent *hent;
1396
1397 hent = gethostbyname(host);
1398 if (!hent) {
1399 log_err("fio: failed to resolve <%s>\n", host);
1400 return 0;
1401 }
1402
1403 if (*ipv6) {
1404 if (hent->h_addrtype != AF_INET6) {
1405 log_info("fio: falling back to IPv4\n");
1406 *ipv6 = 0;
1407 } else
1408 memcpy(inp6, hent->h_addr_list[0], 16);
1409 }
1410 if (!*ipv6) {
1411 if (hent->h_addrtype != AF_INET) {
1412 log_err("fio: lookup type mismatch\n");
1413 return 0;
1414 }
1415 memcpy(inp, hent->h_addr_list[0], 4);
1416 }
1417 ret = 1;
1418 }
1419
1420 return !(ret == 1);
1421}
1422
Jens Axboe660a2bf2011-10-24 09:35:06 +02001423/*
1424 * Parse a host/ip/port string. Reads from 'str'.
1425 *
1426 * Outputs:
1427 *
1428 * For IPv4:
1429 * *ptr is the host, *port is the port, inp is the destination.
1430 * For IPv6:
1431 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1432 * For local domain sockets:
1433 * *ptr is the filename, *is_sock is 1.
1434 */
Jens Axboebebe6392011-10-07 10:00:51 +02001435int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001436 int *port, struct in_addr *inp,
1437 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001438{
Jens Axboe76867622011-10-25 09:52:51 +02001439 const char *host = str;
1440 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001441 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001442
Jens Axboebebe6392011-10-07 10:00:51 +02001443 *ptr = NULL;
1444 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001445 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001446 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001447
1448 if (!strncmp(str, "sock:", 5)) {
1449 *ptr = strdup(str + 5);
1450 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001451
Jens Axboe76867622011-10-25 09:52:51 +02001452 return 0;
1453 }
1454
1455 /*
1456 * Is it ip:<ip or host>:port
1457 */
1458 if (!strncmp(host, "ip:", 3))
1459 host += 3;
1460 else if (!strncmp(host, "ip4:", 4))
1461 host += 4;
1462 else if (!strncmp(host, "ip6:", 4)) {
1463 host += 4;
1464 *ipv6 = 1;
1465 } else if (host[0] == ':') {
1466 /* String is :port */
1467 host++;
1468 lport = atoi(host);
1469 if (!lport || lport > 65535) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +02001470 log_err("fio: bad server port %u\n", lport);
Jens Axboe76867622011-10-25 09:52:51 +02001471 return 1;
1472 }
1473 /* no hostname given, we are done */
1474 *port = lport;
1475 return 0;
1476 }
1477
1478 /*
1479 * If no port seen yet, check if there's a last ':' at the end
1480 */
1481 if (!lport) {
1482 portp = strchr(host, ',');
1483 if (portp) {
1484 *portp = '\0';
1485 portp++;
1486 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001487 if (!lport || lport > 65535) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +02001488 log_err("fio: bad server port %u\n", lport);
Jens Axboebebe6392011-10-07 10:00:51 +02001489 return 1;
1490 }
Jens Axboe76867622011-10-25 09:52:51 +02001491 }
1492 }
1493
1494 if (lport)
1495 *port = lport;
1496
1497 if (!strlen(host))
1498 return 0;
1499
1500 *ptr = strdup(host);
1501
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001502 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1503 free(*ptr);
1504 *ptr = NULL;
1505 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001506 }
1507
1508 if (*port == 0)
1509 *port = fio_net_port;
1510
1511 return 0;
1512}
1513
Jens Axboe87aa8f12011-10-06 21:24:13 +02001514/*
1515 * Server arg should be one of:
1516 *
1517 * sock:/path/to/socket
1518 * ip:1.2.3.4
1519 * 1.2.3.4
1520 *
1521 * Where sock uses unix domain sockets, and ip binds the server to
1522 * a specific interface. If no arguments are given to the server, it
1523 * uses IP and binds to 0.0.0.0.
1524 *
1525 */
1526static int fio_handle_server_arg(void)
1527{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001528 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001529 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001530
Jens Axboe87aa8f12011-10-06 21:24:13 +02001531 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1532
1533 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001534 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001535
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001536 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001537 &port, &saddr_in.sin_addr,
1538 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001539
1540 if (!is_sock && bind_sock) {
1541 free(bind_sock);
1542 bind_sock = NULL;
1543 }
1544
Jens Axboea7de0a12011-10-07 12:55:14 +02001545out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001546 fio_net_port = port;
1547 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001548 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001549 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001550}
1551
1552static int fio_server(void)
1553{
1554 int sk, ret;
1555
1556 dprint(FD_NET, "starting server\n");
1557
1558 if (fio_handle_server_arg())
1559 return -1;
1560
1561 sk = fio_init_server_connection();
1562 if (sk < 0)
1563 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001564
1565 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001566
Jens Axboe81179ee2011-10-04 12:42:06 +02001567 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001568
1569 if (fio_server_arg) {
1570 free(fio_server_arg);
1571 fio_server_arg = NULL;
1572 }
Jens Axboebebe6392011-10-07 10:00:51 +02001573 if (bind_sock)
1574 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001575
Jens Axboe81179ee2011-10-04 12:42:06 +02001576 return ret;
1577}
1578
Jens Axboe7b821682011-10-11 12:16:32 +02001579void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001580{
Jens Axboe7b821682011-10-11 12:16:32 +02001581 if (signal == SIGPIPE)
1582 server_fd = -1;
1583 else {
1584 log_info("\nfio: terminating on signal %d\n", signal);
1585 exit_backend = 1;
1586 }
Jens Axboe9abea482011-10-04 13:21:54 +02001587}
1588
Jens Axboe13755d92011-10-10 19:51:26 +02001589static int check_existing_pidfile(const char *pidfile)
1590{
1591 struct stat sb;
1592 char buf[16];
1593 pid_t pid;
1594 FILE *f;
1595
1596 if (stat(pidfile, &sb))
1597 return 0;
1598
1599 f = fopen(pidfile, "r");
1600 if (!f)
1601 return 0;
1602
Jens Axboebfc3b172011-10-10 21:16:55 +02001603 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001604 fclose(f);
1605 return 1;
1606 }
1607 fclose(f);
1608
1609 pid = atoi(buf);
1610 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001611 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001612
1613 return 1;
1614}
1615
1616static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001617{
1618 FILE *fpid;
1619
1620 fpid = fopen(pidfile, "w");
1621 if (!fpid) {
1622 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001623 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001624 }
1625
1626 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001627 fclose(fpid);
1628 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001629}
1630
1631/*
1632 * If pidfile is specified, background us.
1633 */
1634int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001635{
1636 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001637 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001638
Bruce Cran93bcfd22012-02-20 20:18:19 +01001639#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001640 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +02001641 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001642#endif
1643
Jens Axboe402668f2011-10-10 15:28:58 +02001644 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001645 return fio_server();
1646
Jens Axboe13755d92011-10-10 19:51:26 +02001647 if (check_existing_pidfile(pidfile)) {
1648 log_err("fio: pidfile %s exists and server appears alive\n",
1649 pidfile);
1650 return -1;
1651 }
1652
Jens Axboee46d8092011-10-03 09:11:02 +02001653 pid = fork();
1654 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001655 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001656 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001657 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001658 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001659 int ret = write_pid(pid, pidfile);
1660
1661 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001662 }
Jens Axboee46d8092011-10-03 09:11:02 +02001663
1664 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001665 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1666 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001667 close(STDIN_FILENO);
1668 close(STDOUT_FILENO);
1669 close(STDERR_FILENO);
1670 f_out = NULL;
1671 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001672
1673 ret = fio_server();
1674
1675 closelog();
1676 unlink(pidfile);
1677 free(pidfile);
1678 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001679}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001680
Jens Axboebebe6392011-10-07 10:00:51 +02001681void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001682{
1683 fio_server_arg = strdup(arg);
1684}