blob: 6e736bba3850099d4910ebcee07ffb9cc22742f0 [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 Axboe3c3ed072012-03-27 09:12:39 +0200279 buf[pdu->buf_len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100280 } 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 Axboef58bd2a2012-03-27 10:06:42 +0200398static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error,
399 int signal)
Jens Axboe122c7722012-03-19 09:13:15 +0100400{
401 struct cmd_end_pdu epdu;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200402 uint64_t tag = 0;
Jens Axboe122c7722012-03-19 09:13:15 +0100403
Jens Axboef58bd2a2012-03-27 10:06:42 +0200404 if (cmd)
405 tag = cmd->tag;
Jens Axboe122c7722012-03-19 09:13:15 +0100406
407 epdu.error = __cpu_to_le32(error);
408 epdu.signal = __cpu_to_le32(signal);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200409 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), tag);
410}
411
412int fio_net_send_stop(int sk, int error, int signal)
413{
414 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
415 return fio_net_send_ack(sk, NULL, error, signal);
Jens Axboe122c7722012-03-19 09:13:15 +0100416}
417
418static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
419{
420 struct fio_fork_item *ffi;
421
422 ffi = malloc(sizeof(*ffi));
423 ffi->exitval = 0;
424 ffi->signal = 0;
425 ffi->exited = 0;
426 ffi->pid = pid;
427 flist_add_tail(&ffi->list, list);
428}
429
430static void fio_server_add_conn_pid(pid_t pid)
431{
432 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", pid);
433 fio_server_add_fork_item(pid, &conn_list);
434}
435
436static void fio_server_add_job_pid(pid_t pid)
437{
438 dprint(FD_NET, "server: forked off job job (pid=%u)\n", pid);
439 fio_server_add_fork_item(pid, &job_list);
440}
441
442static void fio_server_check_fork_item(struct fio_fork_item *ffi)
443{
444 int ret, status;
445
446 ret = waitpid(ffi->pid, &status, WNOHANG);
447 if (ret < 0) {
448 if (errno == ECHILD) {
449 log_err("fio: connection pid %u disappeared\n", ffi->pid);
450 ffi->exited = 1;
451 } else
452 log_err("fio: waitpid: %s\n", strerror(errno));
453 } else if (ret == ffi->pid) {
454 if (WIFSIGNALED(status)) {
455 ffi->signal = WTERMSIG(status);
456 ffi->exited = 1;
457 }
458 if (WIFEXITED(status)) {
459 if (WEXITSTATUS(status))
460 ffi->exitval = WEXITSTATUS(status);
461 ffi->exited = 1;
462 }
463 }
464}
465
466static void fio_server_fork_item_done(struct fio_fork_item *ffi)
467{
468 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", ffi->pid, ffi->signal, ffi->exitval);
469
470 /*
471 * Fold STOP and QUIT...
472 */
473 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
474 fio_net_send_quit(server_fd);
475 flist_del(&ffi->list);
476 free(ffi);
477}
478
Jens Axboe54163252012-03-23 12:25:18 +0100479static void fio_server_check_fork_items(struct flist_head *list)
Jens Axboe122c7722012-03-19 09:13:15 +0100480{
481 struct flist_head *entry, *tmp;
482 struct fio_fork_item *ffi;
483
484 flist_for_each_safe(entry, tmp, list) {
485 ffi = flist_entry(entry, struct fio_fork_item, list);
486
487 fio_server_check_fork_item(ffi);
488
489 if (ffi->exited)
490 fio_server_fork_item_done(ffi);
491 }
492}
493
494static void fio_server_check_jobs(void)
495{
Jens Axboe54163252012-03-23 12:25:18 +0100496 fio_server_check_fork_items(&job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100497}
498
499static void fio_server_check_conns(void)
500{
Jens Axboe54163252012-03-23 12:25:18 +0100501 fio_server_check_fork_items(&conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100502}
503
Jens Axboeb9d2f302012-03-08 20:36:28 +0100504static int handle_run_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600505{
Jens Axboe122c7722012-03-19 09:13:15 +0100506 pid_t pid;
Jens Axboea64e88d2011-10-03 14:20:01 +0200507 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600508
Jens Axboe122c7722012-03-19 09:13:15 +0100509 set_genesis_time();
510
511 pid = fork();
512 if (pid) {
513 fio_server_add_job_pid(pid);
514 return 0;
515 }
516
Jens Axboe2e1df072012-02-09 11:15:02 +0100517 ret = fio_backend();
Jens Axboe122c7722012-03-19 09:13:15 +0100518 _exit(ret);
Jens Axboe81179ee2011-10-04 12:42:06 +0200519}
520
Jens Axboeb9d2f302012-03-08 20:36:28 +0100521static int handle_job_cmd(struct fio_net_cmd *cmd)
522{
Jens Axboe46bcd492012-03-14 11:31:21 +0100523 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
524 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100525 struct cmd_start_pdu spdu;
526
Jens Axboe46bcd492012-03-14 11:31:21 +0100527 pdu->buf_len = le32_to_cpu(pdu->buf_len);
528 pdu->client_type = le32_to_cpu(pdu->client_type);
529
530 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100531 fio_net_send_quit(server_fd);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100532 return -1;
533 }
534
535 spdu.jobs = cpu_to_le32(thread_number);
536 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
537 return 0;
538}
539
Jens Axboe81179ee2011-10-04 12:42:06 +0200540static int handle_jobline_cmd(struct fio_net_cmd *cmd)
541{
Jens Axboefa2ea802011-10-08 21:07:29 +0200542 void *pdu = cmd->payload;
543 struct cmd_single_line_pdu *cslp;
544 struct cmd_line_pdu *clp;
545 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100546 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200547 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100548 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200549
Jens Axboefa2ea802011-10-08 21:07:29 +0200550 clp = pdu;
551 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100552 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200553 argv = malloc(clp->lines * sizeof(char *));
554 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200555
Jens Axboefa2ea802011-10-08 21:07:29 +0200556 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200557
Jens Axboefa2ea802011-10-08 21:07:29 +0200558 for (i = 0; i < clp->lines; i++) {
559 cslp = pdu + offset;
560 argv[i] = (char *) cslp->text;
561
562 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200563 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
564 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200565
Jens Axboe46bcd492012-03-14 11:31:21 +0100566 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100567 fio_net_send_quit(server_fd);
Jens Axboefa2ea802011-10-08 21:07:29 +0200568 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200569 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200570 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200571
Jens Axboefa2ea802011-10-08 21:07:29 +0200572 free(argv);
573
Jens Axboeb9d2f302012-03-08 20:36:28 +0100574 spdu.jobs = cpu_to_le32(thread_number);
575 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
576 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600577}
578
Jens Axboec28e8e82011-10-04 08:57:39 +0200579static int handle_probe_cmd(struct fio_net_cmd *cmd)
580{
581 struct cmd_probe_pdu probe;
582
Jens Axboe89c17072011-10-11 10:15:51 +0200583 dprint(FD_NET, "server: sending probe reply\n");
584
Jens Axboec28e8e82011-10-04 08:57:39 +0200585 memset(&probe, 0, sizeof(probe));
586 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200587#ifdef FIO_BIG_ENDIAN
588 probe.bigendian = 1;
589#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200590 probe.fio_major = FIO_MAJOR;
591 probe.fio_minor = FIO_MINOR;
592 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200593
Jens Axboecca84642011-10-07 12:47:57 +0200594 probe.os = FIO_OS;
595 probe.arch = FIO_ARCH;
596
Jens Axboe38fdef22011-10-11 14:30:06 +0200597 probe.bpp = sizeof(void *);
598
Jens Axboe89c17072011-10-11 10:15:51 +0200599 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200600}
601
602static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
603{
604 struct jobs_eta *je;
605 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200606 int i;
607
Jens Axboeb814fb22011-10-12 09:20:34 +0200608 if (!thread_number)
609 return 0;
610
611 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200612 je = malloc(size);
613 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200614
615 if (!calc_thread_status(je, 1)) {
616 free(je);
617 return 0;
618 }
619
620 dprint(FD_NET, "server sending status\n");
621
622 je->nr_running = cpu_to_le32(je->nr_running);
623 je->nr_ramp = cpu_to_le32(je->nr_ramp);
624 je->nr_pending = cpu_to_le32(je->nr_pending);
625 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200626
627 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100628 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
629 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
630 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
631 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200632 je->rate[i] = cpu_to_le32(je->rate[i]);
633 je->iops[i] = cpu_to_le32(je->iops[i]);
634 }
635
Jens Axboec1970b92012-03-06 15:37:40 +0100636 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200637 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100638 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200639
Jens Axboe7f868312011-10-10 09:55:21 +0200640 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200641 free(je);
642 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200643}
644
Jens Axboef58bd2a2012-03-27 10:06:42 +0200645static int handle_update_job_cmd(struct fio_net_cmd *cmd)
646{
647 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
648 struct thread_data *td;
649 uint32_t tnumber;
650
651 tnumber = le32_to_cpu(pdu->thread_number);
652
653 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
654
655 if (tnumber >= thread_number) {
656 fio_net_send_ack(server_fd, cmd, ENODEV, 0);
657 return 0;
658 }
659
660 td = &threads[tnumber];
661 convert_thread_options_to_cpu(&td->o, &pdu->top);
662 fio_net_send_ack(server_fd, cmd, 0, 0);
663 return 0;
664}
665
Jens Axboe132159a2011-09-30 15:01:32 -0600666static int handle_command(struct fio_net_cmd *cmd)
667{
668 int ret;
669
Jens Axboe89c17072011-10-11 10:15:51 +0200670 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
671 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600672
Jens Axboe132159a2011-09-30 15:01:32 -0600673 switch (cmd->opcode) {
674 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200675 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200676 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200677 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600678 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200679 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600680 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200681 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600682 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200683 case FIO_NET_CMD_JOBLINE:
684 ret = handle_jobline_cmd(cmd);
685 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200686 case FIO_NET_CMD_PROBE:
687 ret = handle_probe_cmd(cmd);
688 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200689 case FIO_NET_CMD_SEND_ETA:
690 ret = handle_send_eta_cmd(cmd);
691 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100692 case FIO_NET_CMD_RUN:
693 ret = handle_run_cmd(cmd);
694 break;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200695 case FIO_NET_CMD_UPDATE_JOB:
696 ret = handle_update_job_cmd(cmd);
697 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600698 default:
Jens Axboe3c3ed072012-03-27 09:12:39 +0200699 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600700 ret = 1;
701 }
702
703 return ret;
704}
705
Jens Axboe122c7722012-03-19 09:13:15 +0100706static int handle_connection(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600707{
708 struct fio_net_cmd *cmd = NULL;
709 int ret = 0;
710
Jens Axboe122c7722012-03-19 09:13:15 +0100711 reset_fio_state();
712 INIT_FLIST_HEAD(&job_list);
713 server_fd = sk;
714
Jens Axboe132159a2011-09-30 15:01:32 -0600715 /* read forever */
716 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200717 struct pollfd pfd = {
718 .fd = sk,
719 .events = POLLIN,
720 };
721
722 ret = 0;
723 do {
Jens Axboe54163252012-03-23 12:25:18 +0100724 int timeout = 1000;
725
726 if (!flist_empty(&job_list))
727 timeout = 100;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200728
Jens Axboe54163252012-03-23 12:25:18 +0100729 ret = poll(&pfd, 1, timeout);
Jens Axboee951bdc2011-10-05 21:58:45 +0200730 if (ret < 0) {
731 if (errno == EINTR)
732 break;
733 log_err("fio: poll: %s\n", strerror(errno));
734 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200735 } else if (!ret) {
Jens Axboe122c7722012-03-19 09:13:15 +0100736 fio_server_check_jobs();
Jens Axboee951bdc2011-10-05 21:58:45 +0200737 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200738 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200739
740 if (pfd.revents & POLLIN)
741 break;
742 if (pfd.revents & (POLLERR|POLLHUP)) {
743 ret = 1;
744 break;
745 }
Jens Axboe19c65172011-10-05 22:05:37 +0200746 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200747
Jens Axboe122c7722012-03-19 09:13:15 +0100748 fio_server_check_jobs();
749
Jens Axboee951bdc2011-10-05 21:58:45 +0200750 if (ret < 0)
751 break;
752
753 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600754 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200755 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600756 break;
757 }
758
Jens Axboe132159a2011-09-30 15:01:32 -0600759 ret = handle_command(cmd);
760 if (ret)
761 break;
762
763 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400764 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600765 }
766
767 if (cmd)
768 free(cmd);
769
Jens Axboe122c7722012-03-19 09:13:15 +0100770 close(sk);
771 _exit(ret);
Jens Axboecc0df002011-10-03 20:53:32 +0200772}
773
Jens Axboe50d16972011-09-29 17:45:28 -0600774static int accept_loop(int listen_sk)
775{
Jens Axboebb447a22011-10-04 15:06:42 +0200776 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200777 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600778 struct pollfd pfd;
Jens Axboe122c7722012-03-19 09:13:15 +0100779 int ret = 0, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600780
Jens Axboe60efd142011-10-04 13:30:11 +0200781 dprint(FD_NET, "server enter accept loop\n");
782
Jens Axboe009b1be2011-09-29 18:27:02 -0600783 flags = fcntl(listen_sk, F_GETFL);
784 flags |= O_NONBLOCK;
785 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe122c7722012-03-19 09:13:15 +0100786
787 while (!exit_backend) {
788 pid_t pid;
789
790 pfd.fd = listen_sk;
791 pfd.events = POLLIN;
792 do {
Jens Axboe54163252012-03-23 12:25:18 +0100793 int timeout = 1000;
794
795 if (!flist_empty(&conn_list))
796 timeout = 100;
797
798 ret = poll(&pfd, 1, timeout);
Jens Axboe122c7722012-03-19 09:13:15 +0100799 if (ret < 0) {
800 if (errno == EINTR)
801 break;
802 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600803 break;
Jens Axboe122c7722012-03-19 09:13:15 +0100804 } else if (!ret) {
805 fio_server_check_conns();
806 continue;
807 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600808
Jens Axboe122c7722012-03-19 09:13:15 +0100809 if (pfd.revents & POLLIN)
810 break;
811 } while (!exit_backend);
812
813 fio_server_check_conns();
814
815 if (exit_backend || ret < 0)
Jens Axboe009b1be2011-09-29 18:27:02 -0600816 break;
Jens Axboe009b1be2011-09-29 18:27:02 -0600817
Jens Axboe122c7722012-03-19 09:13:15 +0100818 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
819 if (sk < 0) {
820 log_err("fio: accept: %s\n", strerror(errno));
821 return -1;
822 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600823
Jens Axboe122c7722012-03-19 09:13:15 +0100824 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
825
826 pid = fork();
827 if (pid) {
828 close(sk);
829 fio_server_add_conn_pid(pid);
830 continue;
831 }
832
833 /* exits */
834 handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -0600835 }
836
Jens Axboe132159a2011-09-30 15:01:32 -0600837 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600838}
839
Jens Axboe084d1c62012-03-03 20:28:07 +0100840int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600841{
Jens Axboe084d1c62012-03-03 20:28:07 +0100842 struct cmd_text_pdu *pdu;
843 unsigned int tlen;
844 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600845
Jens Axboe084d1c62012-03-03 20:28:07 +0100846 if (server_fd == -1)
847 return log_local_buf(buf, len);
848
849 tlen = sizeof(*pdu) + len;
850 pdu = malloc(tlen);
851
852 pdu->level = __cpu_to_le32(level);
853 pdu->buf_len = __cpu_to_le32(len);
854
855 gettimeofday(&tv, NULL);
856 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
857 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
858
859 memcpy(pdu->buf, buf, len);
860
861 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, 0);
862 free(pdu);
863 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600864}
865
Jens Axboea64e88d2011-10-03 14:20:01 +0200866static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
867{
868 dst->max_val = cpu_to_le64(src->max_val);
869 dst->min_val = cpu_to_le64(src->min_val);
870 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200871
872 /*
873 * Encode to IEEE 754 for network transfer
874 */
875 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
876 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200877}
878
879static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
880{
881 int i;
882
883 for (i = 0; i < 2; i++) {
884 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
885 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
886 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
887 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
888 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
889 dst->agg[i] = cpu_to_le64(src->agg[i]);
890 }
891
892 dst->kb_base = cpu_to_le32(src->kb_base);
893 dst->groupid = cpu_to_le32(src->groupid);
894}
895
896/*
897 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
898 * into a single payload.
899 */
900void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
901{
902 struct cmd_ts_pdu p;
903 int i, j;
904
Jens Axboe60efd142011-10-04 13:30:11 +0200905 dprint(FD_NET, "server sending end stats\n");
906
Jens Axboe317b3c82011-10-03 21:47:27 +0200907 memset(&p, 0, sizeof(p));
908
Jens Axboea64e88d2011-10-03 14:20:01 +0200909 strcpy(p.ts.name, ts->name);
910 strcpy(p.ts.verror, ts->verror);
911 strcpy(p.ts.description, ts->description);
912
Jens Axboe2f122b12012-03-15 13:10:19 +0100913 p.ts.error = cpu_to_le32(ts->error);
914 p.ts.thread_number = cpu_to_le32(ts->thread_number);
915 p.ts.groupid = cpu_to_le32(ts->groupid);
916 p.ts.pid = cpu_to_le32(ts->pid);
917 p.ts.members = cpu_to_le32(ts->members);
Jens Axboea64e88d2011-10-03 14:20:01 +0200918
919 for (i = 0; i < 2; i++) {
920 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
921 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
922 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
923 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
924 }
925
926 p.ts.usr_time = cpu_to_le64(ts->usr_time);
927 p.ts.sys_time = cpu_to_le64(ts->sys_time);
928 p.ts.ctx = cpu_to_le64(ts->ctx);
929 p.ts.minf = cpu_to_le64(ts->minf);
930 p.ts.majf = cpu_to_le64(ts->majf);
931 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200932
933 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200934 fio_fp64_t *src = &ts->percentile_list[i];
935 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200936
Jens Axboecfc03e42011-10-12 21:20:42 +0200937 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200938 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200939
940 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
941 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
942 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
943 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
944 }
945
946 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
947 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
948 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
949 }
950
951 for (i = 0; i < 2; i++)
952 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
953 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
954
955 for (i = 0; i < 3; i++) {
956 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200957 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200958 }
959
Jens Axboe93eee042011-10-03 21:08:48 +0200960 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200961 p.ts.total_complete = cpu_to_le64(ts->total_complete);
962
963 for (i = 0; i < 2; i++) {
964 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
965 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
966 }
967
968 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
969 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
970 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200971 p.ts.first_error = cpu_to_le32(ts->first_error);
972 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200973
974 convert_gs(&p.rs, rs);
975
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200976 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200977}
978
979void fio_server_send_gs(struct group_run_stats *rs)
980{
981 struct group_run_stats gs;
982
Jens Axboe60efd142011-10-04 13:30:11 +0200983 dprint(FD_NET, "server sending group run stats\n");
984
Jens Axboea64e88d2011-10-03 14:20:01 +0200985 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200986 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200987}
988
Jens Axboed09a64a2011-10-13 11:38:56 +0200989static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
990{
991 int i;
992
993 for (i = 0; i < 2; i++) {
994 dst->ios[i] = cpu_to_le32(src->ios[i]);
995 dst->merges[i] = cpu_to_le32(src->merges[i]);
996 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
997 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
998 }
999
1000 dst->io_ticks = cpu_to_le32(src->io_ticks);
1001 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1002 dst->slavecount = cpu_to_le32(src->slavecount);
1003 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1004}
1005
1006static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1007{
1008 int i;
1009
1010 strcpy((char *) dst->name, (char *) src->name);
1011
1012 for (i = 0; i < 2; i++) {
1013 dst->ios[i] = cpu_to_le32(src->ios[i]);
1014 dst->merges[i] = cpu_to_le32(src->merges[i]);
1015 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1016 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1017 }
1018
1019 dst->io_ticks = cpu_to_le32(src->io_ticks);
1020 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1021 dst->msec = cpu_to_le64(src->msec);
1022}
1023
1024void fio_server_send_du(void)
1025{
1026 struct disk_util *du;
1027 struct flist_head *entry;
1028 struct cmd_du_pdu pdu;
1029
1030 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1031
Jens Axboe0766d922011-10-13 12:02:08 +02001032 memset(&pdu, 0, sizeof(pdu));
1033
Jens Axboed09a64a2011-10-13 11:38:56 +02001034 flist_for_each(entry, &disk_list) {
1035 du = flist_entry(entry, struct disk_util, list);
1036
1037 convert_dus(&pdu.dus, &du->dus);
1038 convert_agg(&pdu.agg, &du->agg);
1039
1040 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
1041 }
1042}
1043
Jens Axboe53bd8db2012-03-14 21:51:38 +01001044/*
1045 * Send a command with a separate PDU, not inlined in the command
1046 */
1047static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1048 off_t size, uint64_t tag, uint32_t flags)
1049{
1050 struct fio_net_cmd cmd;
1051 struct iovec iov[2];
1052
1053 iov[0].iov_base = &cmd;
1054 iov[0].iov_len = sizeof(cmd);
1055 iov[1].iov_base = (void *) buf;
1056 iov[1].iov_len = size;
1057
1058 __fio_init_net_cmd(&cmd, opcode, size, tag);
1059 cmd.flags = __cpu_to_le32(flags);
1060 fio_net_cmd_crc_pdu(&cmd, buf);
1061
Jens Axboe8c953072012-03-20 14:35:35 +01001062 return fio_sendv_data(sk, iov, 2);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001063}
1064
Jens Axboe1b427252012-03-14 15:03:03 +01001065int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1066{
Jens Axboef5ed7652012-03-14 16:28:07 +01001067 struct cmd_iolog_pdu pdu;
Jens Axboe1b427252012-03-14 15:03:03 +01001068 z_stream stream;
1069 void *out_pdu;
Jens Axboe53bd8db2012-03-14 21:51:38 +01001070 int i, ret = 0;
Jens Axboe1b427252012-03-14 15:03:03 +01001071
Jens Axboe2f122b12012-03-15 13:10:19 +01001072 pdu.thread_number = cpu_to_le32(td->thread_number);
Jens Axboef5ed7652012-03-14 16:28:07 +01001073 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
1074 pdu.log_type = cpu_to_le32(log->log_type);
1075 strcpy((char *) pdu.name, name);
Jens Axboe1b427252012-03-14 15:03:03 +01001076
1077 for (i = 0; i < log->nr_samples; i++) {
Jens Axboef5ed7652012-03-14 16:28:07 +01001078 struct io_sample *s = &log->log[i];
Jens Axboe1b427252012-03-14 15:03:03 +01001079
Jens Axboef5ed7652012-03-14 16:28:07 +01001080 s->time = cpu_to_le64(s->time);
1081 s->val = cpu_to_le64(s->val);
1082 s->ddir = cpu_to_le32(s->ddir);
1083 s->bs = cpu_to_le32(s->bs);
Jens Axboe1b427252012-03-14 15:03:03 +01001084 }
1085
1086 /*
1087 * Dirty - since the log is potentially huge, compress it into
1088 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1089 * side defragment it.
1090 */
1091 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1092
1093 stream.zalloc = Z_NULL;
1094 stream.zfree = Z_NULL;
1095 stream.opaque = Z_NULL;
1096
1097 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001098 ret = 1;
1099 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001100 }
1101
1102 /*
Jens Axboef5ed7652012-03-14 16:28:07 +01001103 * Send header first, it's not compressed.
Jens Axboe1b427252012-03-14 15:03:03 +01001104 */
Jens Axboe53bd8db2012-03-14 21:51:38 +01001105 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1106 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1107 if (ret)
1108 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001109
Jens Axboef5ed7652012-03-14 16:28:07 +01001110 stream.next_in = (void *) log->log;
1111 stream.avail_in = log->nr_samples * sizeof(struct io_sample);
Jens Axboe1b427252012-03-14 15:03:03 +01001112
1113 do {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001114 unsigned int this_len, flags = 0;
1115 int ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001116
1117 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1118 stream.next_out = out_pdu;
Jens Axboe3c547fe2012-03-14 21:53:00 +01001119 ret = deflate(&stream, Z_FINISH);
1120 /* may be Z_OK, or Z_STREAM_END */
1121 if (ret < 0)
1122 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001123
1124 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1125
Jens Axboe1b427252012-03-14 15:03:03 +01001126 if (stream.avail_in)
Jens Axboe53bd8db2012-03-14 21:51:38 +01001127 flags = FIO_NET_CMD_F_MORE;
Jens Axboe1b427252012-03-14 15:03:03 +01001128
Jens Axboe53bd8db2012-03-14 21:51:38 +01001129 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
1130 out_pdu, this_len, 0, flags);
1131 if (ret)
1132 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001133 } while (stream.avail_in);
1134
Jens Axboe53bd8db2012-03-14 21:51:38 +01001135err_zlib:
Jens Axboe1b427252012-03-14 15:03:03 +01001136 deflateEnd(&stream);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001137err:
1138 free(out_pdu);
1139 return ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001140}
1141
Jens Axboe2f122b12012-03-15 13:10:19 +01001142void fio_server_send_add_job(struct thread_data *td)
Jens Axboe807f9972012-03-02 10:25:24 +01001143{
1144 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +01001145
Jens Axboe731e30a2012-03-14 16:35:37 +01001146 memset(&pdu, 0, sizeof(pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +01001147 pdu.thread_number = cpu_to_le32(td->thread_number);
1148 pdu.groupid = cpu_to_le32(td->groupid);
1149 convert_thread_options_to_net(&pdu.top, &td->o);
Jens Axboe807f9972012-03-02 10:25:24 +01001150
1151 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), 0);
1152}
1153
Jens Axboe122c7722012-03-19 09:13:15 +01001154void fio_server_send_start(struct thread_data *td)
1155{
1156 assert(server_fd != -1);
1157
1158 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1159}
1160
Jens Axboe87aa8f12011-10-06 21:24:13 +02001161static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +02001162{
Jens Axboe811826b2011-10-24 09:11:50 +02001163 struct sockaddr *addr;
1164 fio_socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001165 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +02001166
Jens Axboe811826b2011-10-24 09:11:50 +02001167 if (use_ipv6)
1168 sk = socket(AF_INET6, SOCK_STREAM, 0);
1169 else
1170 sk = socket(AF_INET, SOCK_STREAM, 0);
1171
Jens Axboe81179ee2011-10-04 12:42:06 +02001172 if (sk < 0) {
1173 log_err("fio: socket: %s\n", strerror(errno));
1174 return -1;
1175 }
1176
1177 opt = 1;
1178 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1179 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001180 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001181 return -1;
1182 }
1183#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +02001184 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001185 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001186 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001187 return -1;
1188 }
1189#endif
1190
Jens Axboe811826b2011-10-24 09:11:50 +02001191 if (use_ipv6) {
1192 addr = (struct sockaddr *) &saddr_in6;
1193 socklen = sizeof(saddr_in6);
1194 saddr_in6.sin6_family = AF_INET6;
1195 } else {
1196 addr = (struct sockaddr *) &saddr_in;
1197 socklen = sizeof(saddr_in);
1198 saddr_in.sin_family = AF_INET;
1199 }
Jens Axboe81179ee2011-10-04 12:42:06 +02001200
Jens Axboe811826b2011-10-24 09:11:50 +02001201 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001202 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001203 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001204 return -1;
1205 }
1206
Jens Axboe87aa8f12011-10-06 21:24:13 +02001207 return sk;
1208}
1209
1210static int fio_init_server_sock(void)
1211{
1212 struct sockaddr_un addr;
1213 fio_socklen_t len;
1214 mode_t mode;
1215 int sk;
1216
1217 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1218 if (sk < 0) {
1219 log_err("fio: socket: %s\n", strerror(errno));
1220 return -1;
1221 }
1222
1223 mode = umask(000);
1224
1225 memset(&addr, 0, sizeof(addr));
1226 addr.sun_family = AF_UNIX;
1227 strcpy(addr.sun_path, bind_sock);
1228 unlink(bind_sock);
1229
1230 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1231
1232 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1233 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001234 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001235 return -1;
1236 }
1237
1238 umask(mode);
1239 return sk;
1240}
1241
1242static int fio_init_server_connection(void)
1243{
Jens Axboebebe6392011-10-07 10:00:51 +02001244 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001245 int sk;
1246
1247 dprint(FD_NET, "starting server\n");
1248
1249 if (!bind_sock)
1250 sk = fio_init_server_ip();
1251 else
1252 sk = fio_init_server_sock();
1253
1254 if (sk < 0)
1255 return sk;
1256
Jens Axboe811826b2011-10-24 09:11:50 +02001257 if (!bind_sock) {
1258 char *p, port[16];
1259 const void *src;
1260 int af;
1261
1262 if (use_ipv6) {
1263 af = AF_INET6;
1264 src = &saddr_in6.sin6_addr;
1265 } else {
1266 af = AF_INET;
1267 src = &saddr_in.sin_addr;
1268 }
1269
1270 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1271
1272 sprintf(port, ",%u", fio_net_port);
1273 if (p)
1274 strcat(p, port);
1275 else
1276 strcpy(bind_str, port);
1277 } else
Jens Axboebebe6392011-10-07 10:00:51 +02001278 strcpy(bind_str, bind_sock);
1279
1280 log_info("fio: server listening on %s\n", bind_str);
1281
Jens Axboe89c17072011-10-11 10:15:51 +02001282 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001283 log_err("fio: listen: %s\n", strerror(errno));
1284 return -1;
1285 }
1286
Jens Axboe87aa8f12011-10-06 21:24:13 +02001287 return sk;
1288}
1289
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001290int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1291 struct in6_addr *inp6)
1292
1293{
1294 int ret = 0;
1295
1296 if (*ipv6)
1297 ret = inet_pton(AF_INET6, host, inp6);
1298 else
1299 ret = inet_pton(AF_INET, host, inp);
1300
1301 if (ret != 1) {
1302 struct hostent *hent;
1303
1304 hent = gethostbyname(host);
1305 if (!hent) {
1306 log_err("fio: failed to resolve <%s>\n", host);
1307 return 0;
1308 }
1309
1310 if (*ipv6) {
1311 if (hent->h_addrtype != AF_INET6) {
1312 log_info("fio: falling back to IPv4\n");
1313 *ipv6 = 0;
1314 } else
1315 memcpy(inp6, hent->h_addr_list[0], 16);
1316 }
1317 if (!*ipv6) {
1318 if (hent->h_addrtype != AF_INET) {
1319 log_err("fio: lookup type mismatch\n");
1320 return 0;
1321 }
1322 memcpy(inp, hent->h_addr_list[0], 4);
1323 }
1324 ret = 1;
1325 }
1326
1327 return !(ret == 1);
1328}
1329
Jens Axboe660a2bf2011-10-24 09:35:06 +02001330/*
1331 * Parse a host/ip/port string. Reads from 'str'.
1332 *
1333 * Outputs:
1334 *
1335 * For IPv4:
1336 * *ptr is the host, *port is the port, inp is the destination.
1337 * For IPv6:
1338 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1339 * For local domain sockets:
1340 * *ptr is the filename, *is_sock is 1.
1341 */
Jens Axboebebe6392011-10-07 10:00:51 +02001342int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001343 int *port, struct in_addr *inp,
1344 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001345{
Jens Axboe76867622011-10-25 09:52:51 +02001346 const char *host = str;
1347 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001348 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001349
Jens Axboebebe6392011-10-07 10:00:51 +02001350 *ptr = NULL;
1351 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001352 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001353 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001354
1355 if (!strncmp(str, "sock:", 5)) {
1356 *ptr = strdup(str + 5);
1357 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001358
Jens Axboe76867622011-10-25 09:52:51 +02001359 return 0;
1360 }
1361
1362 /*
1363 * Is it ip:<ip or host>:port
1364 */
1365 if (!strncmp(host, "ip:", 3))
1366 host += 3;
1367 else if (!strncmp(host, "ip4:", 4))
1368 host += 4;
1369 else if (!strncmp(host, "ip6:", 4)) {
1370 host += 4;
1371 *ipv6 = 1;
1372 } else if (host[0] == ':') {
1373 /* String is :port */
1374 host++;
1375 lport = atoi(host);
1376 if (!lport || lport > 65535) {
1377 log_err("fio: bad server port %u\n", port);
1378 return 1;
1379 }
1380 /* no hostname given, we are done */
1381 *port = lport;
1382 return 0;
1383 }
1384
1385 /*
1386 * If no port seen yet, check if there's a last ':' at the end
1387 */
1388 if (!lport) {
1389 portp = strchr(host, ',');
1390 if (portp) {
1391 *portp = '\0';
1392 portp++;
1393 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001394 if (!lport || lport > 65535) {
1395 log_err("fio: bad server port %u\n", port);
1396 return 1;
1397 }
Jens Axboe76867622011-10-25 09:52:51 +02001398 }
1399 }
1400
1401 if (lport)
1402 *port = lport;
1403
1404 if (!strlen(host))
1405 return 0;
1406
1407 *ptr = strdup(host);
1408
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001409 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1410 free(*ptr);
1411 *ptr = NULL;
1412 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001413 }
1414
1415 if (*port == 0)
1416 *port = fio_net_port;
1417
1418 return 0;
1419}
1420
Jens Axboe87aa8f12011-10-06 21:24:13 +02001421/*
1422 * Server arg should be one of:
1423 *
1424 * sock:/path/to/socket
1425 * ip:1.2.3.4
1426 * 1.2.3.4
1427 *
1428 * Where sock uses unix domain sockets, and ip binds the server to
1429 * a specific interface. If no arguments are given to the server, it
1430 * uses IP and binds to 0.0.0.0.
1431 *
1432 */
1433static int fio_handle_server_arg(void)
1434{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001435 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001436 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001437
Jens Axboe87aa8f12011-10-06 21:24:13 +02001438 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1439
1440 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001441 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001442
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001443 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001444 &port, &saddr_in.sin_addr,
1445 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001446
1447 if (!is_sock && bind_sock) {
1448 free(bind_sock);
1449 bind_sock = NULL;
1450 }
1451
Jens Axboea7de0a12011-10-07 12:55:14 +02001452out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001453 fio_net_port = port;
1454 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001455 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001456 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001457}
1458
1459static int fio_server(void)
1460{
1461 int sk, ret;
1462
1463 dprint(FD_NET, "starting server\n");
1464
1465 if (fio_handle_server_arg())
1466 return -1;
1467
1468 sk = fio_init_server_connection();
1469 if (sk < 0)
1470 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001471
1472 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001473
Jens Axboe81179ee2011-10-04 12:42:06 +02001474 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001475
1476 if (fio_server_arg) {
1477 free(fio_server_arg);
1478 fio_server_arg = NULL;
1479 }
Jens Axboebebe6392011-10-07 10:00:51 +02001480 if (bind_sock)
1481 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001482
Jens Axboe81179ee2011-10-04 12:42:06 +02001483 return ret;
1484}
1485
Jens Axboe7b821682011-10-11 12:16:32 +02001486void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001487{
Jens Axboe7b821682011-10-11 12:16:32 +02001488 if (signal == SIGPIPE)
1489 server_fd = -1;
1490 else {
1491 log_info("\nfio: terminating on signal %d\n", signal);
1492 exit_backend = 1;
1493 }
Jens Axboe9abea482011-10-04 13:21:54 +02001494}
1495
Jens Axboe13755d92011-10-10 19:51:26 +02001496static int check_existing_pidfile(const char *pidfile)
1497{
1498 struct stat sb;
1499 char buf[16];
1500 pid_t pid;
1501 FILE *f;
1502
1503 if (stat(pidfile, &sb))
1504 return 0;
1505
1506 f = fopen(pidfile, "r");
1507 if (!f)
1508 return 0;
1509
Jens Axboebfc3b172011-10-10 21:16:55 +02001510 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001511 fclose(f);
1512 return 1;
1513 }
1514 fclose(f);
1515
1516 pid = atoi(buf);
1517 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001518 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001519
1520 return 1;
1521}
1522
1523static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001524{
1525 FILE *fpid;
1526
1527 fpid = fopen(pidfile, "w");
1528 if (!fpid) {
1529 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001530 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001531 }
1532
1533 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001534 fclose(fpid);
1535 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001536}
1537
1538/*
1539 * If pidfile is specified, background us.
1540 */
1541int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001542{
1543 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001544 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001545
Bruce Cran93bcfd22012-02-20 20:18:19 +01001546#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001547 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +02001548 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001549#endif
1550
Jens Axboe402668f2011-10-10 15:28:58 +02001551 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001552 return fio_server();
1553
Jens Axboe13755d92011-10-10 19:51:26 +02001554 if (check_existing_pidfile(pidfile)) {
1555 log_err("fio: pidfile %s exists and server appears alive\n",
1556 pidfile);
1557 return -1;
1558 }
1559
Jens Axboee46d8092011-10-03 09:11:02 +02001560 pid = fork();
1561 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001562 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001563 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001564 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001565 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001566 int ret = write_pid(pid, pidfile);
1567
1568 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001569 }
Jens Axboee46d8092011-10-03 09:11:02 +02001570
1571 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001572 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1573 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001574 close(STDIN_FILENO);
1575 close(STDOUT_FILENO);
1576 close(STDERR_FILENO);
1577 f_out = NULL;
1578 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001579
1580 ret = fio_server();
1581
1582 closelog();
1583 unlink(pidfile);
1584 free(pidfile);
1585 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001586}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001587
Jens Axboebebe6392011-10-07 10:00:51 +02001588void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001589{
1590 fio_server_arg = strdup(arg);
1591}