blob: 020d1d7b5a4bc333305ac98fdaf94514d748cc26 [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 Axboe3989b142013-04-12 13:13:24 +020019#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +010020#include <zlib.h>
Jens Axboe3989b142013-04-12 13:13:24 +020021#endif
Jens Axboe50d16972011-09-29 17:45:28 -060022
23#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060024#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060025#include "crc/crc16.h"
Jens Axboec7c6cb42011-10-13 14:12:40 +020026#include "lib/ieee754.h"
Jens Axboe50d16972011-09-29 17:45:28 -060027
Stephen M. Cameron5adc2442012-02-24 08:17:31 +010028int fio_net_port = FIO_NET_PORT;
Jens Axboe50d16972011-09-29 17:45:28 -060029
Jens Axboe009b1be2011-09-29 18:27:02 -060030int exit_backend = 0;
31
Jens Axboe46c48f12011-10-01 12:50:51 -060032static int server_fd = -1;
Jens Axboe87aa8f12011-10-06 21:24:13 +020033static char *fio_server_arg;
34static char *bind_sock;
35static struct sockaddr_in saddr_in;
Jens Axboe811826b2011-10-24 09:11:50 +020036static struct sockaddr_in6 saddr_in6;
Jens Axboe811826b2011-10-24 09:11:50 +020037static int use_ipv6;
Jens Axboe3989b142013-04-12 13:13:24 +020038#ifdef CONFIG_ZLIB
39static unsigned int has_zlib = 1;
40#else
41static unsigned int has_zlib = 0;
42#endif
43static unsigned int use_zlib;
Jens Axboe37db14f2011-09-30 17:00:42 -060044
Jens Axboe122c7722012-03-19 09:13:15 +010045struct fio_fork_item {
46 struct flist_head list;
47 int exitval;
48 int signal;
49 int exited;
50 pid_t pid;
51};
52
53/* Created on fork on new connection */
54static FLIST_HEAD(conn_list);
55
56/* Created on job fork from connection */
57static FLIST_HEAD(job_list);
58
Jens Axboe89c17072011-10-11 10:15:51 +020059static const char *fio_server_ops[FIO_NET_CMD_NR] = {
60 "",
61 "QUIT",
62 "EXIT",
63 "JOB",
64 "JOBLINE",
65 "TEXT",
66 "TS",
67 "GS",
68 "SEND_ETA",
69 "ETA",
70 "PROBE",
71 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020072 "STOP",
73 "DISK_UTIL",
Jens Axboeb9d2f302012-03-08 20:36:28 +010074 "SERVER_START",
Jens Axboe807f9972012-03-02 10:25:24 +010075 "ADD_JOB",
Jens Axboeb9d2f302012-03-08 20:36:28 +010076 "CMD_RUN"
Jens Axboec70e63e2012-03-15 08:26:18 +010077 "CMD_IOLOG",
Jens Axboe89c17072011-10-11 10:15:51 +020078};
79
80const char *fio_server_op(unsigned int op)
81{
82 static char buf[32];
83
84 if (op < FIO_NET_CMD_NR)
85 return fio_server_ops[op];
86
87 sprintf(buf, "UNKNOWN/%d", op);
88 return buf;
89}
90
Jens Axboe5235f622012-03-14 21:25:51 +010091static ssize_t iov_total_len(const struct iovec *iov, int count)
Jens Axboe132159a2011-09-30 15:01:32 -060092{
Jens Axboe5235f622012-03-14 21:25:51 +010093 ssize_t ret = 0;
94
95 while (count--) {
96 ret += iov->iov_len;
97 iov++;
98 }
99
100 return ret;
101}
102
103static int fio_sendv_data(int sk, struct iovec *iov, int count)
104{
105 ssize_t total_len = iov_total_len(iov, count);
106 ssize_t ret;
Jens Axboe794d69c2011-10-01 08:48:50 -0600107
Jens Axboe132159a2011-09-30 15:01:32 -0600108 do {
Jens Axboe5235f622012-03-14 21:25:51 +0100109 ret = writev(sk, iov, count);
Jens Axboe132159a2011-09-30 15:01:32 -0600110 if (ret > 0) {
Jens Axboe5235f622012-03-14 21:25:51 +0100111 total_len -= ret;
112 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600113 break;
Jens Axboe5235f622012-03-14 21:25:51 +0100114
115 while (ret) {
116 if (ret >= iov->iov_len) {
117 ret -= iov->iov_len;
118 iov++;
119 continue;
120 }
121 iov->iov_base += ret;
122 iov->iov_len -= ret;
123 ret = 0;
124 }
Jens Axboe132159a2011-09-30 15:01:32 -0600125 } else if (!ret)
126 break;
127 else if (errno == EAGAIN || errno == EINTR)
128 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200129 else
130 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600131 } while (!exit_backend);
132
Jens Axboe5235f622012-03-14 21:25:51 +0100133 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600134 return 0;
135
Jens Axboe8b4e61b2012-03-09 17:10:54 +0100136 if (errno)
137 return -errno;
138
Jens Axboe132159a2011-09-30 15:01:32 -0600139 return 1;
140}
141
Jens Axboe5235f622012-03-14 21:25:51 +0100142int fio_send_data(int sk, const void *p, unsigned int len)
143{
144 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
145
146 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
147
148 return fio_sendv_data(sk, &iov, 1);
149}
150
Jens Axboe132159a2011-09-30 15:01:32 -0600151int fio_recv_data(int sk, void *p, unsigned int len)
152{
153 do {
154 int ret = recv(sk, p, len, MSG_WAITALL);
155
156 if (ret > 0) {
157 len -= ret;
158 if (!len)
159 break;
160 p += ret;
161 continue;
162 } else if (!ret)
163 break;
164 else if (errno == EAGAIN || errno == EINTR)
165 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200166 else
167 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600168 } while (!exit_backend);
169
170 if (!len)
171 return 0;
172
173 return -1;
174}
175
176static int verify_convert_cmd(struct fio_net_cmd *cmd)
177{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600178 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600179
Jens Axboefcee5ff2011-09-30 22:50:39 -0600180 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
181 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600182
Jens Axboe25dfa842012-02-29 10:01:34 +0100183 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600184 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600185 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600186 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600187 return 1;
188 }
189
190 cmd->version = le16_to_cpu(cmd->version);
191 cmd->opcode = le16_to_cpu(cmd->opcode);
192 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200193 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600194 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
195
196 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200197 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600198 break;
199 default:
200 log_err("fio: bad server cmd version %d\n", cmd->version);
201 return 1;
202 }
203
Jens Axboeb9d2f302012-03-08 20:36:28 +0100204 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600205 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
206 return 1;
207 }
208
209 return 0;
210}
211
Jens Axboea64e88d2011-10-03 14:20:01 +0200212/*
213 * Read (and defragment, if necessary) incoming commands
214 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200215struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600216{
Jens Axboea64e88d2011-10-03 14:20:01 +0200217 struct fio_net_cmd cmd, *cmdret = NULL;
218 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600219 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200220 int ret, first = 1;
221 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600222
Jens Axboea64e88d2011-10-03 14:20:01 +0200223 do {
224 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
225 if (ret)
226 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600227
Jens Axboea64e88d2011-10-03 14:20:01 +0200228 /* We have a command, verify it and swap if need be */
229 ret = verify_convert_cmd(&cmd);
230 if (ret)
231 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600232
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200233 if (first) {
234 /* if this is text, add room for \0 at the end */
235 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
236 assert(!cmdret);
237 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200238 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600239
Jens Axboea64e88d2011-10-03 14:20:01 +0200240 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600241
Jens Axboea64e88d2011-10-03 14:20:01 +0200242 if (first)
243 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200244 else if (cmdret->opcode != cmd.opcode) {
245 log_err("fio: fragment opcode mismatch (%d != %d)\n",
246 cmdret->opcode, cmd.opcode);
247 ret = 1;
248 break;
249 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200250
251 if (!cmd.pdu_len)
252 break;
253
254 /* There's payload, get it */
255 pdu = (void *) cmdret->payload + pdu_offset;
256 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
257 if (ret)
258 break;
259
260 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100261 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200262 if (crc != cmd.pdu_crc16) {
263 log_err("fio: server bad crc on payload ");
264 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
265 ret = 1;
266 break;
267 }
268
269 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200270 if (!first)
271 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200272 first = 0;
273 } while (cmd.flags & FIO_NET_CMD_F_MORE);
274
275 if (ret) {
276 free(cmdret);
277 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200278 } else if (cmdret) {
279 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100280 if (cmdret->pdu_len) {
281 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
282 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
283 char *buf = (char *) pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200284
Jens Axboe3c3ed072012-03-27 09:12:39 +0200285 buf[pdu->buf_len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100286 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
Jens Axboe46bcd492012-03-14 11:31:21 +0100287 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
288 char *buf = (char *) pdu->buf;
289 int len = le32_to_cpu(pdu->buf_len);
Jens Axboe084d1c62012-03-03 20:28:07 +0100290
Jens Axboe46bcd492012-03-14 11:31:21 +0100291 buf[len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100292 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200293 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100294
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200295 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200296 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200297 }
Jens Axboe132159a2011-09-30 15:01:32 -0600298
Jens Axboea64e88d2011-10-03 14:20:01 +0200299 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600300}
301
Jens Axboe40c60512012-03-27 16:03:04 +0200302static void add_reply(uint64_t tag, struct flist_head *list)
303{
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200304 struct fio_net_cmd_reply *reply;
Jens Axboe40c60512012-03-27 16:03:04 +0200305
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200306 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
Jens Axboe40c60512012-03-27 16:03:04 +0200307 flist_add_tail(&reply->list, list);
308}
309
310static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
311{
312 struct fio_net_cmd_reply *reply;
313
314 reply = calloc(1, sizeof(*reply));
315 INIT_FLIST_HEAD(&reply->list);
316 gettimeofday(&reply->tv, NULL);
317 reply->saved_tag = tag;
318 reply->opcode = opcode;
319
320 return (uintptr_t) reply;
321}
322
323static void free_reply(uint64_t tag)
324{
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200325 struct fio_net_cmd_reply *reply;
Jens Axboe40c60512012-03-27 16:03:04 +0200326
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200327 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
Jens Axboe40c60512012-03-27 16:03:04 +0200328 free(reply);
329}
330
Jens Axboe53bd8db2012-03-14 21:51:38 +0100331void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
Jens Axboe132159a2011-09-30 15:01:32 -0600332{
333 uint32_t pdu_len;
334
Jens Axboe25dfa842012-02-29 10:01:34 +0100335 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600336
337 pdu_len = le32_to_cpu(cmd->pdu_len);
Jens Axboe1b427252012-03-14 15:03:03 +0100338 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
339}
340
341void fio_net_cmd_crc(struct fio_net_cmd *cmd)
342{
343 fio_net_cmd_crc_pdu(cmd, cmd->payload);
Jens Axboe132159a2011-09-30 15:01:32 -0600344}
345
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200346int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
Jens Axboe40c60512012-03-27 16:03:04 +0200347 uint64_t *tagptr, struct flist_head *list)
Jens Axboe794d69c2011-10-01 08:48:50 -0600348{
Jens Axboe7f868312011-10-10 09:55:21 +0200349 struct fio_net_cmd *cmd = NULL;
350 size_t this_len, cur_len = 0;
Jens Axboe40c60512012-03-27 16:03:04 +0200351 uint64_t tag;
Jens Axboe794d69c2011-10-01 08:48:50 -0600352 int ret;
353
Jens Axboe40c60512012-03-27 16:03:04 +0200354 if (list) {
355 assert(tagptr);
356 tag = *tagptr = alloc_reply(*tagptr, opcode);
357 } else
358 tag = tagptr ? *tagptr : 0;
359
Jens Axboe794d69c2011-10-01 08:48:50 -0600360 do {
361 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100362 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
363 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600364
Jens Axboe7f868312011-10-10 09:55:21 +0200365 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
366 if (cmd)
367 free(cmd);
368
369 cur_len = sizeof(*cmd) + this_len;
370 cmd = malloc(cur_len);
371 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600372
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200373 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600374
375 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200376 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600377
378 fio_net_cmd_crc(cmd);
379
380 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600381 size -= this_len;
382 buf += this_len;
383 } while (!ret && size);
384
Jens Axboe40c60512012-03-27 16:03:04 +0200385 if (list) {
386 if (ret)
387 free_reply(tag);
388 else
389 add_reply(tag, list);
390 }
391
Jens Axboe7f868312011-10-10 09:55:21 +0200392 if (cmd)
393 free(cmd);
394
Jens Axboe794d69c2011-10-01 08:48:50 -0600395 return ret;
396}
397
Jens Axboe89c17072011-10-11 10:15:51 +0200398static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600399{
Jens Axboe178cde92011-10-05 22:14:31 +0200400 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600401
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200402 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600403 fio_net_cmd_crc(&cmd);
404
405 return fio_send_data(sk, &cmd, sizeof(cmd));
406}
407
Jens Axboe89c17072011-10-11 10:15:51 +0200408/*
409 * If 'list' is non-NULL, then allocate and store the sent command for
410 * later verification.
411 */
412int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
413 struct flist_head *list)
414{
Jens Axboe89c17072011-10-11 10:15:51 +0200415 int ret;
416
Jens Axboe40c60512012-03-27 16:03:04 +0200417 if (list)
418 tag = alloc_reply(tag, opcode);
Jens Axboe89c17072011-10-11 10:15:51 +0200419
Jens Axboe40c60512012-03-27 16:03:04 +0200420 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
Jens Axboe89c17072011-10-11 10:15:51 +0200421 if (ret) {
Jens Axboe40c60512012-03-27 16:03:04 +0200422 if (list)
423 free_reply(tag);
424
Jens Axboe89c17072011-10-11 10:15:51 +0200425 return ret;
426 }
427
Jens Axboe40c60512012-03-27 16:03:04 +0200428 if (list)
429 add_reply(tag, list);
430
Jens Axboe89c17072011-10-11 10:15:51 +0200431 return 0;
432}
433
Jens Axboe122c7722012-03-19 09:13:15 +0100434int fio_net_send_quit(int sk)
Jens Axboe437377e2011-10-01 08:31:30 -0600435{
Jens Axboe46c48f12011-10-01 12:50:51 -0600436 dprint(FD_NET, "server: sending quit\n");
Jens Axboe122c7722012-03-19 09:13:15 +0100437
Jens Axboe8c953072012-03-20 14:35:35 +0100438 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600439}
440
Jens Axboef58bd2a2012-03-27 10:06:42 +0200441static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error,
442 int signal)
Jens Axboe122c7722012-03-19 09:13:15 +0100443{
444 struct cmd_end_pdu epdu;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200445 uint64_t tag = 0;
Jens Axboe122c7722012-03-19 09:13:15 +0100446
Jens Axboef58bd2a2012-03-27 10:06:42 +0200447 if (cmd)
448 tag = cmd->tag;
Jens Axboe122c7722012-03-19 09:13:15 +0100449
450 epdu.error = __cpu_to_le32(error);
451 epdu.signal = __cpu_to_le32(signal);
Jens Axboe40c60512012-03-27 16:03:04 +0200452 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200453}
454
455int fio_net_send_stop(int sk, int error, int signal)
456{
457 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
458 return fio_net_send_ack(sk, NULL, error, signal);
Jens Axboe122c7722012-03-19 09:13:15 +0100459}
460
461static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
462{
463 struct fio_fork_item *ffi;
464
465 ffi = malloc(sizeof(*ffi));
466 ffi->exitval = 0;
467 ffi->signal = 0;
468 ffi->exited = 0;
469 ffi->pid = pid;
470 flist_add_tail(&ffi->list, list);
471}
472
473static void fio_server_add_conn_pid(pid_t pid)
474{
475 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", pid);
476 fio_server_add_fork_item(pid, &conn_list);
477}
478
479static void fio_server_add_job_pid(pid_t pid)
480{
481 dprint(FD_NET, "server: forked off job job (pid=%u)\n", pid);
482 fio_server_add_fork_item(pid, &job_list);
483}
484
485static void fio_server_check_fork_item(struct fio_fork_item *ffi)
486{
487 int ret, status;
488
489 ret = waitpid(ffi->pid, &status, WNOHANG);
490 if (ret < 0) {
491 if (errno == ECHILD) {
492 log_err("fio: connection pid %u disappeared\n", ffi->pid);
493 ffi->exited = 1;
494 } else
495 log_err("fio: waitpid: %s\n", strerror(errno));
496 } else if (ret == ffi->pid) {
497 if (WIFSIGNALED(status)) {
498 ffi->signal = WTERMSIG(status);
499 ffi->exited = 1;
500 }
501 if (WIFEXITED(status)) {
502 if (WEXITSTATUS(status))
503 ffi->exitval = WEXITSTATUS(status);
504 ffi->exited = 1;
505 }
506 }
507}
508
509static void fio_server_fork_item_done(struct fio_fork_item *ffi)
510{
511 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", ffi->pid, ffi->signal, ffi->exitval);
512
513 /*
514 * Fold STOP and QUIT...
515 */
516 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
517 fio_net_send_quit(server_fd);
518 flist_del(&ffi->list);
519 free(ffi);
520}
521
Jens Axboe54163252012-03-23 12:25:18 +0100522static void fio_server_check_fork_items(struct flist_head *list)
Jens Axboe122c7722012-03-19 09:13:15 +0100523{
524 struct flist_head *entry, *tmp;
525 struct fio_fork_item *ffi;
526
527 flist_for_each_safe(entry, tmp, list) {
528 ffi = flist_entry(entry, struct fio_fork_item, list);
529
530 fio_server_check_fork_item(ffi);
531
532 if (ffi->exited)
533 fio_server_fork_item_done(ffi);
534 }
535}
536
537static void fio_server_check_jobs(void)
538{
Jens Axboe54163252012-03-23 12:25:18 +0100539 fio_server_check_fork_items(&job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100540}
541
542static void fio_server_check_conns(void)
543{
Jens Axboe54163252012-03-23 12:25:18 +0100544 fio_server_check_fork_items(&conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100545}
546
Jens Axboeb9d2f302012-03-08 20:36:28 +0100547static int handle_run_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600548{
Jens Axboe122c7722012-03-19 09:13:15 +0100549 pid_t pid;
Jens Axboea64e88d2011-10-03 14:20:01 +0200550 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600551
Jens Axboe122c7722012-03-19 09:13:15 +0100552 set_genesis_time();
553
554 pid = fork();
555 if (pid) {
556 fio_server_add_job_pid(pid);
557 return 0;
558 }
559
Jens Axboe2e1df072012-02-09 11:15:02 +0100560 ret = fio_backend();
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200561 free_threads_shm();
Jens Axboe122c7722012-03-19 09:13:15 +0100562 _exit(ret);
Jens Axboe81179ee2011-10-04 12:42:06 +0200563}
564
Jens Axboeb9d2f302012-03-08 20:36:28 +0100565static int handle_job_cmd(struct fio_net_cmd *cmd)
566{
Jens Axboe46bcd492012-03-14 11:31:21 +0100567 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
568 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100569 struct cmd_start_pdu spdu;
570
Jens Axboe46bcd492012-03-14 11:31:21 +0100571 pdu->buf_len = le32_to_cpu(pdu->buf_len);
572 pdu->client_type = le32_to_cpu(pdu->client_type);
573
574 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100575 fio_net_send_quit(server_fd);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100576 return -1;
577 }
578
579 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe108fea72012-11-14 13:09:45 -0700580 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200581 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100582 return 0;
583}
584
Jens Axboe81179ee2011-10-04 12:42:06 +0200585static int handle_jobline_cmd(struct fio_net_cmd *cmd)
586{
Jens Axboefa2ea802011-10-08 21:07:29 +0200587 void *pdu = cmd->payload;
588 struct cmd_single_line_pdu *cslp;
589 struct cmd_line_pdu *clp;
590 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100591 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200592 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100593 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200594
Jens Axboefa2ea802011-10-08 21:07:29 +0200595 clp = pdu;
596 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100597 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200598 argv = malloc(clp->lines * sizeof(char *));
599 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200600
Jens Axboefa2ea802011-10-08 21:07:29 +0200601 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200602
Jens Axboefa2ea802011-10-08 21:07:29 +0200603 for (i = 0; i < clp->lines; i++) {
604 cslp = pdu + offset;
605 argv[i] = (char *) cslp->text;
606
607 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200608 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
609 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200610
Jens Axboe46bcd492012-03-14 11:31:21 +0100611 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100612 fio_net_send_quit(server_fd);
Jens Axboefa2ea802011-10-08 21:07:29 +0200613 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200614 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200615 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200616
Jens Axboefa2ea802011-10-08 21:07:29 +0200617 free(argv);
618
Jens Axboeb9d2f302012-03-08 20:36:28 +0100619 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe1e5324e2012-11-14 14:25:31 -0700620 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200621 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100622 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600623}
624
Jens Axboec28e8e82011-10-04 08:57:39 +0200625static int handle_probe_cmd(struct fio_net_cmd *cmd)
626{
Jens Axboe3989b142013-04-12 13:13:24 +0200627 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
628 struct cmd_probe_reply_pdu probe;
Jens Axboe40c60512012-03-27 16:03:04 +0200629 uint64_t tag = cmd->tag;
Jens Axboec28e8e82011-10-04 08:57:39 +0200630
Jens Axboe89c17072011-10-11 10:15:51 +0200631 dprint(FD_NET, "server: sending probe reply\n");
632
Jens Axboec28e8e82011-10-04 08:57:39 +0200633 memset(&probe, 0, sizeof(probe));
634 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe0dcebdf2013-01-23 15:42:16 -0700635#ifdef CONFIG_BIG_ENDIAN
Jens Axboe6eb24792011-10-04 15:00:30 +0200636 probe.bigendian = 1;
637#endif
Jens Axboe750db472012-04-16 11:44:48 +0200638 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
Jens Axboec28e8e82011-10-04 08:57:39 +0200639
Jens Axboecca84642011-10-07 12:47:57 +0200640 probe.os = FIO_OS;
641 probe.arch = FIO_ARCH;
Jens Axboe38fdef22011-10-11 14:30:06 +0200642 probe.bpp = sizeof(void *);
Jens Axboed31e26d2012-03-28 21:38:24 +0200643 probe.cpus = __cpu_to_le32(cpus_online());
Jens Axboe3989b142013-04-12 13:13:24 +0200644
645 /*
646 * If the client supports compression and we do too, then enable it
647 */
648 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
649 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
650 use_zlib = 1;
651 } else {
652 probe.flags = 0;
653 use_zlib = 0;
654 }
Jens Axboe38fdef22011-10-11 14:30:06 +0200655
Jens Axboe40c60512012-03-27 16:03:04 +0200656 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200657}
658
659static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
660{
661 struct jobs_eta *je;
662 size_t size;
Jens Axboe40c60512012-03-27 16:03:04 +0200663 uint64_t tag = cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200664 int i;
665
Jens Axboeb814fb22011-10-12 09:20:34 +0200666 if (!thread_number)
667 return 0;
668
669 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200670 je = malloc(size);
671 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200672
673 if (!calc_thread_status(je, 1)) {
674 free(je);
675 return 0;
676 }
677
678 dprint(FD_NET, "server sending status\n");
679
680 je->nr_running = cpu_to_le32(je->nr_running);
681 je->nr_ramp = cpu_to_le32(je->nr_ramp);
682 je->nr_pending = cpu_to_le32(je->nr_pending);
683 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200684
Jens Axboe298921d2012-09-24 18:47:01 +0200685 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100686 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
687 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
688 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
689 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboe16725bb2013-04-11 12:43:05 +0200690 je->rate[i] = cpu_to_le32(je->rate[i]);
691 je->iops[i] = cpu_to_le32(je->iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200692 }
693
Jens Axboec1970b92012-03-06 15:37:40 +0100694 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200695 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100696 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200697 je->is_pow2 = cpu_to_le32(je->is_pow2);
Jens Axboeed2c0a12013-04-11 12:55:16 +0200698 je->unit_base = cpu_to_le32(je->unit_base);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200699
Jens Axboe40c60512012-03-27 16:03:04 +0200700 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200701 free(je);
702 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200703}
704
Jens Axboe40c60512012-03-27 16:03:04 +0200705static int send_update_job_reply(int fd, uint64_t __tag, int error)
706{
707 uint64_t tag = __tag;
708 uint32_t pdu_error;
709
710 pdu_error = __cpu_to_le32(error);
711 return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL);
712}
713
Jens Axboef58bd2a2012-03-27 10:06:42 +0200714static int handle_update_job_cmd(struct fio_net_cmd *cmd)
715{
716 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
717 struct thread_data *td;
718 uint32_t tnumber;
719
720 tnumber = le32_to_cpu(pdu->thread_number);
721
722 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
723
Jens Axboe30ffacb2012-03-28 09:15:05 +0200724 if (!tnumber || tnumber > thread_number) {
Jens Axboe40c60512012-03-27 16:03:04 +0200725 send_update_job_reply(server_fd, cmd->tag, ENODEV);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200726 return 0;
727 }
728
Jens Axboe30ffacb2012-03-28 09:15:05 +0200729 td = &threads[tnumber - 1];
Jens Axboef58bd2a2012-03-27 10:06:42 +0200730 convert_thread_options_to_cpu(&td->o, &pdu->top);
Jens Axboe40c60512012-03-27 16:03:04 +0200731 send_update_job_reply(server_fd, cmd->tag, 0);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200732 return 0;
733}
734
Jens Axboe132159a2011-09-30 15:01:32 -0600735static int handle_command(struct fio_net_cmd *cmd)
736{
737 int ret;
738
Jens Axboe4b91ee82013-02-25 10:18:33 +0100739 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
740 fio_server_op(cmd->opcode), cmd->pdu_len,
741 (unsigned long long) cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600742
Jens Axboe132159a2011-09-30 15:01:32 -0600743 switch (cmd->opcode) {
744 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200745 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200746 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200747 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600748 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200749 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600750 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200751 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600752 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200753 case FIO_NET_CMD_JOBLINE:
754 ret = handle_jobline_cmd(cmd);
755 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200756 case FIO_NET_CMD_PROBE:
757 ret = handle_probe_cmd(cmd);
758 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200759 case FIO_NET_CMD_SEND_ETA:
760 ret = handle_send_eta_cmd(cmd);
761 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100762 case FIO_NET_CMD_RUN:
763 ret = handle_run_cmd(cmd);
764 break;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200765 case FIO_NET_CMD_UPDATE_JOB:
766 ret = handle_update_job_cmd(cmd);
767 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600768 default:
Jens Axboe3c3ed072012-03-27 09:12:39 +0200769 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600770 ret = 1;
771 }
772
773 return ret;
774}
775
Jens Axboe122c7722012-03-19 09:13:15 +0100776static int handle_connection(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600777{
778 struct fio_net_cmd *cmd = NULL;
779 int ret = 0;
780
Jens Axboe122c7722012-03-19 09:13:15 +0100781 reset_fio_state();
782 INIT_FLIST_HEAD(&job_list);
783 server_fd = sk;
784
Jens Axboe132159a2011-09-30 15:01:32 -0600785 /* read forever */
786 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200787 struct pollfd pfd = {
788 .fd = sk,
789 .events = POLLIN,
790 };
791
792 ret = 0;
793 do {
Jens Axboe54163252012-03-23 12:25:18 +0100794 int timeout = 1000;
795
796 if (!flist_empty(&job_list))
797 timeout = 100;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200798
Jens Axboe54163252012-03-23 12:25:18 +0100799 ret = poll(&pfd, 1, timeout);
Jens Axboee951bdc2011-10-05 21:58:45 +0200800 if (ret < 0) {
801 if (errno == EINTR)
802 break;
803 log_err("fio: poll: %s\n", strerror(errno));
804 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200805 } else if (!ret) {
Jens Axboe122c7722012-03-19 09:13:15 +0100806 fio_server_check_jobs();
Jens Axboee951bdc2011-10-05 21:58:45 +0200807 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200808 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200809
810 if (pfd.revents & POLLIN)
811 break;
812 if (pfd.revents & (POLLERR|POLLHUP)) {
813 ret = 1;
814 break;
815 }
Jens Axboe19c65172011-10-05 22:05:37 +0200816 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200817
Jens Axboe122c7722012-03-19 09:13:15 +0100818 fio_server_check_jobs();
819
Jens Axboee951bdc2011-10-05 21:58:45 +0200820 if (ret < 0)
821 break;
822
823 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600824 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200825 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600826 break;
827 }
828
Jens Axboe132159a2011-09-30 15:01:32 -0600829 ret = handle_command(cmd);
830 if (ret)
831 break;
832
833 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400834 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600835 }
836
837 if (cmd)
838 free(cmd);
839
Jens Axboe122c7722012-03-19 09:13:15 +0100840 close(sk);
841 _exit(ret);
Jens Axboecc0df002011-10-03 20:53:32 +0200842}
843
Jens Axboe50d16972011-09-29 17:45:28 -0600844static int accept_loop(int listen_sk)
845{
Jens Axboebb447a22011-10-04 15:06:42 +0200846 struct sockaddr_in addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100847 socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600848 struct pollfd pfd;
Jens Axboe122c7722012-03-19 09:13:15 +0100849 int ret = 0, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600850
Jens Axboe60efd142011-10-04 13:30:11 +0200851 dprint(FD_NET, "server enter accept loop\n");
852
Jens Axboe009b1be2011-09-29 18:27:02 -0600853 flags = fcntl(listen_sk, F_GETFL);
854 flags |= O_NONBLOCK;
855 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe122c7722012-03-19 09:13:15 +0100856
857 while (!exit_backend) {
858 pid_t pid;
859
860 pfd.fd = listen_sk;
861 pfd.events = POLLIN;
862 do {
Jens Axboe54163252012-03-23 12:25:18 +0100863 int timeout = 1000;
864
865 if (!flist_empty(&conn_list))
866 timeout = 100;
867
868 ret = poll(&pfd, 1, timeout);
Jens Axboe122c7722012-03-19 09:13:15 +0100869 if (ret < 0) {
870 if (errno == EINTR)
871 break;
872 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600873 break;
Jens Axboe122c7722012-03-19 09:13:15 +0100874 } else if (!ret) {
875 fio_server_check_conns();
876 continue;
877 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600878
Jens Axboe122c7722012-03-19 09:13:15 +0100879 if (pfd.revents & POLLIN)
880 break;
881 } while (!exit_backend);
882
883 fio_server_check_conns();
884
885 if (exit_backend || ret < 0)
Jens Axboe009b1be2011-09-29 18:27:02 -0600886 break;
Jens Axboe009b1be2011-09-29 18:27:02 -0600887
Jens Axboe122c7722012-03-19 09:13:15 +0100888 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
889 if (sk < 0) {
890 log_err("fio: accept: %s\n", strerror(errno));
891 return -1;
892 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600893
Jens Axboe122c7722012-03-19 09:13:15 +0100894 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
895
896 pid = fork();
897 if (pid) {
898 close(sk);
899 fio_server_add_conn_pid(pid);
900 continue;
901 }
902
903 /* exits */
904 handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -0600905 }
906
Jens Axboe132159a2011-09-30 15:01:32 -0600907 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600908}
909
Jens Axboe084d1c62012-03-03 20:28:07 +0100910int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600911{
Jens Axboe084d1c62012-03-03 20:28:07 +0100912 struct cmd_text_pdu *pdu;
913 unsigned int tlen;
914 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600915
Jens Axboe084d1c62012-03-03 20:28:07 +0100916 if (server_fd == -1)
917 return log_local_buf(buf, len);
918
919 tlen = sizeof(*pdu) + len;
920 pdu = malloc(tlen);
921
922 pdu->level = __cpu_to_le32(level);
923 pdu->buf_len = __cpu_to_le32(len);
924
925 gettimeofday(&tv, NULL);
926 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
927 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
928
929 memcpy(pdu->buf, buf, len);
930
Jens Axboe40c60512012-03-27 16:03:04 +0200931 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL);
Jens Axboe084d1c62012-03-03 20:28:07 +0100932 free(pdu);
933 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600934}
935
Jens Axboea64e88d2011-10-03 14:20:01 +0200936static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
937{
938 dst->max_val = cpu_to_le64(src->max_val);
939 dst->min_val = cpu_to_le64(src->min_val);
940 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200941
942 /*
943 * Encode to IEEE 754 for network transfer
944 */
945 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
946 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200947}
948
949static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
950{
951 int i;
952
Jens Axboe298921d2012-09-24 18:47:01 +0200953 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200954 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
955 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
956 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
957 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
958 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
959 dst->agg[i] = cpu_to_le64(src->agg[i]);
960 }
961
962 dst->kb_base = cpu_to_le32(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700963 dst->unit_base = cpu_to_le32(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200964 dst->groupid = cpu_to_le32(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +0100965 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200966}
967
968/*
969 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
970 * into a single payload.
971 */
972void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
973{
974 struct cmd_ts_pdu p;
975 int i, j;
976
Jens Axboe60efd142011-10-04 13:30:11 +0200977 dprint(FD_NET, "server sending end stats\n");
978
Jens Axboe317b3c82011-10-03 21:47:27 +0200979 memset(&p, 0, sizeof(p));
980
Jens Axboea64e88d2011-10-03 14:20:01 +0200981 strcpy(p.ts.name, ts->name);
982 strcpy(p.ts.verror, ts->verror);
983 strcpy(p.ts.description, ts->description);
984
Jens Axboe2f122b12012-03-15 13:10:19 +0100985 p.ts.error = cpu_to_le32(ts->error);
986 p.ts.thread_number = cpu_to_le32(ts->thread_number);
987 p.ts.groupid = cpu_to_le32(ts->groupid);
988 p.ts.pid = cpu_to_le32(ts->pid);
989 p.ts.members = cpu_to_le32(ts->members);
Jens Axboe771e58b2013-01-30 12:56:23 +0100990 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200991
Jens Axboe298921d2012-09-24 18:47:01 +0200992 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200993 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
994 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
995 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
996 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
997 }
998
999 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1000 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1001 p.ts.ctx = cpu_to_le64(ts->ctx);
1002 p.ts.minf = cpu_to_le64(ts->minf);
1003 p.ts.majf = cpu_to_le64(ts->majf);
1004 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +02001005
1006 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +02001007 fio_fp64_t *src = &ts->percentile_list[i];
1008 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +02001009
Jens Axboecfc03e42011-10-12 21:20:42 +02001010 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +02001011 }
Jens Axboea64e88d2011-10-03 14:20:01 +02001012
1013 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1014 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1015 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1016 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1017 }
1018
1019 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1020 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1021 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1022 }
1023
Jens Axboe298921d2012-09-24 18:47:01 +02001024 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +02001025 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1026 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1027
Jens Axboe78799de2013-01-29 20:53:52 +01001028 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001029 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +02001030 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +02001031 }
1032
Jens Axboe93eee042011-10-03 21:08:48 +02001033 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +02001034 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1035
Jens Axboe298921d2012-09-24 18:47:01 +02001036 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001037 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1038 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1039 }
1040
1041 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1042 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1043 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +02001044 p.ts.first_error = cpu_to_le32(ts->first_error);
1045 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -07001046 p.ts.unit_base = cpu_to_le32(ts->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +02001047
1048 convert_gs(&p.rs, rs);
1049
Jens Axboe40c60512012-03-27 16:03:04 +02001050 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL);
Jens Axboea64e88d2011-10-03 14:20:01 +02001051}
1052
1053void fio_server_send_gs(struct group_run_stats *rs)
1054{
1055 struct group_run_stats gs;
1056
Jens Axboe60efd142011-10-04 13:30:11 +02001057 dprint(FD_NET, "server sending group run stats\n");
1058
Jens Axboea64e88d2011-10-03 14:20:01 +02001059 convert_gs(&gs, rs);
Jens Axboe40c60512012-03-27 16:03:04 +02001060 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL);
Jens Axboecf451d12011-10-03 16:48:30 +02001061}
1062
Jens Axboed09a64a2011-10-13 11:38:56 +02001063static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1064{
1065 int i;
1066
1067 for (i = 0; i < 2; i++) {
1068 dst->ios[i] = cpu_to_le32(src->ios[i]);
1069 dst->merges[i] = cpu_to_le32(src->merges[i]);
1070 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1071 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1072 }
1073
1074 dst->io_ticks = cpu_to_le32(src->io_ticks);
1075 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1076 dst->slavecount = cpu_to_le32(src->slavecount);
1077 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1078}
1079
1080static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1081{
1082 int i;
1083
1084 strcpy((char *) dst->name, (char *) src->name);
1085
1086 for (i = 0; i < 2; i++) {
1087 dst->ios[i] = cpu_to_le32(src->ios[i]);
1088 dst->merges[i] = cpu_to_le32(src->merges[i]);
1089 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1090 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1091 }
1092
1093 dst->io_ticks = cpu_to_le32(src->io_ticks);
1094 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1095 dst->msec = cpu_to_le64(src->msec);
1096}
1097
1098void fio_server_send_du(void)
1099{
1100 struct disk_util *du;
1101 struct flist_head *entry;
1102 struct cmd_du_pdu pdu;
1103
1104 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1105
Jens Axboe0766d922011-10-13 12:02:08 +02001106 memset(&pdu, 0, sizeof(pdu));
1107
Jens Axboed09a64a2011-10-13 11:38:56 +02001108 flist_for_each(entry, &disk_list) {
1109 du = flist_entry(entry, struct disk_util, list);
1110
1111 convert_dus(&pdu.dus, &du->dus);
1112 convert_agg(&pdu.agg, &du->agg);
1113
Jens Axboe40c60512012-03-27 16:03:04 +02001114 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboed09a64a2011-10-13 11:38:56 +02001115 }
1116}
1117
Jens Axboe53bd8db2012-03-14 21:51:38 +01001118/*
1119 * Send a command with a separate PDU, not inlined in the command
1120 */
1121static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1122 off_t size, uint64_t tag, uint32_t flags)
1123{
1124 struct fio_net_cmd cmd;
1125 struct iovec iov[2];
1126
1127 iov[0].iov_base = &cmd;
1128 iov[0].iov_len = sizeof(cmd);
1129 iov[1].iov_base = (void *) buf;
1130 iov[1].iov_len = size;
1131
1132 __fio_init_net_cmd(&cmd, opcode, size, tag);
1133 cmd.flags = __cpu_to_le32(flags);
1134 fio_net_cmd_crc_pdu(&cmd, buf);
1135
Jens Axboe8c953072012-03-20 14:35:35 +01001136 return fio_sendv_data(sk, iov, 2);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001137}
1138
Jens Axboe3989b142013-04-12 13:13:24 +02001139static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log)
Jens Axboe1b427252012-03-14 15:03:03 +01001140{
Jens Axboe3989b142013-04-12 13:13:24 +02001141 int ret = 0;
1142#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +01001143 z_stream stream;
1144 void *out_pdu;
Jens Axboe1b427252012-03-14 15:03:03 +01001145
1146 /*
1147 * Dirty - since the log is potentially huge, compress it into
1148 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1149 * side defragment it.
1150 */
1151 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1152
1153 stream.zalloc = Z_NULL;
1154 stream.zfree = Z_NULL;
1155 stream.opaque = Z_NULL;
1156
1157 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001158 ret = 1;
1159 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001160 }
1161
Jens Axboef5ed7652012-03-14 16:28:07 +01001162 stream.next_in = (void *) log->log;
1163 stream.avail_in = log->nr_samples * sizeof(struct io_sample);
Jens Axboe1b427252012-03-14 15:03:03 +01001164
1165 do {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001166 unsigned int this_len, flags = 0;
1167 int ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001168
1169 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1170 stream.next_out = out_pdu;
Jens Axboe3c547fe2012-03-14 21:53:00 +01001171 ret = deflate(&stream, Z_FINISH);
1172 /* may be Z_OK, or Z_STREAM_END */
1173 if (ret < 0)
1174 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001175
1176 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1177
Jens Axboe1b427252012-03-14 15:03:03 +01001178 if (stream.avail_in)
Jens Axboe53bd8db2012-03-14 21:51:38 +01001179 flags = FIO_NET_CMD_F_MORE;
Jens Axboe1b427252012-03-14 15:03:03 +01001180
Jens Axboe53bd8db2012-03-14 21:51:38 +01001181 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
1182 out_pdu, this_len, 0, flags);
1183 if (ret)
1184 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001185 } while (stream.avail_in);
1186
Jens Axboe53bd8db2012-03-14 21:51:38 +01001187err_zlib:
Jens Axboe1b427252012-03-14 15:03:03 +01001188 deflateEnd(&stream);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001189err:
1190 free(out_pdu);
Jens Axboe3989b142013-04-12 13:13:24 +02001191#endif
Jens Axboe53bd8db2012-03-14 21:51:38 +01001192 return ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001193}
1194
Jens Axboe3989b142013-04-12 13:13:24 +02001195int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1196{
1197 struct cmd_iolog_pdu pdu;
1198 int i, ret = 0;
1199
1200 pdu.thread_number = cpu_to_le32(td->thread_number);
1201 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
1202 pdu.log_type = cpu_to_le32(log->log_type);
1203 pdu.compressed = cpu_to_le32(use_zlib);
1204 strcpy((char *) pdu.name, name);
1205
1206 for (i = 0; i < log->nr_samples; i++) {
1207 struct io_sample *s = &log->log[i];
1208
1209 s->time = cpu_to_le64(s->time);
1210 s->val = cpu_to_le64(s->val);
1211 s->ddir = cpu_to_le32(s->ddir);
1212 s->bs = cpu_to_le32(s->bs);
1213 }
1214
1215 /*
1216 * Send header first, it's not compressed.
1217 */
1218 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1219 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1220 if (ret)
1221 return ret;
1222
1223 /*
1224 * Now send actual log, compress if we can, otherwise just plain
1225 */
1226 if (use_zlib)
1227 return fio_send_iolog_gz(&pdu, log);
1228
1229 return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log,
1230 log->nr_samples * sizeof(struct io_sample), 0, 0);
1231}
1232
Jens Axboe2f122b12012-03-15 13:10:19 +01001233void fio_server_send_add_job(struct thread_data *td)
Jens Axboe807f9972012-03-02 10:25:24 +01001234{
1235 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +01001236
Jens Axboe731e30a2012-03-14 16:35:37 +01001237 memset(&pdu, 0, sizeof(pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +01001238 pdu.thread_number = cpu_to_le32(td->thread_number);
1239 pdu.groupid = cpu_to_le32(td->groupid);
1240 convert_thread_options_to_net(&pdu.top, &td->o);
Jens Axboe807f9972012-03-02 10:25:24 +01001241
Jens Axboe40c60512012-03-27 16:03:04 +02001242 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboe807f9972012-03-02 10:25:24 +01001243}
1244
Jens Axboe122c7722012-03-19 09:13:15 +01001245void fio_server_send_start(struct thread_data *td)
1246{
1247 assert(server_fd != -1);
1248
1249 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1250}
1251
Jens Axboe87aa8f12011-10-06 21:24:13 +02001252static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +02001253{
Jens Axboe811826b2011-10-24 09:11:50 +02001254 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001255 socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001256 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +02001257
Jens Axboe811826b2011-10-24 09:11:50 +02001258 if (use_ipv6)
1259 sk = socket(AF_INET6, SOCK_STREAM, 0);
1260 else
1261 sk = socket(AF_INET, SOCK_STREAM, 0);
1262
Jens Axboe81179ee2011-10-04 12:42:06 +02001263 if (sk < 0) {
1264 log_err("fio: socket: %s\n", strerror(errno));
1265 return -1;
1266 }
1267
1268 opt = 1;
Jens Axboe1f819912013-01-23 17:21:41 -07001269 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001270 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001271 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001272 return -1;
1273 }
1274#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +02001275 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001276 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001277 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001278 return -1;
1279 }
1280#endif
1281
Jens Axboe811826b2011-10-24 09:11:50 +02001282 if (use_ipv6) {
1283 addr = (struct sockaddr *) &saddr_in6;
1284 socklen = sizeof(saddr_in6);
1285 saddr_in6.sin6_family = AF_INET6;
1286 } else {
1287 addr = (struct sockaddr *) &saddr_in;
1288 socklen = sizeof(saddr_in);
1289 saddr_in.sin_family = AF_INET;
1290 }
Jens Axboe81179ee2011-10-04 12:42:06 +02001291
Jens Axboe811826b2011-10-24 09:11:50 +02001292 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001293 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001294 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001295 return -1;
1296 }
1297
Jens Axboe87aa8f12011-10-06 21:24:13 +02001298 return sk;
1299}
1300
1301static int fio_init_server_sock(void)
1302{
1303 struct sockaddr_un addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001304 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001305 mode_t mode;
1306 int sk;
1307
1308 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1309 if (sk < 0) {
1310 log_err("fio: socket: %s\n", strerror(errno));
1311 return -1;
1312 }
1313
1314 mode = umask(000);
1315
1316 memset(&addr, 0, sizeof(addr));
1317 addr.sun_family = AF_UNIX;
1318 strcpy(addr.sun_path, bind_sock);
1319 unlink(bind_sock);
1320
1321 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1322
1323 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1324 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001325 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001326 return -1;
1327 }
1328
1329 umask(mode);
1330 return sk;
1331}
1332
1333static int fio_init_server_connection(void)
1334{
Jens Axboebebe6392011-10-07 10:00:51 +02001335 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001336 int sk;
1337
1338 dprint(FD_NET, "starting server\n");
1339
1340 if (!bind_sock)
1341 sk = fio_init_server_ip();
1342 else
1343 sk = fio_init_server_sock();
1344
1345 if (sk < 0)
1346 return sk;
1347
Jens Axboe811826b2011-10-24 09:11:50 +02001348 if (!bind_sock) {
1349 char *p, port[16];
1350 const void *src;
1351 int af;
1352
1353 if (use_ipv6) {
1354 af = AF_INET6;
1355 src = &saddr_in6.sin6_addr;
1356 } else {
1357 af = AF_INET;
1358 src = &saddr_in.sin_addr;
1359 }
1360
1361 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1362
1363 sprintf(port, ",%u", fio_net_port);
1364 if (p)
1365 strcat(p, port);
1366 else
1367 strcpy(bind_str, port);
1368 } else
Jens Axboebebe6392011-10-07 10:00:51 +02001369 strcpy(bind_str, bind_sock);
1370
1371 log_info("fio: server listening on %s\n", bind_str);
1372
Jens Axboe89c17072011-10-11 10:15:51 +02001373 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001374 log_err("fio: listen: %s\n", strerror(errno));
1375 return -1;
1376 }
1377
Jens Axboe87aa8f12011-10-06 21:24:13 +02001378 return sk;
1379}
1380
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001381int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1382 struct in6_addr *inp6)
1383
1384{
1385 int ret = 0;
1386
1387 if (*ipv6)
1388 ret = inet_pton(AF_INET6, host, inp6);
1389 else
1390 ret = inet_pton(AF_INET, host, inp);
1391
1392 if (ret != 1) {
1393 struct hostent *hent;
1394
1395 hent = gethostbyname(host);
1396 if (!hent) {
1397 log_err("fio: failed to resolve <%s>\n", host);
1398 return 0;
1399 }
1400
1401 if (*ipv6) {
1402 if (hent->h_addrtype != AF_INET6) {
1403 log_info("fio: falling back to IPv4\n");
1404 *ipv6 = 0;
1405 } else
1406 memcpy(inp6, hent->h_addr_list[0], 16);
1407 }
1408 if (!*ipv6) {
1409 if (hent->h_addrtype != AF_INET) {
1410 log_err("fio: lookup type mismatch\n");
1411 return 0;
1412 }
1413 memcpy(inp, hent->h_addr_list[0], 4);
1414 }
1415 ret = 1;
1416 }
1417
1418 return !(ret == 1);
1419}
1420
Jens Axboe660a2bf2011-10-24 09:35:06 +02001421/*
1422 * Parse a host/ip/port string. Reads from 'str'.
1423 *
1424 * Outputs:
1425 *
1426 * For IPv4:
1427 * *ptr is the host, *port is the port, inp is the destination.
1428 * For IPv6:
1429 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1430 * For local domain sockets:
1431 * *ptr is the filename, *is_sock is 1.
1432 */
Jens Axboebebe6392011-10-07 10:00:51 +02001433int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001434 int *port, struct in_addr *inp,
1435 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001436{
Jens Axboe76867622011-10-25 09:52:51 +02001437 const char *host = str;
1438 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001439 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001440
Jens Axboebebe6392011-10-07 10:00:51 +02001441 *ptr = NULL;
1442 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001443 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001444 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001445
1446 if (!strncmp(str, "sock:", 5)) {
1447 *ptr = strdup(str + 5);
1448 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001449
Jens Axboe76867622011-10-25 09:52:51 +02001450 return 0;
1451 }
1452
1453 /*
1454 * Is it ip:<ip or host>:port
1455 */
1456 if (!strncmp(host, "ip:", 3))
1457 host += 3;
1458 else if (!strncmp(host, "ip4:", 4))
1459 host += 4;
1460 else if (!strncmp(host, "ip6:", 4)) {
1461 host += 4;
1462 *ipv6 = 1;
1463 } else if (host[0] == ':') {
1464 /* String is :port */
1465 host++;
1466 lport = atoi(host);
1467 if (!lport || lport > 65535) {
1468 log_err("fio: bad server port %u\n", port);
1469 return 1;
1470 }
1471 /* no hostname given, we are done */
1472 *port = lport;
1473 return 0;
1474 }
1475
1476 /*
1477 * If no port seen yet, check if there's a last ':' at the end
1478 */
1479 if (!lport) {
1480 portp = strchr(host, ',');
1481 if (portp) {
1482 *portp = '\0';
1483 portp++;
1484 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001485 if (!lport || lport > 65535) {
1486 log_err("fio: bad server port %u\n", port);
1487 return 1;
1488 }
Jens Axboe76867622011-10-25 09:52:51 +02001489 }
1490 }
1491
1492 if (lport)
1493 *port = lport;
1494
1495 if (!strlen(host))
1496 return 0;
1497
1498 *ptr = strdup(host);
1499
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001500 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1501 free(*ptr);
1502 *ptr = NULL;
1503 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001504 }
1505
1506 if (*port == 0)
1507 *port = fio_net_port;
1508
1509 return 0;
1510}
1511
Jens Axboe87aa8f12011-10-06 21:24:13 +02001512/*
1513 * Server arg should be one of:
1514 *
1515 * sock:/path/to/socket
1516 * ip:1.2.3.4
1517 * 1.2.3.4
1518 *
1519 * Where sock uses unix domain sockets, and ip binds the server to
1520 * a specific interface. If no arguments are given to the server, it
1521 * uses IP and binds to 0.0.0.0.
1522 *
1523 */
1524static int fio_handle_server_arg(void)
1525{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001526 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001527 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001528
Jens Axboe87aa8f12011-10-06 21:24:13 +02001529 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1530
1531 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001532 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001533
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001534 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001535 &port, &saddr_in.sin_addr,
1536 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001537
1538 if (!is_sock && bind_sock) {
1539 free(bind_sock);
1540 bind_sock = NULL;
1541 }
1542
Jens Axboea7de0a12011-10-07 12:55:14 +02001543out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001544 fio_net_port = port;
1545 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001546 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001547 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001548}
1549
1550static int fio_server(void)
1551{
1552 int sk, ret;
1553
1554 dprint(FD_NET, "starting server\n");
1555
1556 if (fio_handle_server_arg())
1557 return -1;
1558
1559 sk = fio_init_server_connection();
1560 if (sk < 0)
1561 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001562
1563 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001564
Jens Axboe81179ee2011-10-04 12:42:06 +02001565 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001566
1567 if (fio_server_arg) {
1568 free(fio_server_arg);
1569 fio_server_arg = NULL;
1570 }
Jens Axboebebe6392011-10-07 10:00:51 +02001571 if (bind_sock)
1572 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001573
Jens Axboe81179ee2011-10-04 12:42:06 +02001574 return ret;
1575}
1576
Jens Axboe7b821682011-10-11 12:16:32 +02001577void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001578{
Jens Axboe7b821682011-10-11 12:16:32 +02001579 if (signal == SIGPIPE)
1580 server_fd = -1;
1581 else {
1582 log_info("\nfio: terminating on signal %d\n", signal);
1583 exit_backend = 1;
1584 }
Jens Axboe9abea482011-10-04 13:21:54 +02001585}
1586
Jens Axboe13755d92011-10-10 19:51:26 +02001587static int check_existing_pidfile(const char *pidfile)
1588{
1589 struct stat sb;
1590 char buf[16];
1591 pid_t pid;
1592 FILE *f;
1593
1594 if (stat(pidfile, &sb))
1595 return 0;
1596
1597 f = fopen(pidfile, "r");
1598 if (!f)
1599 return 0;
1600
Jens Axboebfc3b172011-10-10 21:16:55 +02001601 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001602 fclose(f);
1603 return 1;
1604 }
1605 fclose(f);
1606
1607 pid = atoi(buf);
1608 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001609 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001610
1611 return 1;
1612}
1613
1614static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001615{
1616 FILE *fpid;
1617
1618 fpid = fopen(pidfile, "w");
1619 if (!fpid) {
1620 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001621 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001622 }
1623
1624 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001625 fclose(fpid);
1626 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001627}
1628
1629/*
1630 * If pidfile is specified, background us.
1631 */
1632int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001633{
1634 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001635 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001636
Bruce Cran93bcfd22012-02-20 20:18:19 +01001637#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001638 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +02001639 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001640#endif
1641
Jens Axboe402668f2011-10-10 15:28:58 +02001642 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001643 return fio_server();
1644
Jens Axboe13755d92011-10-10 19:51:26 +02001645 if (check_existing_pidfile(pidfile)) {
1646 log_err("fio: pidfile %s exists and server appears alive\n",
1647 pidfile);
1648 return -1;
1649 }
1650
Jens Axboee46d8092011-10-03 09:11:02 +02001651 pid = fork();
1652 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001653 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001654 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001655 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001656 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001657 int ret = write_pid(pid, pidfile);
1658
1659 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001660 }
Jens Axboee46d8092011-10-03 09:11:02 +02001661
1662 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001663 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1664 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001665 close(STDIN_FILENO);
1666 close(STDOUT_FILENO);
1667 close(STDERR_FILENO);
1668 f_out = NULL;
1669 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001670
1671 ret = fio_server();
1672
1673 closelog();
1674 unlink(pidfile);
1675 free(pidfile);
1676 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001677}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001678
Jens Axboebebe6392011-10-07 10:00:51 +02001679void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001680{
1681 fio_server_arg = strdup(arg);
1682}