blob: 274497514484f36a4b22b730bc0127316c6dc16e [file] [log] [blame]
Jens Axboe50d16972011-09-29 17:45:28 -06001#include <stdio.h>
2#include <stdlib.h>
Jens Axboe142575e2011-09-30 17:35:45 -06003#include <stdarg.h>
Jens Axboe50d16972011-09-29 17:45:28 -06004#include <unistd.h>
5#include <limits.h>
Jens Axboe50d16972011-09-29 17:45:28 -06006#include <errno.h>
7#include <fcntl.h>
8#include <sys/poll.h>
Jens Axboe50d16972011-09-29 17:45:28 -06009#include <sys/types.h>
10#include <sys/wait.h>
Jens Axboed05c4a02011-10-05 00:16:11 +020011#include <sys/socket.h>
Jens Axboe87aa8f12011-10-06 21:24:13 +020012#include <sys/stat.h>
13#include <sys/un.h>
Jens Axboe50d16972011-09-29 17:45:28 -060014#include <netinet/in.h>
15#include <arpa/inet.h>
16#include <netdb.h>
Jens Axboee46d8092011-10-03 09:11:02 +020017#include <syslog.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020018#include <signal.h>
Jens Axboe1b427252012-03-14 15:03:03 +010019#include <zlib.h>
Jens Axboe50d16972011-09-29 17:45:28 -060020
21#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060022#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060023#include "crc/crc16.h"
Jens Axboec7c6cb42011-10-13 14:12:40 +020024#include "lib/ieee754.h"
Jens Axboe50d16972011-09-29 17:45:28 -060025
Jens Axboe89cf1482011-10-07 10:10:18 +020026#include "fio_version.h"
27
Stephen M. Cameron5adc2442012-02-24 08:17:31 +010028int fio_net_port = FIO_NET_PORT;
Jens Axboe50d16972011-09-29 17:45:28 -060029
Jens Axboe009b1be2011-09-29 18:27:02 -060030int exit_backend = 0;
31
Jens Axboe46c48f12011-10-01 12:50:51 -060032static int server_fd = -1;
Jens Axboe87aa8f12011-10-06 21:24:13 +020033static char *fio_server_arg;
34static char *bind_sock;
35static struct sockaddr_in saddr_in;
Jens Axboe811826b2011-10-24 09:11:50 +020036static struct sockaddr_in6 saddr_in6;
Jens Axboe811826b2011-10-24 09:11:50 +020037static int use_ipv6;
Jens Axboe37db14f2011-09-30 17:00:42 -060038
Jens Axboe122c7722012-03-19 09:13:15 +010039struct fio_fork_item {
40 struct flist_head list;
41 int exitval;
42 int signal;
43 int exited;
44 pid_t pid;
45};
46
47/* Created on fork on new connection */
48static FLIST_HEAD(conn_list);
49
50/* Created on job fork from connection */
51static FLIST_HEAD(job_list);
52
Jens Axboe89c17072011-10-11 10:15:51 +020053static const char *fio_server_ops[FIO_NET_CMD_NR] = {
54 "",
55 "QUIT",
56 "EXIT",
57 "JOB",
58 "JOBLINE",
59 "TEXT",
60 "TS",
61 "GS",
62 "SEND_ETA",
63 "ETA",
64 "PROBE",
65 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020066 "STOP",
67 "DISK_UTIL",
Jens Axboeb9d2f302012-03-08 20:36:28 +010068 "SERVER_START",
Jens Axboe807f9972012-03-02 10:25:24 +010069 "ADD_JOB",
Jens Axboeb9d2f302012-03-08 20:36:28 +010070 "CMD_RUN"
Jens Axboec70e63e2012-03-15 08:26:18 +010071 "CMD_IOLOG",
Jens Axboe89c17072011-10-11 10:15:51 +020072};
73
74const char *fio_server_op(unsigned int op)
75{
76 static char buf[32];
77
78 if (op < FIO_NET_CMD_NR)
79 return fio_server_ops[op];
80
81 sprintf(buf, "UNKNOWN/%d", op);
82 return buf;
83}
84
Jens Axboe5235f622012-03-14 21:25:51 +010085static ssize_t iov_total_len(const struct iovec *iov, int count)
Jens Axboe132159a2011-09-30 15:01:32 -060086{
Jens Axboe5235f622012-03-14 21:25:51 +010087 ssize_t ret = 0;
88
89 while (count--) {
90 ret += iov->iov_len;
91 iov++;
92 }
93
94 return ret;
95}
96
97static int fio_sendv_data(int sk, struct iovec *iov, int count)
98{
99 ssize_t total_len = iov_total_len(iov, count);
100 ssize_t ret;
Jens Axboe794d69c2011-10-01 08:48:50 -0600101
Jens Axboe132159a2011-09-30 15:01:32 -0600102 do {
Jens Axboe5235f622012-03-14 21:25:51 +0100103 ret = writev(sk, iov, count);
Jens Axboe132159a2011-09-30 15:01:32 -0600104 if (ret > 0) {
Jens Axboe5235f622012-03-14 21:25:51 +0100105 total_len -= ret;
106 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600107 break;
Jens Axboe5235f622012-03-14 21:25:51 +0100108
109 while (ret) {
110 if (ret >= iov->iov_len) {
111 ret -= iov->iov_len;
112 iov++;
113 continue;
114 }
115 iov->iov_base += ret;
116 iov->iov_len -= ret;
117 ret = 0;
118 }
Jens Axboe132159a2011-09-30 15:01:32 -0600119 } else if (!ret)
120 break;
121 else if (errno == EAGAIN || errno == EINTR)
122 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200123 else
124 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600125 } while (!exit_backend);
126
Jens Axboe5235f622012-03-14 21:25:51 +0100127 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600128 return 0;
129
Jens Axboe8b4e61b2012-03-09 17:10:54 +0100130 if (errno)
131 return -errno;
132
Jens Axboe132159a2011-09-30 15:01:32 -0600133 return 1;
134}
135
Jens Axboe5235f622012-03-14 21:25:51 +0100136int fio_send_data(int sk, const void *p, unsigned int len)
137{
138 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
139
140 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
141
142 return fio_sendv_data(sk, &iov, 1);
143}
144
Jens Axboe132159a2011-09-30 15:01:32 -0600145int fio_recv_data(int sk, void *p, unsigned int len)
146{
147 do {
148 int ret = recv(sk, p, len, MSG_WAITALL);
149
150 if (ret > 0) {
151 len -= ret;
152 if (!len)
153 break;
154 p += ret;
155 continue;
156 } else if (!ret)
157 break;
158 else if (errno == EAGAIN || errno == EINTR)
159 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200160 else
161 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600162 } while (!exit_backend);
163
164 if (!len)
165 return 0;
166
167 return -1;
168}
169
170static int verify_convert_cmd(struct fio_net_cmd *cmd)
171{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600172 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600173
Jens Axboefcee5ff2011-09-30 22:50:39 -0600174 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
175 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600176
Jens Axboe25dfa842012-02-29 10:01:34 +0100177 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600178 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600179 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600180 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600181 return 1;
182 }
183
184 cmd->version = le16_to_cpu(cmd->version);
185 cmd->opcode = le16_to_cpu(cmd->opcode);
186 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200187 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600188 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
189
190 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200191 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600192 break;
193 default:
194 log_err("fio: bad server cmd version %d\n", cmd->version);
195 return 1;
196 }
197
Jens Axboeb9d2f302012-03-08 20:36:28 +0100198 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600199 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
200 return 1;
201 }
202
203 return 0;
204}
205
Jens Axboea64e88d2011-10-03 14:20:01 +0200206/*
207 * Read (and defragment, if necessary) incoming commands
208 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200209struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600210{
Jens Axboea64e88d2011-10-03 14:20:01 +0200211 struct fio_net_cmd cmd, *cmdret = NULL;
212 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600213 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200214 int ret, first = 1;
215 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600216
Jens Axboea64e88d2011-10-03 14:20:01 +0200217 do {
218 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
219 if (ret)
220 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600221
Jens Axboea64e88d2011-10-03 14:20:01 +0200222 /* We have a command, verify it and swap if need be */
223 ret = verify_convert_cmd(&cmd);
224 if (ret)
225 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600226
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200227 if (first) {
228 /* if this is text, add room for \0 at the end */
229 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
230 assert(!cmdret);
231 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200232 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600233
Jens Axboea64e88d2011-10-03 14:20:01 +0200234 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600235
Jens Axboea64e88d2011-10-03 14:20:01 +0200236 if (first)
237 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200238 else if (cmdret->opcode != cmd.opcode) {
239 log_err("fio: fragment opcode mismatch (%d != %d)\n",
240 cmdret->opcode, cmd.opcode);
241 ret = 1;
242 break;
243 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200244
245 if (!cmd.pdu_len)
246 break;
247
248 /* There's payload, get it */
249 pdu = (void *) cmdret->payload + pdu_offset;
250 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
251 if (ret)
252 break;
253
254 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100255 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200256 if (crc != cmd.pdu_crc16) {
257 log_err("fio: server bad crc on payload ");
258 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
259 ret = 1;
260 break;
261 }
262
263 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200264 if (!first)
265 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200266 first = 0;
267 } while (cmd.flags & FIO_NET_CMD_F_MORE);
268
269 if (ret) {
270 free(cmdret);
271 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200272 } else if (cmdret) {
273 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100274 if (cmdret->pdu_len) {
275 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
276 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
277 char *buf = (char *) pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200278
Jens Axboe084d1c62012-03-03 20:28:07 +0100279 buf[pdu->buf_len ] = '\0';
280 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
Jens Axboe46bcd492012-03-14 11:31:21 +0100281 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
282 char *buf = (char *) pdu->buf;
283 int len = le32_to_cpu(pdu->buf_len);
Jens Axboe084d1c62012-03-03 20:28:07 +0100284
Jens Axboe46bcd492012-03-14 11:31:21 +0100285 buf[len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100286 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200287 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100288
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200289 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200290 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200291 }
Jens Axboe132159a2011-09-30 15:01:32 -0600292
Jens Axboea64e88d2011-10-03 14:20:01 +0200293 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600294}
295
Jens Axboe53bd8db2012-03-14 21:51:38 +0100296void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
Jens Axboe132159a2011-09-30 15:01:32 -0600297{
298 uint32_t pdu_len;
299
Jens Axboe25dfa842012-02-29 10:01:34 +0100300 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600301
302 pdu_len = le32_to_cpu(cmd->pdu_len);
Jens Axboe1b427252012-03-14 15:03:03 +0100303 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
304}
305
306void fio_net_cmd_crc(struct fio_net_cmd *cmd)
307{
308 fio_net_cmd_crc_pdu(cmd, cmd->payload);
Jens Axboe132159a2011-09-30 15:01:32 -0600309}
310
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200311int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
312 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600313{
Jens Axboe7f868312011-10-10 09:55:21 +0200314 struct fio_net_cmd *cmd = NULL;
315 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600316 int ret;
317
318 do {
319 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100320 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
321 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600322
Jens Axboe7f868312011-10-10 09:55:21 +0200323 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
324 if (cmd)
325 free(cmd);
326
327 cur_len = sizeof(*cmd) + this_len;
328 cmd = malloc(cur_len);
329 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600330
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200331 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600332
333 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200334 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600335
336 fio_net_cmd_crc(cmd);
337
338 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600339 size -= this_len;
340 buf += this_len;
341 } while (!ret && size);
342
Jens Axboe7f868312011-10-10 09:55:21 +0200343 if (cmd)
344 free(cmd);
345
Jens Axboe794d69c2011-10-01 08:48:50 -0600346 return ret;
347}
348
Jens Axboe89c17072011-10-11 10:15:51 +0200349static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600350{
Jens Axboe178cde92011-10-05 22:14:31 +0200351 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600352
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200353 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600354 fio_net_cmd_crc(&cmd);
355
356 return fio_send_data(sk, &cmd, sizeof(cmd));
357}
358
Jens Axboe89c17072011-10-11 10:15:51 +0200359/*
360 * If 'list' is non-NULL, then allocate and store the sent command for
361 * later verification.
362 */
363int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
364 struct flist_head *list)
365{
366 struct fio_net_int_cmd *cmd;
367 int ret;
368
369 if (!list)
370 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
371
372 cmd = malloc(sizeof(*cmd));
373
Jens Axboedf380932011-10-11 14:25:08 +0200374 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
Jens Axboe89c17072011-10-11 10:15:51 +0200375 fio_net_cmd_crc(&cmd->cmd);
376
377 INIT_FLIST_HEAD(&cmd->list);
378 gettimeofday(&cmd->tv, NULL);
379 cmd->saved_tag = tag;
380
381 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
382 if (ret) {
383 free(cmd);
384 return ret;
385 }
386
387 flist_add_tail(&cmd->list, list);
388 return 0;
389}
390
Jens Axboe122c7722012-03-19 09:13:15 +0100391int fio_net_send_quit(int sk)
Jens Axboe437377e2011-10-01 08:31:30 -0600392{
Jens Axboe46c48f12011-10-01 12:50:51 -0600393 dprint(FD_NET, "server: sending quit\n");
Jens Axboe122c7722012-03-19 09:13:15 +0100394
Jens Axboe8c953072012-03-20 14:35:35 +0100395 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600396}
397
Jens Axboe122c7722012-03-19 09:13:15 +0100398int fio_net_send_stop(int sk, int error, int signal)
399{
400 struct cmd_end_pdu epdu;
401
402 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
403
404 epdu.error = __cpu_to_le32(error);
405 epdu.signal = __cpu_to_le32(signal);
Jens Axboe8c953072012-03-20 14:35:35 +0100406 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0);
Jens Axboe122c7722012-03-19 09:13:15 +0100407}
408
409static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
410{
411 struct fio_fork_item *ffi;
412
413 ffi = malloc(sizeof(*ffi));
414 ffi->exitval = 0;
415 ffi->signal = 0;
416 ffi->exited = 0;
417 ffi->pid = pid;
418 flist_add_tail(&ffi->list, list);
419}
420
421static void fio_server_add_conn_pid(pid_t pid)
422{
423 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", pid);
424 fio_server_add_fork_item(pid, &conn_list);
425}
426
427static void fio_server_add_job_pid(pid_t pid)
428{
429 dprint(FD_NET, "server: forked off job job (pid=%u)\n", pid);
430 fio_server_add_fork_item(pid, &job_list);
431}
432
433static void fio_server_check_fork_item(struct fio_fork_item *ffi)
434{
435 int ret, status;
436
437 ret = waitpid(ffi->pid, &status, WNOHANG);
438 if (ret < 0) {
439 if (errno == ECHILD) {
440 log_err("fio: connection pid %u disappeared\n", ffi->pid);
441 ffi->exited = 1;
442 } else
443 log_err("fio: waitpid: %s\n", strerror(errno));
444 } else if (ret == ffi->pid) {
445 if (WIFSIGNALED(status)) {
446 ffi->signal = WTERMSIG(status);
447 ffi->exited = 1;
448 }
449 if (WIFEXITED(status)) {
450 if (WEXITSTATUS(status))
451 ffi->exitval = WEXITSTATUS(status);
452 ffi->exited = 1;
453 }
454 }
455}
456
457static void fio_server_fork_item_done(struct fio_fork_item *ffi)
458{
459 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", ffi->pid, ffi->signal, ffi->exitval);
460
461 /*
462 * Fold STOP and QUIT...
463 */
464 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
465 fio_net_send_quit(server_fd);
466 flist_del(&ffi->list);
467 free(ffi);
468}
469
Jens Axboe54163252012-03-23 12:25:18 +0100470static void fio_server_check_fork_items(struct flist_head *list)
Jens Axboe122c7722012-03-19 09:13:15 +0100471{
472 struct flist_head *entry, *tmp;
473 struct fio_fork_item *ffi;
474
475 flist_for_each_safe(entry, tmp, list) {
476 ffi = flist_entry(entry, struct fio_fork_item, list);
477
478 fio_server_check_fork_item(ffi);
479
480 if (ffi->exited)
481 fio_server_fork_item_done(ffi);
482 }
483}
484
485static void fio_server_check_jobs(void)
486{
Jens Axboe54163252012-03-23 12:25:18 +0100487 fio_server_check_fork_items(&job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100488}
489
490static void fio_server_check_conns(void)
491{
Jens Axboe54163252012-03-23 12:25:18 +0100492 fio_server_check_fork_items(&conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100493}
494
Jens Axboeb9d2f302012-03-08 20:36:28 +0100495static int handle_run_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600496{
Jens Axboe122c7722012-03-19 09:13:15 +0100497 pid_t pid;
Jens Axboea64e88d2011-10-03 14:20:01 +0200498 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600499
Jens Axboe122c7722012-03-19 09:13:15 +0100500 set_genesis_time();
501
502 pid = fork();
503 if (pid) {
504 fio_server_add_job_pid(pid);
505 return 0;
506 }
507
Jens Axboe2e1df072012-02-09 11:15:02 +0100508 ret = fio_backend();
Jens Axboe122c7722012-03-19 09:13:15 +0100509 _exit(ret);
Jens Axboe81179ee2011-10-04 12:42:06 +0200510}
511
Jens Axboeb9d2f302012-03-08 20:36:28 +0100512static int handle_job_cmd(struct fio_net_cmd *cmd)
513{
Jens Axboe46bcd492012-03-14 11:31:21 +0100514 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
515 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100516 struct cmd_start_pdu spdu;
517
Jens Axboe46bcd492012-03-14 11:31:21 +0100518 pdu->buf_len = le32_to_cpu(pdu->buf_len);
519 pdu->client_type = le32_to_cpu(pdu->client_type);
520
521 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100522 fio_net_send_quit(server_fd);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100523 return -1;
524 }
525
526 spdu.jobs = cpu_to_le32(thread_number);
527 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
528 return 0;
529}
530
Jens Axboe81179ee2011-10-04 12:42:06 +0200531static int handle_jobline_cmd(struct fio_net_cmd *cmd)
532{
Jens Axboefa2ea802011-10-08 21:07:29 +0200533 void *pdu = cmd->payload;
534 struct cmd_single_line_pdu *cslp;
535 struct cmd_line_pdu *clp;
536 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100537 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200538 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100539 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200540
Jens Axboefa2ea802011-10-08 21:07:29 +0200541 clp = pdu;
542 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100543 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200544 argv = malloc(clp->lines * sizeof(char *));
545 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200546
Jens Axboefa2ea802011-10-08 21:07:29 +0200547 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200548
Jens Axboefa2ea802011-10-08 21:07:29 +0200549 for (i = 0; i < clp->lines; i++) {
550 cslp = pdu + offset;
551 argv[i] = (char *) cslp->text;
552
553 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200554 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
555 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200556
Jens Axboe46bcd492012-03-14 11:31:21 +0100557 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100558 fio_net_send_quit(server_fd);
Jens Axboefa2ea802011-10-08 21:07:29 +0200559 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200560 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200561 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200562
Jens Axboefa2ea802011-10-08 21:07:29 +0200563 free(argv);
564
Jens Axboeb9d2f302012-03-08 20:36:28 +0100565 spdu.jobs = cpu_to_le32(thread_number);
566 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
567 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600568}
569
Jens Axboec28e8e82011-10-04 08:57:39 +0200570static int handle_probe_cmd(struct fio_net_cmd *cmd)
571{
572 struct cmd_probe_pdu probe;
573
Jens Axboe89c17072011-10-11 10:15:51 +0200574 dprint(FD_NET, "server: sending probe reply\n");
575
Jens Axboec28e8e82011-10-04 08:57:39 +0200576 memset(&probe, 0, sizeof(probe));
577 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200578#ifdef FIO_BIG_ENDIAN
579 probe.bigendian = 1;
580#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200581 probe.fio_major = FIO_MAJOR;
582 probe.fio_minor = FIO_MINOR;
583 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200584
Jens Axboecca84642011-10-07 12:47:57 +0200585 probe.os = FIO_OS;
586 probe.arch = FIO_ARCH;
587
Jens Axboe38fdef22011-10-11 14:30:06 +0200588 probe.bpp = sizeof(void *);
589
Jens Axboe89c17072011-10-11 10:15:51 +0200590 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200591}
592
593static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
594{
595 struct jobs_eta *je;
596 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200597 int i;
598
Jens Axboeb814fb22011-10-12 09:20:34 +0200599 if (!thread_number)
600 return 0;
601
602 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200603 je = malloc(size);
604 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200605
606 if (!calc_thread_status(je, 1)) {
607 free(je);
608 return 0;
609 }
610
611 dprint(FD_NET, "server sending status\n");
612
613 je->nr_running = cpu_to_le32(je->nr_running);
614 je->nr_ramp = cpu_to_le32(je->nr_ramp);
615 je->nr_pending = cpu_to_le32(je->nr_pending);
616 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200617
618 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100619 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
620 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
621 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
622 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200623 je->rate[i] = cpu_to_le32(je->rate[i]);
624 je->iops[i] = cpu_to_le32(je->iops[i]);
625 }
626
Jens Axboec1970b92012-03-06 15:37:40 +0100627 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200628 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100629 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200630
Jens Axboe7f868312011-10-10 09:55:21 +0200631 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200632 free(je);
633 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200634}
635
Jens Axboe132159a2011-09-30 15:01:32 -0600636static int handle_command(struct fio_net_cmd *cmd)
637{
638 int ret;
639
Jens Axboe89c17072011-10-11 10:15:51 +0200640 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
641 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600642
Jens Axboe132159a2011-09-30 15:01:32 -0600643 switch (cmd->opcode) {
644 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200645 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200646 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200647 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600648 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200649 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600650 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200651 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600652 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200653 case FIO_NET_CMD_JOBLINE:
654 ret = handle_jobline_cmd(cmd);
655 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200656 case FIO_NET_CMD_PROBE:
657 ret = handle_probe_cmd(cmd);
658 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200659 case FIO_NET_CMD_SEND_ETA:
660 ret = handle_send_eta_cmd(cmd);
661 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100662 case FIO_NET_CMD_RUN:
663 ret = handle_run_cmd(cmd);
664 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600665 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200666 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600667 ret = 1;
668 }
669
670 return ret;
671}
672
Jens Axboe122c7722012-03-19 09:13:15 +0100673static int handle_connection(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600674{
675 struct fio_net_cmd *cmd = NULL;
676 int ret = 0;
677
Jens Axboe122c7722012-03-19 09:13:15 +0100678 reset_fio_state();
679 INIT_FLIST_HEAD(&job_list);
680 server_fd = sk;
681
Jens Axboe132159a2011-09-30 15:01:32 -0600682 /* read forever */
683 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200684 struct pollfd pfd = {
685 .fd = sk,
686 .events = POLLIN,
687 };
688
689 ret = 0;
690 do {
Jens Axboe54163252012-03-23 12:25:18 +0100691 int timeout = 1000;
692
693 if (!flist_empty(&job_list))
694 timeout = 100;
695
696 ret = poll(&pfd, 1, timeout);
Jens Axboee951bdc2011-10-05 21:58:45 +0200697 if (ret < 0) {
698 if (errno == EINTR)
699 break;
700 log_err("fio: poll: %s\n", strerror(errno));
701 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200702 } else if (!ret) {
Jens Axboe122c7722012-03-19 09:13:15 +0100703 fio_server_check_jobs();
Jens Axboee951bdc2011-10-05 21:58:45 +0200704 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200705 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200706
707 if (pfd.revents & POLLIN)
708 break;
709 if (pfd.revents & (POLLERR|POLLHUP)) {
710 ret = 1;
711 break;
712 }
Jens Axboe19c65172011-10-05 22:05:37 +0200713 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200714
Jens Axboe122c7722012-03-19 09:13:15 +0100715 fio_server_check_jobs();
716
Jens Axboee951bdc2011-10-05 21:58:45 +0200717 if (ret < 0)
718 break;
719
720 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600721 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200722 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600723 break;
724 }
725
Jens Axboe132159a2011-09-30 15:01:32 -0600726 ret = handle_command(cmd);
727 if (ret)
728 break;
729
730 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400731 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600732 }
733
734 if (cmd)
735 free(cmd);
736
Jens Axboe122c7722012-03-19 09:13:15 +0100737 close(sk);
738 _exit(ret);
Jens Axboecc0df002011-10-03 20:53:32 +0200739}
740
Jens Axboe50d16972011-09-29 17:45:28 -0600741static int accept_loop(int listen_sk)
742{
Jens Axboebb447a22011-10-04 15:06:42 +0200743 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200744 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600745 struct pollfd pfd;
Jens Axboe122c7722012-03-19 09:13:15 +0100746 int ret = 0, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600747
Jens Axboe60efd142011-10-04 13:30:11 +0200748 dprint(FD_NET, "server enter accept loop\n");
749
Jens Axboe009b1be2011-09-29 18:27:02 -0600750 flags = fcntl(listen_sk, F_GETFL);
751 flags |= O_NONBLOCK;
752 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe122c7722012-03-19 09:13:15 +0100753
754 while (!exit_backend) {
755 pid_t pid;
756
757 pfd.fd = listen_sk;
758 pfd.events = POLLIN;
759 do {
Jens Axboe54163252012-03-23 12:25:18 +0100760 int timeout = 1000;
761
762 if (!flist_empty(&conn_list))
763 timeout = 100;
764
765 ret = poll(&pfd, 1, timeout);
Jens Axboe122c7722012-03-19 09:13:15 +0100766 if (ret < 0) {
767 if (errno == EINTR)
768 break;
769 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600770 break;
Jens Axboe122c7722012-03-19 09:13:15 +0100771 } else if (!ret) {
772 fio_server_check_conns();
773 continue;
774 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600775
Jens Axboe122c7722012-03-19 09:13:15 +0100776 if (pfd.revents & POLLIN)
777 break;
778 } while (!exit_backend);
779
780 fio_server_check_conns();
781
782 if (exit_backend || ret < 0)
Jens Axboe009b1be2011-09-29 18:27:02 -0600783 break;
Jens Axboe009b1be2011-09-29 18:27:02 -0600784
Jens Axboe122c7722012-03-19 09:13:15 +0100785 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
786 if (sk < 0) {
787 log_err("fio: accept: %s\n", strerror(errno));
788 return -1;
789 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600790
Jens Axboe122c7722012-03-19 09:13:15 +0100791 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
792
793 pid = fork();
794 if (pid) {
795 close(sk);
796 fio_server_add_conn_pid(pid);
797 continue;
798 }
799
800 /* exits */
801 handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -0600802 }
803
Jens Axboe132159a2011-09-30 15:01:32 -0600804 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600805}
806
Jens Axboe084d1c62012-03-03 20:28:07 +0100807int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600808{
Jens Axboe084d1c62012-03-03 20:28:07 +0100809 struct cmd_text_pdu *pdu;
810 unsigned int tlen;
811 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600812
Jens Axboe084d1c62012-03-03 20:28:07 +0100813 if (server_fd == -1)
814 return log_local_buf(buf, len);
815
816 tlen = sizeof(*pdu) + len;
817 pdu = malloc(tlen);
818
819 pdu->level = __cpu_to_le32(level);
820 pdu->buf_len = __cpu_to_le32(len);
821
822 gettimeofday(&tv, NULL);
823 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
824 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
825
826 memcpy(pdu->buf, buf, len);
827
828 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, 0);
829 free(pdu);
830 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600831}
832
Jens Axboea64e88d2011-10-03 14:20:01 +0200833static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
834{
835 dst->max_val = cpu_to_le64(src->max_val);
836 dst->min_val = cpu_to_le64(src->min_val);
837 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200838
839 /*
840 * Encode to IEEE 754 for network transfer
841 */
842 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
843 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200844}
845
846static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
847{
848 int i;
849
850 for (i = 0; i < 2; i++) {
851 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
852 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
853 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
854 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
855 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
856 dst->agg[i] = cpu_to_le64(src->agg[i]);
857 }
858
859 dst->kb_base = cpu_to_le32(src->kb_base);
860 dst->groupid = cpu_to_le32(src->groupid);
861}
862
863/*
864 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
865 * into a single payload.
866 */
867void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
868{
869 struct cmd_ts_pdu p;
870 int i, j;
871
Jens Axboe60efd142011-10-04 13:30:11 +0200872 dprint(FD_NET, "server sending end stats\n");
873
Jens Axboe317b3c82011-10-03 21:47:27 +0200874 memset(&p, 0, sizeof(p));
875
Jens Axboea64e88d2011-10-03 14:20:01 +0200876 strcpy(p.ts.name, ts->name);
877 strcpy(p.ts.verror, ts->verror);
878 strcpy(p.ts.description, ts->description);
879
Jens Axboe2f122b12012-03-15 13:10:19 +0100880 p.ts.error = cpu_to_le32(ts->error);
881 p.ts.thread_number = cpu_to_le32(ts->thread_number);
882 p.ts.groupid = cpu_to_le32(ts->groupid);
883 p.ts.pid = cpu_to_le32(ts->pid);
884 p.ts.members = cpu_to_le32(ts->members);
Jens Axboea64e88d2011-10-03 14:20:01 +0200885
886 for (i = 0; i < 2; i++) {
887 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
888 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
889 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
890 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
891 }
892
893 p.ts.usr_time = cpu_to_le64(ts->usr_time);
894 p.ts.sys_time = cpu_to_le64(ts->sys_time);
895 p.ts.ctx = cpu_to_le64(ts->ctx);
896 p.ts.minf = cpu_to_le64(ts->minf);
897 p.ts.majf = cpu_to_le64(ts->majf);
898 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200899
900 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200901 fio_fp64_t *src = &ts->percentile_list[i];
902 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200903
Jens Axboecfc03e42011-10-12 21:20:42 +0200904 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200905 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200906
907 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
908 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
909 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
910 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
911 }
912
913 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
914 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
915 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
916 }
917
918 for (i = 0; i < 2; i++)
919 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
920 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
921
922 for (i = 0; i < 3; i++) {
923 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200924 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200925 }
926
Jens Axboe93eee042011-10-03 21:08:48 +0200927 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200928 p.ts.total_complete = cpu_to_le64(ts->total_complete);
929
930 for (i = 0; i < 2; i++) {
931 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
932 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
933 }
934
935 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
936 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
937 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200938 p.ts.first_error = cpu_to_le32(ts->first_error);
939 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200940
941 convert_gs(&p.rs, rs);
942
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200943 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200944}
945
946void fio_server_send_gs(struct group_run_stats *rs)
947{
948 struct group_run_stats gs;
949
Jens Axboe60efd142011-10-04 13:30:11 +0200950 dprint(FD_NET, "server sending group run stats\n");
951
Jens Axboea64e88d2011-10-03 14:20:01 +0200952 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200953 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200954}
955
Jens Axboed09a64a2011-10-13 11:38:56 +0200956static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
957{
958 int i;
959
960 for (i = 0; i < 2; i++) {
961 dst->ios[i] = cpu_to_le32(src->ios[i]);
962 dst->merges[i] = cpu_to_le32(src->merges[i]);
963 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
964 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
965 }
966
967 dst->io_ticks = cpu_to_le32(src->io_ticks);
968 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
969 dst->slavecount = cpu_to_le32(src->slavecount);
970 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
971}
972
973static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
974{
975 int i;
976
977 strcpy((char *) dst->name, (char *) src->name);
978
979 for (i = 0; i < 2; i++) {
980 dst->ios[i] = cpu_to_le32(src->ios[i]);
981 dst->merges[i] = cpu_to_le32(src->merges[i]);
982 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
983 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
984 }
985
986 dst->io_ticks = cpu_to_le32(src->io_ticks);
987 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
988 dst->msec = cpu_to_le64(src->msec);
989}
990
991void fio_server_send_du(void)
992{
993 struct disk_util *du;
994 struct flist_head *entry;
995 struct cmd_du_pdu pdu;
996
997 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
998
Jens Axboe0766d922011-10-13 12:02:08 +0200999 memset(&pdu, 0, sizeof(pdu));
1000
Jens Axboed09a64a2011-10-13 11:38:56 +02001001 flist_for_each(entry, &disk_list) {
1002 du = flist_entry(entry, struct disk_util, list);
1003
1004 convert_dus(&pdu.dus, &du->dus);
1005 convert_agg(&pdu.agg, &du->agg);
1006
1007 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
1008 }
1009}
1010
Jens Axboe53bd8db2012-03-14 21:51:38 +01001011/*
1012 * Send a command with a separate PDU, not inlined in the command
1013 */
1014static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1015 off_t size, uint64_t tag, uint32_t flags)
1016{
1017 struct fio_net_cmd cmd;
1018 struct iovec iov[2];
1019
1020 iov[0].iov_base = &cmd;
1021 iov[0].iov_len = sizeof(cmd);
1022 iov[1].iov_base = (void *) buf;
1023 iov[1].iov_len = size;
1024
1025 __fio_init_net_cmd(&cmd, opcode, size, tag);
1026 cmd.flags = __cpu_to_le32(flags);
1027 fio_net_cmd_crc_pdu(&cmd, buf);
1028
Jens Axboe8c953072012-03-20 14:35:35 +01001029 return fio_sendv_data(sk, iov, 2);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001030}
1031
Jens Axboe1b427252012-03-14 15:03:03 +01001032int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1033{
Jens Axboef5ed7652012-03-14 16:28:07 +01001034 struct cmd_iolog_pdu pdu;
Jens Axboe1b427252012-03-14 15:03:03 +01001035 z_stream stream;
1036 void *out_pdu;
Jens Axboe53bd8db2012-03-14 21:51:38 +01001037 int i, ret = 0;
Jens Axboe1b427252012-03-14 15:03:03 +01001038
Jens Axboe2f122b12012-03-15 13:10:19 +01001039 pdu.thread_number = cpu_to_le32(td->thread_number);
Jens Axboef5ed7652012-03-14 16:28:07 +01001040 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
1041 pdu.log_type = cpu_to_le32(log->log_type);
1042 strcpy((char *) pdu.name, name);
Jens Axboe1b427252012-03-14 15:03:03 +01001043
1044 for (i = 0; i < log->nr_samples; i++) {
Jens Axboef5ed7652012-03-14 16:28:07 +01001045 struct io_sample *s = &log->log[i];
Jens Axboe1b427252012-03-14 15:03:03 +01001046
Jens Axboef5ed7652012-03-14 16:28:07 +01001047 s->time = cpu_to_le64(s->time);
1048 s->val = cpu_to_le64(s->val);
1049 s->ddir = cpu_to_le32(s->ddir);
1050 s->bs = cpu_to_le32(s->bs);
Jens Axboe1b427252012-03-14 15:03:03 +01001051 }
1052
1053 /*
1054 * Dirty - since the log is potentially huge, compress it into
1055 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1056 * side defragment it.
1057 */
1058 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1059
1060 stream.zalloc = Z_NULL;
1061 stream.zfree = Z_NULL;
1062 stream.opaque = Z_NULL;
1063
1064 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001065 ret = 1;
1066 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001067 }
1068
1069 /*
Jens Axboef5ed7652012-03-14 16:28:07 +01001070 * Send header first, it's not compressed.
Jens Axboe1b427252012-03-14 15:03:03 +01001071 */
Jens Axboe53bd8db2012-03-14 21:51:38 +01001072 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1073 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1074 if (ret)
1075 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001076
Jens Axboef5ed7652012-03-14 16:28:07 +01001077 stream.next_in = (void *) log->log;
1078 stream.avail_in = log->nr_samples * sizeof(struct io_sample);
Jens Axboe1b427252012-03-14 15:03:03 +01001079
1080 do {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001081 unsigned int this_len, flags = 0;
1082 int ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001083
1084 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1085 stream.next_out = out_pdu;
Jens Axboe3c547fe2012-03-14 21:53:00 +01001086 ret = deflate(&stream, Z_FINISH);
1087 /* may be Z_OK, or Z_STREAM_END */
1088 if (ret < 0)
1089 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001090
1091 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1092
Jens Axboe1b427252012-03-14 15:03:03 +01001093 if (stream.avail_in)
Jens Axboe53bd8db2012-03-14 21:51:38 +01001094 flags = FIO_NET_CMD_F_MORE;
Jens Axboe1b427252012-03-14 15:03:03 +01001095
Jens Axboe53bd8db2012-03-14 21:51:38 +01001096 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
1097 out_pdu, this_len, 0, flags);
1098 if (ret)
1099 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001100 } while (stream.avail_in);
1101
Jens Axboe53bd8db2012-03-14 21:51:38 +01001102err_zlib:
Jens Axboe1b427252012-03-14 15:03:03 +01001103 deflateEnd(&stream);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001104err:
1105 free(out_pdu);
1106 return ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001107}
1108
Jens Axboe2f122b12012-03-15 13:10:19 +01001109void fio_server_send_add_job(struct thread_data *td)
Jens Axboe807f9972012-03-02 10:25:24 +01001110{
1111 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +01001112
Jens Axboe731e30a2012-03-14 16:35:37 +01001113 memset(&pdu, 0, sizeof(pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +01001114 pdu.thread_number = cpu_to_le32(td->thread_number);
1115 pdu.groupid = cpu_to_le32(td->groupid);
1116 convert_thread_options_to_net(&pdu.top, &td->o);
Jens Axboe807f9972012-03-02 10:25:24 +01001117
1118 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), 0);
1119}
1120
Jens Axboe122c7722012-03-19 09:13:15 +01001121void fio_server_send_start(struct thread_data *td)
1122{
1123 assert(server_fd != -1);
1124
1125 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1126}
1127
Jens Axboe87aa8f12011-10-06 21:24:13 +02001128static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +02001129{
Jens Axboe811826b2011-10-24 09:11:50 +02001130 struct sockaddr *addr;
1131 fio_socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001132 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +02001133
Jens Axboe811826b2011-10-24 09:11:50 +02001134 if (use_ipv6)
1135 sk = socket(AF_INET6, SOCK_STREAM, 0);
1136 else
1137 sk = socket(AF_INET, SOCK_STREAM, 0);
1138
Jens Axboe81179ee2011-10-04 12:42:06 +02001139 if (sk < 0) {
1140 log_err("fio: socket: %s\n", strerror(errno));
1141 return -1;
1142 }
1143
1144 opt = 1;
1145 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1146 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001147 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001148 return -1;
1149 }
1150#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +02001151 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001152 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001153 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001154 return -1;
1155 }
1156#endif
1157
Jens Axboe811826b2011-10-24 09:11:50 +02001158 if (use_ipv6) {
1159 addr = (struct sockaddr *) &saddr_in6;
1160 socklen = sizeof(saddr_in6);
1161 saddr_in6.sin6_family = AF_INET6;
1162 } else {
1163 addr = (struct sockaddr *) &saddr_in;
1164 socklen = sizeof(saddr_in);
1165 saddr_in.sin_family = AF_INET;
1166 }
Jens Axboe81179ee2011-10-04 12:42:06 +02001167
Jens Axboe811826b2011-10-24 09:11:50 +02001168 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001169 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001170 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001171 return -1;
1172 }
1173
Jens Axboe87aa8f12011-10-06 21:24:13 +02001174 return sk;
1175}
1176
1177static int fio_init_server_sock(void)
1178{
1179 struct sockaddr_un addr;
1180 fio_socklen_t len;
1181 mode_t mode;
1182 int sk;
1183
1184 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1185 if (sk < 0) {
1186 log_err("fio: socket: %s\n", strerror(errno));
1187 return -1;
1188 }
1189
1190 mode = umask(000);
1191
1192 memset(&addr, 0, sizeof(addr));
1193 addr.sun_family = AF_UNIX;
1194 strcpy(addr.sun_path, bind_sock);
1195 unlink(bind_sock);
1196
1197 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1198
1199 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1200 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001201 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001202 return -1;
1203 }
1204
1205 umask(mode);
1206 return sk;
1207}
1208
1209static int fio_init_server_connection(void)
1210{
Jens Axboebebe6392011-10-07 10:00:51 +02001211 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001212 int sk;
1213
1214 dprint(FD_NET, "starting server\n");
1215
1216 if (!bind_sock)
1217 sk = fio_init_server_ip();
1218 else
1219 sk = fio_init_server_sock();
1220
1221 if (sk < 0)
1222 return sk;
1223
Jens Axboe811826b2011-10-24 09:11:50 +02001224 if (!bind_sock) {
1225 char *p, port[16];
1226 const void *src;
1227 int af;
1228
1229 if (use_ipv6) {
1230 af = AF_INET6;
1231 src = &saddr_in6.sin6_addr;
1232 } else {
1233 af = AF_INET;
1234 src = &saddr_in.sin_addr;
1235 }
1236
1237 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1238
1239 sprintf(port, ",%u", fio_net_port);
1240 if (p)
1241 strcat(p, port);
1242 else
1243 strcpy(bind_str, port);
1244 } else
Jens Axboebebe6392011-10-07 10:00:51 +02001245 strcpy(bind_str, bind_sock);
1246
1247 log_info("fio: server listening on %s\n", bind_str);
1248
Jens Axboe89c17072011-10-11 10:15:51 +02001249 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001250 log_err("fio: listen: %s\n", strerror(errno));
1251 return -1;
1252 }
1253
Jens Axboe87aa8f12011-10-06 21:24:13 +02001254 return sk;
1255}
1256
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001257int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1258 struct in6_addr *inp6)
1259
1260{
1261 int ret = 0;
1262
1263 if (*ipv6)
1264 ret = inet_pton(AF_INET6, host, inp6);
1265 else
1266 ret = inet_pton(AF_INET, host, inp);
1267
1268 if (ret != 1) {
1269 struct hostent *hent;
1270
1271 hent = gethostbyname(host);
1272 if (!hent) {
1273 log_err("fio: failed to resolve <%s>\n", host);
1274 return 0;
1275 }
1276
1277 if (*ipv6) {
1278 if (hent->h_addrtype != AF_INET6) {
1279 log_info("fio: falling back to IPv4\n");
1280 *ipv6 = 0;
1281 } else
1282 memcpy(inp6, hent->h_addr_list[0], 16);
1283 }
1284 if (!*ipv6) {
1285 if (hent->h_addrtype != AF_INET) {
1286 log_err("fio: lookup type mismatch\n");
1287 return 0;
1288 }
1289 memcpy(inp, hent->h_addr_list[0], 4);
1290 }
1291 ret = 1;
1292 }
1293
1294 return !(ret == 1);
1295}
1296
Jens Axboe660a2bf2011-10-24 09:35:06 +02001297/*
1298 * Parse a host/ip/port string. Reads from 'str'.
1299 *
1300 * Outputs:
1301 *
1302 * For IPv4:
1303 * *ptr is the host, *port is the port, inp is the destination.
1304 * For IPv6:
1305 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1306 * For local domain sockets:
1307 * *ptr is the filename, *is_sock is 1.
1308 */
Jens Axboebebe6392011-10-07 10:00:51 +02001309int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001310 int *port, struct in_addr *inp,
1311 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001312{
Jens Axboe76867622011-10-25 09:52:51 +02001313 const char *host = str;
1314 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001315 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001316
Jens Axboebebe6392011-10-07 10:00:51 +02001317 *ptr = NULL;
1318 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001319 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001320 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001321
1322 if (!strncmp(str, "sock:", 5)) {
1323 *ptr = strdup(str + 5);
1324 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001325
Jens Axboe76867622011-10-25 09:52:51 +02001326 return 0;
1327 }
1328
1329 /*
1330 * Is it ip:<ip or host>:port
1331 */
1332 if (!strncmp(host, "ip:", 3))
1333 host += 3;
1334 else if (!strncmp(host, "ip4:", 4))
1335 host += 4;
1336 else if (!strncmp(host, "ip6:", 4)) {
1337 host += 4;
1338 *ipv6 = 1;
1339 } else if (host[0] == ':') {
1340 /* String is :port */
1341 host++;
1342 lport = atoi(host);
1343 if (!lport || lport > 65535) {
1344 log_err("fio: bad server port %u\n", port);
1345 return 1;
1346 }
1347 /* no hostname given, we are done */
1348 *port = lport;
1349 return 0;
1350 }
1351
1352 /*
1353 * If no port seen yet, check if there's a last ':' at the end
1354 */
1355 if (!lport) {
1356 portp = strchr(host, ',');
1357 if (portp) {
1358 *portp = '\0';
1359 portp++;
1360 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001361 if (!lport || lport > 65535) {
1362 log_err("fio: bad server port %u\n", port);
1363 return 1;
1364 }
Jens Axboe76867622011-10-25 09:52:51 +02001365 }
1366 }
1367
1368 if (lport)
1369 *port = lport;
1370
1371 if (!strlen(host))
1372 return 0;
1373
1374 *ptr = strdup(host);
1375
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001376 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1377 free(*ptr);
1378 *ptr = NULL;
1379 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001380 }
1381
1382 if (*port == 0)
1383 *port = fio_net_port;
1384
1385 return 0;
1386}
1387
Jens Axboe87aa8f12011-10-06 21:24:13 +02001388/*
1389 * Server arg should be one of:
1390 *
1391 * sock:/path/to/socket
1392 * ip:1.2.3.4
1393 * 1.2.3.4
1394 *
1395 * Where sock uses unix domain sockets, and ip binds the server to
1396 * a specific interface. If no arguments are given to the server, it
1397 * uses IP and binds to 0.0.0.0.
1398 *
1399 */
1400static int fio_handle_server_arg(void)
1401{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001402 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001403 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001404
Jens Axboe87aa8f12011-10-06 21:24:13 +02001405 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1406
1407 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001408 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001409
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001410 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001411 &port, &saddr_in.sin_addr,
1412 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001413
1414 if (!is_sock && bind_sock) {
1415 free(bind_sock);
1416 bind_sock = NULL;
1417 }
1418
Jens Axboea7de0a12011-10-07 12:55:14 +02001419out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001420 fio_net_port = port;
1421 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001422 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001423 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001424}
1425
1426static int fio_server(void)
1427{
1428 int sk, ret;
1429
1430 dprint(FD_NET, "starting server\n");
1431
1432 if (fio_handle_server_arg())
1433 return -1;
1434
1435 sk = fio_init_server_connection();
1436 if (sk < 0)
1437 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001438
1439 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001440
Jens Axboe81179ee2011-10-04 12:42:06 +02001441 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001442
1443 if (fio_server_arg) {
1444 free(fio_server_arg);
1445 fio_server_arg = NULL;
1446 }
Jens Axboebebe6392011-10-07 10:00:51 +02001447 if (bind_sock)
1448 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001449
Jens Axboe81179ee2011-10-04 12:42:06 +02001450 return ret;
1451}
1452
Jens Axboe7b821682011-10-11 12:16:32 +02001453void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001454{
Jens Axboe7b821682011-10-11 12:16:32 +02001455 if (signal == SIGPIPE)
1456 server_fd = -1;
1457 else {
1458 log_info("\nfio: terminating on signal %d\n", signal);
1459 exit_backend = 1;
1460 }
Jens Axboe9abea482011-10-04 13:21:54 +02001461}
1462
Jens Axboe13755d92011-10-10 19:51:26 +02001463static int check_existing_pidfile(const char *pidfile)
1464{
1465 struct stat sb;
1466 char buf[16];
1467 pid_t pid;
1468 FILE *f;
1469
1470 if (stat(pidfile, &sb))
1471 return 0;
1472
1473 f = fopen(pidfile, "r");
1474 if (!f)
1475 return 0;
1476
Jens Axboebfc3b172011-10-10 21:16:55 +02001477 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001478 fclose(f);
1479 return 1;
1480 }
1481 fclose(f);
1482
1483 pid = atoi(buf);
1484 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001485 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001486
1487 return 1;
1488}
1489
1490static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001491{
1492 FILE *fpid;
1493
1494 fpid = fopen(pidfile, "w");
1495 if (!fpid) {
1496 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001497 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001498 }
1499
1500 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001501 fclose(fpid);
1502 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001503}
1504
1505/*
1506 * If pidfile is specified, background us.
1507 */
1508int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001509{
1510 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001511 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001512
Bruce Cran93bcfd22012-02-20 20:18:19 +01001513#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001514 WSADATA wsd;
1515 WSAStartup(MAKEWORD(2,2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001516#endif
1517
Jens Axboe402668f2011-10-10 15:28:58 +02001518 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001519 return fio_server();
1520
Jens Axboe13755d92011-10-10 19:51:26 +02001521 if (check_existing_pidfile(pidfile)) {
1522 log_err("fio: pidfile %s exists and server appears alive\n",
1523 pidfile);
1524 return -1;
1525 }
1526
Jens Axboee46d8092011-10-03 09:11:02 +02001527 pid = fork();
1528 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001529 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001530 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001531 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001532 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001533 int ret = write_pid(pid, pidfile);
1534
1535 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001536 }
Jens Axboee46d8092011-10-03 09:11:02 +02001537
1538 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001539 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1540 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001541 close(STDIN_FILENO);
1542 close(STDOUT_FILENO);
1543 close(STDERR_FILENO);
1544 f_out = NULL;
1545 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001546
1547 ret = fio_server();
1548
1549 closelog();
1550 unlink(pidfile);
1551 free(pidfile);
1552 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001553}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001554
Jens Axboebebe6392011-10-07 10:00:51 +02001555void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001556{
1557 fio_server_arg = strdup(arg);
1558}