blob: 884ff8ef5d8c597ec6d9f674ff6b495bdd876d92 [file] [log] [blame]
Jens Axboe50d16972011-09-29 17:45:28 -06001#include <stdio.h>
2#include <stdlib.h>
Jens Axboe142575e2011-09-30 17:35:45 -06003#include <stdarg.h>
Jens Axboe50d16972011-09-29 17:45:28 -06004#include <unistd.h>
5#include <limits.h>
Jens Axboe50d16972011-09-29 17:45:28 -06006#include <errno.h>
7#include <fcntl.h>
8#include <sys/poll.h>
Jens Axboe50d16972011-09-29 17:45:28 -06009#include <sys/types.h>
10#include <sys/wait.h>
Jens Axboed05c4a02011-10-05 00:16:11 +020011#include <sys/socket.h>
Jens Axboe87aa8f12011-10-06 21:24:13 +020012#include <sys/stat.h>
13#include <sys/un.h>
Bruce Cran1f51bba2013-04-12 15:40:21 +020014#include <sys/uio.h>
Jens Axboe50d16972011-09-29 17:45:28 -060015#include <netinet/in.h>
16#include <arpa/inet.h>
17#include <netdb.h>
Jens Axboee46d8092011-10-03 09:11:02 +020018#include <syslog.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020019#include <signal.h>
Jens Axboe3989b142013-04-12 13:13:24 +020020#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +010021#include <zlib.h>
Jens Axboe3989b142013-04-12 13:13:24 +020022#endif
Jens Axboe50d16972011-09-29 17:45:28 -060023
24#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060025#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060026#include "crc/crc16.h"
Jens Axboec7c6cb42011-10-13 14:12:40 +020027#include "lib/ieee754.h"
Jens Axboe50d16972011-09-29 17:45:28 -060028
Stephen M. Cameron5adc2442012-02-24 08:17:31 +010029int fio_net_port = FIO_NET_PORT;
Jens Axboe50d16972011-09-29 17:45:28 -060030
Jens Axboe009b1be2011-09-29 18:27:02 -060031int exit_backend = 0;
32
Jens Axboe46c48f12011-10-01 12:50:51 -060033static int server_fd = -1;
Jens Axboe87aa8f12011-10-06 21:24:13 +020034static char *fio_server_arg;
35static char *bind_sock;
36static struct sockaddr_in saddr_in;
Jens Axboe811826b2011-10-24 09:11:50 +020037static struct sockaddr_in6 saddr_in6;
Jens Axboe811826b2011-10-24 09:11:50 +020038static int use_ipv6;
Jens Axboe3989b142013-04-12 13:13:24 +020039#ifdef CONFIG_ZLIB
40static unsigned int has_zlib = 1;
41#else
42static unsigned int has_zlib = 0;
43#endif
44static unsigned int use_zlib;
Jens Axboe37db14f2011-09-30 17:00:42 -060045
Jens Axboe122c7722012-03-19 09:13:15 +010046struct fio_fork_item {
47 struct flist_head list;
48 int exitval;
49 int signal;
50 int exited;
51 pid_t pid;
52};
53
54/* Created on fork on new connection */
55static FLIST_HEAD(conn_list);
56
57/* Created on job fork from connection */
58static FLIST_HEAD(job_list);
59
Jens Axboe89c17072011-10-11 10:15:51 +020060static const char *fio_server_ops[FIO_NET_CMD_NR] = {
61 "",
62 "QUIT",
63 "EXIT",
64 "JOB",
65 "JOBLINE",
66 "TEXT",
67 "TS",
68 "GS",
69 "SEND_ETA",
70 "ETA",
71 "PROBE",
72 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020073 "STOP",
74 "DISK_UTIL",
Jens Axboeb9d2f302012-03-08 20:36:28 +010075 "SERVER_START",
Jens Axboe807f9972012-03-02 10:25:24 +010076 "ADD_JOB",
Jens Axboeb9d2f302012-03-08 20:36:28 +010077 "CMD_RUN"
Jens Axboec70e63e2012-03-15 08:26:18 +010078 "CMD_IOLOG",
Jens Axboe89c17072011-10-11 10:15:51 +020079};
80
81const char *fio_server_op(unsigned int op)
82{
83 static char buf[32];
84
85 if (op < FIO_NET_CMD_NR)
86 return fio_server_ops[op];
87
88 sprintf(buf, "UNKNOWN/%d", op);
89 return buf;
90}
91
Jens Axboe5235f622012-03-14 21:25:51 +010092static ssize_t iov_total_len(const struct iovec *iov, int count)
Jens Axboe132159a2011-09-30 15:01:32 -060093{
Jens Axboe5235f622012-03-14 21:25:51 +010094 ssize_t ret = 0;
95
96 while (count--) {
97 ret += iov->iov_len;
98 iov++;
99 }
100
101 return ret;
102}
103
104static int fio_sendv_data(int sk, struct iovec *iov, int count)
105{
106 ssize_t total_len = iov_total_len(iov, count);
107 ssize_t ret;
Jens Axboe794d69c2011-10-01 08:48:50 -0600108
Jens Axboe132159a2011-09-30 15:01:32 -0600109 do {
Jens Axboe5235f622012-03-14 21:25:51 +0100110 ret = writev(sk, iov, count);
Jens Axboe132159a2011-09-30 15:01:32 -0600111 if (ret > 0) {
Jens Axboe5235f622012-03-14 21:25:51 +0100112 total_len -= ret;
113 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600114 break;
Jens Axboe5235f622012-03-14 21:25:51 +0100115
116 while (ret) {
117 if (ret >= iov->iov_len) {
118 ret -= iov->iov_len;
119 iov++;
120 continue;
121 }
122 iov->iov_base += ret;
123 iov->iov_len -= ret;
124 ret = 0;
125 }
Jens Axboe132159a2011-09-30 15:01:32 -0600126 } else if (!ret)
127 break;
128 else if (errno == EAGAIN || errno == EINTR)
129 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200130 else
131 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600132 } while (!exit_backend);
133
Jens Axboe5235f622012-03-14 21:25:51 +0100134 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600135 return 0;
136
Jens Axboe8b4e61b2012-03-09 17:10:54 +0100137 if (errno)
138 return -errno;
139
Jens Axboe132159a2011-09-30 15:01:32 -0600140 return 1;
141}
142
Jens Axboe5235f622012-03-14 21:25:51 +0100143int fio_send_data(int sk, const void *p, unsigned int len)
144{
145 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
146
147 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
148
149 return fio_sendv_data(sk, &iov, 1);
150}
151
Jens Axboe132159a2011-09-30 15:01:32 -0600152int fio_recv_data(int sk, void *p, unsigned int len)
153{
154 do {
155 int ret = recv(sk, p, len, MSG_WAITALL);
156
157 if (ret > 0) {
158 len -= ret;
159 if (!len)
160 break;
161 p += ret;
162 continue;
163 } else if (!ret)
164 break;
165 else if (errno == EAGAIN || errno == EINTR)
166 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200167 else
168 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600169 } while (!exit_backend);
170
171 if (!len)
172 return 0;
173
174 return -1;
175}
176
177static int verify_convert_cmd(struct fio_net_cmd *cmd)
178{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600179 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600180
Jens Axboefcee5ff2011-09-30 22:50:39 -0600181 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
182 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600183
Jens Axboe25dfa842012-02-29 10:01:34 +0100184 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600185 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600186 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600187 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600188 return 1;
189 }
190
191 cmd->version = le16_to_cpu(cmd->version);
192 cmd->opcode = le16_to_cpu(cmd->opcode);
193 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200194 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600195 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
196
197 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200198 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600199 break;
200 default:
201 log_err("fio: bad server cmd version %d\n", cmd->version);
202 return 1;
203 }
204
Jens Axboeb9d2f302012-03-08 20:36:28 +0100205 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600206 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
207 return 1;
208 }
209
210 return 0;
211}
212
Jens Axboea64e88d2011-10-03 14:20:01 +0200213/*
214 * Read (and defragment, if necessary) incoming commands
215 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200216struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600217{
Jens Axboea64e88d2011-10-03 14:20:01 +0200218 struct fio_net_cmd cmd, *cmdret = NULL;
219 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600220 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200221 int ret, first = 1;
222 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600223
Jens Axboea64e88d2011-10-03 14:20:01 +0200224 do {
225 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
226 if (ret)
227 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600228
Jens Axboea64e88d2011-10-03 14:20:01 +0200229 /* We have a command, verify it and swap if need be */
230 ret = verify_convert_cmd(&cmd);
231 if (ret)
232 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600233
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200234 if (first) {
235 /* if this is text, add room for \0 at the end */
236 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
237 assert(!cmdret);
238 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200239 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600240
Jens Axboea64e88d2011-10-03 14:20:01 +0200241 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600242
Jens Axboea64e88d2011-10-03 14:20:01 +0200243 if (first)
244 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200245 else if (cmdret->opcode != cmd.opcode) {
246 log_err("fio: fragment opcode mismatch (%d != %d)\n",
247 cmdret->opcode, cmd.opcode);
248 ret = 1;
249 break;
250 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200251
252 if (!cmd.pdu_len)
253 break;
254
255 /* There's payload, get it */
256 pdu = (void *) cmdret->payload + pdu_offset;
257 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
258 if (ret)
259 break;
260
261 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100262 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200263 if (crc != cmd.pdu_crc16) {
264 log_err("fio: server bad crc on payload ");
265 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
266 ret = 1;
267 break;
268 }
269
270 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200271 if (!first)
272 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200273 first = 0;
274 } while (cmd.flags & FIO_NET_CMD_F_MORE);
275
276 if (ret) {
277 free(cmdret);
278 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200279 } else if (cmdret) {
280 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100281 if (cmdret->pdu_len) {
282 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
283 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
284 char *buf = (char *) pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200285
Jens Axboe3c3ed072012-03-27 09:12:39 +0200286 buf[pdu->buf_len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100287 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
Jens Axboe46bcd492012-03-14 11:31:21 +0100288 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
289 char *buf = (char *) pdu->buf;
290 int len = le32_to_cpu(pdu->buf_len);
Jens Axboe084d1c62012-03-03 20:28:07 +0100291
Jens Axboe46bcd492012-03-14 11:31:21 +0100292 buf[len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100293 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200294 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100295
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200296 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200297 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200298 }
Jens Axboe132159a2011-09-30 15:01:32 -0600299
Jens Axboea64e88d2011-10-03 14:20:01 +0200300 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600301}
302
Jens Axboe40c60512012-03-27 16:03:04 +0200303static void add_reply(uint64_t tag, struct flist_head *list)
304{
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200305 struct fio_net_cmd_reply *reply;
Jens Axboe40c60512012-03-27 16:03:04 +0200306
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200307 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
Jens Axboe40c60512012-03-27 16:03:04 +0200308 flist_add_tail(&reply->list, list);
309}
310
311static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
312{
313 struct fio_net_cmd_reply *reply;
314
315 reply = calloc(1, sizeof(*reply));
316 INIT_FLIST_HEAD(&reply->list);
317 gettimeofday(&reply->tv, NULL);
318 reply->saved_tag = tag;
319 reply->opcode = opcode;
320
321 return (uintptr_t) reply;
322}
323
324static void free_reply(uint64_t tag)
325{
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200326 struct fio_net_cmd_reply *reply;
Jens Axboe40c60512012-03-27 16:03:04 +0200327
Aaron Carrolla572bbf2013-04-12 07:55:02 +0200328 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
Jens Axboe40c60512012-03-27 16:03:04 +0200329 free(reply);
330}
331
Jens Axboe53bd8db2012-03-14 21:51:38 +0100332void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
Jens Axboe132159a2011-09-30 15:01:32 -0600333{
334 uint32_t pdu_len;
335
Jens Axboe25dfa842012-02-29 10:01:34 +0100336 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600337
338 pdu_len = le32_to_cpu(cmd->pdu_len);
Jens Axboe1b427252012-03-14 15:03:03 +0100339 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
340}
341
342void fio_net_cmd_crc(struct fio_net_cmd *cmd)
343{
344 fio_net_cmd_crc_pdu(cmd, cmd->payload);
Jens Axboe132159a2011-09-30 15:01:32 -0600345}
346
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200347int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
Jens Axboe40c60512012-03-27 16:03:04 +0200348 uint64_t *tagptr, struct flist_head *list)
Jens Axboe794d69c2011-10-01 08:48:50 -0600349{
Jens Axboe7f868312011-10-10 09:55:21 +0200350 struct fio_net_cmd *cmd = NULL;
351 size_t this_len, cur_len = 0;
Jens Axboe40c60512012-03-27 16:03:04 +0200352 uint64_t tag;
Jens Axboe794d69c2011-10-01 08:48:50 -0600353 int ret;
354
Jens Axboe40c60512012-03-27 16:03:04 +0200355 if (list) {
356 assert(tagptr);
357 tag = *tagptr = alloc_reply(*tagptr, opcode);
358 } else
359 tag = tagptr ? *tagptr : 0;
360
Jens Axboe794d69c2011-10-01 08:48:50 -0600361 do {
362 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100363 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
364 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600365
Jens Axboe7f868312011-10-10 09:55:21 +0200366 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
367 if (cmd)
368 free(cmd);
369
370 cur_len = sizeof(*cmd) + this_len;
371 cmd = malloc(cur_len);
372 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600373
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200374 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600375
376 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200377 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600378
379 fio_net_cmd_crc(cmd);
380
381 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600382 size -= this_len;
383 buf += this_len;
384 } while (!ret && size);
385
Jens Axboe40c60512012-03-27 16:03:04 +0200386 if (list) {
387 if (ret)
388 free_reply(tag);
389 else
390 add_reply(tag, list);
391 }
392
Jens Axboe7f868312011-10-10 09:55:21 +0200393 if (cmd)
394 free(cmd);
395
Jens Axboe794d69c2011-10-01 08:48:50 -0600396 return ret;
397}
398
Jens Axboe89c17072011-10-11 10:15:51 +0200399static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600400{
Jens Axboe178cde92011-10-05 22:14:31 +0200401 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600402
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200403 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600404 fio_net_cmd_crc(&cmd);
405
406 return fio_send_data(sk, &cmd, sizeof(cmd));
407}
408
Jens Axboe89c17072011-10-11 10:15:51 +0200409/*
410 * If 'list' is non-NULL, then allocate and store the sent command for
411 * later verification.
412 */
413int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
414 struct flist_head *list)
415{
Jens Axboe89c17072011-10-11 10:15:51 +0200416 int ret;
417
Jens Axboe40c60512012-03-27 16:03:04 +0200418 if (list)
419 tag = alloc_reply(tag, opcode);
Jens Axboe89c17072011-10-11 10:15:51 +0200420
Jens Axboe40c60512012-03-27 16:03:04 +0200421 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
Jens Axboe89c17072011-10-11 10:15:51 +0200422 if (ret) {
Jens Axboe40c60512012-03-27 16:03:04 +0200423 if (list)
424 free_reply(tag);
425
Jens Axboe89c17072011-10-11 10:15:51 +0200426 return ret;
427 }
428
Jens Axboe40c60512012-03-27 16:03:04 +0200429 if (list)
430 add_reply(tag, list);
431
Jens Axboe89c17072011-10-11 10:15:51 +0200432 return 0;
433}
434
Jens Axboe122c7722012-03-19 09:13:15 +0100435int fio_net_send_quit(int sk)
Jens Axboe437377e2011-10-01 08:31:30 -0600436{
Jens Axboe46c48f12011-10-01 12:50:51 -0600437 dprint(FD_NET, "server: sending quit\n");
Jens Axboe122c7722012-03-19 09:13:15 +0100438
Jens Axboe8c953072012-03-20 14:35:35 +0100439 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600440}
441
Jens Axboef58bd2a2012-03-27 10:06:42 +0200442static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error,
443 int signal)
Jens Axboe122c7722012-03-19 09:13:15 +0100444{
445 struct cmd_end_pdu epdu;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200446 uint64_t tag = 0;
Jens Axboe122c7722012-03-19 09:13:15 +0100447
Jens Axboef58bd2a2012-03-27 10:06:42 +0200448 if (cmd)
449 tag = cmd->tag;
Jens Axboe122c7722012-03-19 09:13:15 +0100450
451 epdu.error = __cpu_to_le32(error);
452 epdu.signal = __cpu_to_le32(signal);
Jens Axboe40c60512012-03-27 16:03:04 +0200453 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200454}
455
456int fio_net_send_stop(int sk, int error, int signal)
457{
458 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
459 return fio_net_send_ack(sk, NULL, error, signal);
Jens Axboe122c7722012-03-19 09:13:15 +0100460}
461
462static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
463{
464 struct fio_fork_item *ffi;
465
466 ffi = malloc(sizeof(*ffi));
467 ffi->exitval = 0;
468 ffi->signal = 0;
469 ffi->exited = 0;
470 ffi->pid = pid;
471 flist_add_tail(&ffi->list, list);
472}
473
474static void fio_server_add_conn_pid(pid_t pid)
475{
476 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", pid);
477 fio_server_add_fork_item(pid, &conn_list);
478}
479
480static void fio_server_add_job_pid(pid_t pid)
481{
482 dprint(FD_NET, "server: forked off job job (pid=%u)\n", pid);
483 fio_server_add_fork_item(pid, &job_list);
484}
485
486static void fio_server_check_fork_item(struct fio_fork_item *ffi)
487{
488 int ret, status;
489
490 ret = waitpid(ffi->pid, &status, WNOHANG);
491 if (ret < 0) {
492 if (errno == ECHILD) {
493 log_err("fio: connection pid %u disappeared\n", ffi->pid);
494 ffi->exited = 1;
495 } else
496 log_err("fio: waitpid: %s\n", strerror(errno));
497 } else if (ret == ffi->pid) {
498 if (WIFSIGNALED(status)) {
499 ffi->signal = WTERMSIG(status);
500 ffi->exited = 1;
501 }
502 if (WIFEXITED(status)) {
503 if (WEXITSTATUS(status))
504 ffi->exitval = WEXITSTATUS(status);
505 ffi->exited = 1;
506 }
507 }
508}
509
510static void fio_server_fork_item_done(struct fio_fork_item *ffi)
511{
512 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", ffi->pid, ffi->signal, ffi->exitval);
513
514 /*
515 * Fold STOP and QUIT...
516 */
517 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
518 fio_net_send_quit(server_fd);
519 flist_del(&ffi->list);
520 free(ffi);
521}
522
Jens Axboe54163252012-03-23 12:25:18 +0100523static void fio_server_check_fork_items(struct flist_head *list)
Jens Axboe122c7722012-03-19 09:13:15 +0100524{
525 struct flist_head *entry, *tmp;
526 struct fio_fork_item *ffi;
527
528 flist_for_each_safe(entry, tmp, list) {
529 ffi = flist_entry(entry, struct fio_fork_item, list);
530
531 fio_server_check_fork_item(ffi);
532
533 if (ffi->exited)
534 fio_server_fork_item_done(ffi);
535 }
536}
537
538static void fio_server_check_jobs(void)
539{
Jens Axboe54163252012-03-23 12:25:18 +0100540 fio_server_check_fork_items(&job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100541}
542
543static void fio_server_check_conns(void)
544{
Jens Axboe54163252012-03-23 12:25:18 +0100545 fio_server_check_fork_items(&conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100546}
547
Jens Axboeb9d2f302012-03-08 20:36:28 +0100548static int handle_run_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600549{
Jens Axboe122c7722012-03-19 09:13:15 +0100550 pid_t pid;
Jens Axboea64e88d2011-10-03 14:20:01 +0200551 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600552
Jens Axboe122c7722012-03-19 09:13:15 +0100553 set_genesis_time();
554
555 pid = fork();
556 if (pid) {
557 fio_server_add_job_pid(pid);
558 return 0;
559 }
560
Jens Axboe2e1df072012-02-09 11:15:02 +0100561 ret = fio_backend();
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200562 free_threads_shm();
Jens Axboe122c7722012-03-19 09:13:15 +0100563 _exit(ret);
Jens Axboe81179ee2011-10-04 12:42:06 +0200564}
565
Jens Axboeb9d2f302012-03-08 20:36:28 +0100566static int handle_job_cmd(struct fio_net_cmd *cmd)
567{
Jens Axboe46bcd492012-03-14 11:31:21 +0100568 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
569 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100570 struct cmd_start_pdu spdu;
571
Jens Axboe46bcd492012-03-14 11:31:21 +0100572 pdu->buf_len = le32_to_cpu(pdu->buf_len);
573 pdu->client_type = le32_to_cpu(pdu->client_type);
574
575 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100576 fio_net_send_quit(server_fd);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100577 return -1;
578 }
579
580 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe108fea72012-11-14 13:09:45 -0700581 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200582 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100583 return 0;
584}
585
Jens Axboe81179ee2011-10-04 12:42:06 +0200586static int handle_jobline_cmd(struct fio_net_cmd *cmd)
587{
Jens Axboefa2ea802011-10-08 21:07:29 +0200588 void *pdu = cmd->payload;
589 struct cmd_single_line_pdu *cslp;
590 struct cmd_line_pdu *clp;
591 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100592 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200593 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100594 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200595
Jens Axboefa2ea802011-10-08 21:07:29 +0200596 clp = pdu;
597 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100598 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200599 argv = malloc(clp->lines * sizeof(char *));
600 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200601
Jens Axboefa2ea802011-10-08 21:07:29 +0200602 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200603
Jens Axboefa2ea802011-10-08 21:07:29 +0200604 for (i = 0; i < clp->lines; i++) {
605 cslp = pdu + offset;
606 argv[i] = (char *) cslp->text;
607
608 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200609 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
610 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200611
Jens Axboe46bcd492012-03-14 11:31:21 +0100612 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100613 fio_net_send_quit(server_fd);
Jens Axboefa2ea802011-10-08 21:07:29 +0200614 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200615 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200616 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200617
Jens Axboefa2ea802011-10-08 21:07:29 +0200618 free(argv);
619
Jens Axboeb9d2f302012-03-08 20:36:28 +0100620 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe1e5324e2012-11-14 14:25:31 -0700621 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200622 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100623 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600624}
625
Jens Axboec28e8e82011-10-04 08:57:39 +0200626static int handle_probe_cmd(struct fio_net_cmd *cmd)
627{
Jens Axboe3989b142013-04-12 13:13:24 +0200628 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
629 struct cmd_probe_reply_pdu probe;
Jens Axboe40c60512012-03-27 16:03:04 +0200630 uint64_t tag = cmd->tag;
Jens Axboec28e8e82011-10-04 08:57:39 +0200631
Jens Axboe89c17072011-10-11 10:15:51 +0200632 dprint(FD_NET, "server: sending probe reply\n");
633
Jens Axboec28e8e82011-10-04 08:57:39 +0200634 memset(&probe, 0, sizeof(probe));
635 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe0dcebdf2013-01-23 15:42:16 -0700636#ifdef CONFIG_BIG_ENDIAN
Jens Axboe6eb24792011-10-04 15:00:30 +0200637 probe.bigendian = 1;
638#endif
Jens Axboe750db472012-04-16 11:44:48 +0200639 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
Jens Axboec28e8e82011-10-04 08:57:39 +0200640
Jens Axboecca84642011-10-07 12:47:57 +0200641 probe.os = FIO_OS;
642 probe.arch = FIO_ARCH;
Jens Axboe38fdef22011-10-11 14:30:06 +0200643 probe.bpp = sizeof(void *);
Jens Axboed31e26d2012-03-28 21:38:24 +0200644 probe.cpus = __cpu_to_le32(cpus_online());
Jens Axboe3989b142013-04-12 13:13:24 +0200645
646 /*
647 * If the client supports compression and we do too, then enable it
648 */
649 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
650 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
651 use_zlib = 1;
652 } else {
653 probe.flags = 0;
654 use_zlib = 0;
655 }
Jens Axboe38fdef22011-10-11 14:30:06 +0200656
Jens Axboe40c60512012-03-27 16:03:04 +0200657 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200658}
659
660static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
661{
662 struct jobs_eta *je;
663 size_t size;
Jens Axboe40c60512012-03-27 16:03:04 +0200664 uint64_t tag = cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200665 int i;
666
Jens Axboeb814fb22011-10-12 09:20:34 +0200667 if (!thread_number)
668 return 0;
669
670 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200671 je = malloc(size);
672 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200673
674 if (!calc_thread_status(je, 1)) {
675 free(je);
676 return 0;
677 }
678
679 dprint(FD_NET, "server sending status\n");
680
681 je->nr_running = cpu_to_le32(je->nr_running);
682 je->nr_ramp = cpu_to_le32(je->nr_ramp);
683 je->nr_pending = cpu_to_le32(je->nr_pending);
684 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200685
Jens Axboe298921d2012-09-24 18:47:01 +0200686 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100687 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
688 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
689 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
690 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboe16725bb2013-04-11 12:43:05 +0200691 je->rate[i] = cpu_to_le32(je->rate[i]);
692 je->iops[i] = cpu_to_le32(je->iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200693 }
694
Jens Axboec1970b92012-03-06 15:37:40 +0100695 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200696 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100697 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200698 je->is_pow2 = cpu_to_le32(je->is_pow2);
Jens Axboeed2c0a12013-04-11 12:55:16 +0200699 je->unit_base = cpu_to_le32(je->unit_base);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200700
Jens Axboe40c60512012-03-27 16:03:04 +0200701 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200702 free(je);
703 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200704}
705
Jens Axboe40c60512012-03-27 16:03:04 +0200706static int send_update_job_reply(int fd, uint64_t __tag, int error)
707{
708 uint64_t tag = __tag;
709 uint32_t pdu_error;
710
711 pdu_error = __cpu_to_le32(error);
712 return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL);
713}
714
Jens Axboef58bd2a2012-03-27 10:06:42 +0200715static int handle_update_job_cmd(struct fio_net_cmd *cmd)
716{
717 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
718 struct thread_data *td;
719 uint32_t tnumber;
720
721 tnumber = le32_to_cpu(pdu->thread_number);
722
723 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
724
Jens Axboe30ffacb2012-03-28 09:15:05 +0200725 if (!tnumber || tnumber > thread_number) {
Jens Axboe40c60512012-03-27 16:03:04 +0200726 send_update_job_reply(server_fd, cmd->tag, ENODEV);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200727 return 0;
728 }
729
Jens Axboe30ffacb2012-03-28 09:15:05 +0200730 td = &threads[tnumber - 1];
Jens Axboef58bd2a2012-03-27 10:06:42 +0200731 convert_thread_options_to_cpu(&td->o, &pdu->top);
Jens Axboe40c60512012-03-27 16:03:04 +0200732 send_update_job_reply(server_fd, cmd->tag, 0);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200733 return 0;
734}
735
Jens Axboe132159a2011-09-30 15:01:32 -0600736static int handle_command(struct fio_net_cmd *cmd)
737{
738 int ret;
739
Jens Axboe4b91ee82013-02-25 10:18:33 +0100740 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
741 fio_server_op(cmd->opcode), cmd->pdu_len,
742 (unsigned long long) cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600743
Jens Axboe132159a2011-09-30 15:01:32 -0600744 switch (cmd->opcode) {
745 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200746 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200747 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200748 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600749 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200750 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600751 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200752 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600753 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200754 case FIO_NET_CMD_JOBLINE:
755 ret = handle_jobline_cmd(cmd);
756 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200757 case FIO_NET_CMD_PROBE:
758 ret = handle_probe_cmd(cmd);
759 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200760 case FIO_NET_CMD_SEND_ETA:
761 ret = handle_send_eta_cmd(cmd);
762 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100763 case FIO_NET_CMD_RUN:
764 ret = handle_run_cmd(cmd);
765 break;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200766 case FIO_NET_CMD_UPDATE_JOB:
767 ret = handle_update_job_cmd(cmd);
768 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600769 default:
Jens Axboe3c3ed072012-03-27 09:12:39 +0200770 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600771 ret = 1;
772 }
773
774 return ret;
775}
776
Jens Axboe122c7722012-03-19 09:13:15 +0100777static int handle_connection(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600778{
779 struct fio_net_cmd *cmd = NULL;
780 int ret = 0;
781
Jens Axboe122c7722012-03-19 09:13:15 +0100782 reset_fio_state();
783 INIT_FLIST_HEAD(&job_list);
784 server_fd = sk;
785
Jens Axboe132159a2011-09-30 15:01:32 -0600786 /* read forever */
787 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200788 struct pollfd pfd = {
789 .fd = sk,
790 .events = POLLIN,
791 };
792
793 ret = 0;
794 do {
Jens Axboe54163252012-03-23 12:25:18 +0100795 int timeout = 1000;
796
797 if (!flist_empty(&job_list))
798 timeout = 100;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200799
Jens Axboe54163252012-03-23 12:25:18 +0100800 ret = poll(&pfd, 1, timeout);
Jens Axboee951bdc2011-10-05 21:58:45 +0200801 if (ret < 0) {
802 if (errno == EINTR)
803 break;
804 log_err("fio: poll: %s\n", strerror(errno));
805 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200806 } else if (!ret) {
Jens Axboe122c7722012-03-19 09:13:15 +0100807 fio_server_check_jobs();
Jens Axboee951bdc2011-10-05 21:58:45 +0200808 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200809 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200810
811 if (pfd.revents & POLLIN)
812 break;
813 if (pfd.revents & (POLLERR|POLLHUP)) {
814 ret = 1;
815 break;
816 }
Jens Axboe19c65172011-10-05 22:05:37 +0200817 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200818
Jens Axboe122c7722012-03-19 09:13:15 +0100819 fio_server_check_jobs();
820
Jens Axboee951bdc2011-10-05 21:58:45 +0200821 if (ret < 0)
822 break;
823
824 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600825 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200826 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600827 break;
828 }
829
Jens Axboe132159a2011-09-30 15:01:32 -0600830 ret = handle_command(cmd);
831 if (ret)
832 break;
833
834 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400835 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600836 }
837
838 if (cmd)
839 free(cmd);
840
Jens Axboe122c7722012-03-19 09:13:15 +0100841 close(sk);
842 _exit(ret);
Jens Axboecc0df002011-10-03 20:53:32 +0200843}
844
Jens Axboe50d16972011-09-29 17:45:28 -0600845static int accept_loop(int listen_sk)
846{
Jens Axboebb447a22011-10-04 15:06:42 +0200847 struct sockaddr_in addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100848 socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600849 struct pollfd pfd;
Jens Axboe122c7722012-03-19 09:13:15 +0100850 int ret = 0, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600851
Jens Axboe60efd142011-10-04 13:30:11 +0200852 dprint(FD_NET, "server enter accept loop\n");
853
Jens Axboe009b1be2011-09-29 18:27:02 -0600854 flags = fcntl(listen_sk, F_GETFL);
855 flags |= O_NONBLOCK;
856 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe122c7722012-03-19 09:13:15 +0100857
858 while (!exit_backend) {
859 pid_t pid;
860
861 pfd.fd = listen_sk;
862 pfd.events = POLLIN;
863 do {
Jens Axboe54163252012-03-23 12:25:18 +0100864 int timeout = 1000;
865
866 if (!flist_empty(&conn_list))
867 timeout = 100;
868
869 ret = poll(&pfd, 1, timeout);
Jens Axboe122c7722012-03-19 09:13:15 +0100870 if (ret < 0) {
871 if (errno == EINTR)
872 break;
873 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600874 break;
Jens Axboe122c7722012-03-19 09:13:15 +0100875 } else if (!ret) {
876 fio_server_check_conns();
877 continue;
878 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600879
Jens Axboe122c7722012-03-19 09:13:15 +0100880 if (pfd.revents & POLLIN)
881 break;
882 } while (!exit_backend);
883
884 fio_server_check_conns();
885
886 if (exit_backend || ret < 0)
Jens Axboe009b1be2011-09-29 18:27:02 -0600887 break;
Jens Axboe009b1be2011-09-29 18:27:02 -0600888
Jens Axboe122c7722012-03-19 09:13:15 +0100889 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
890 if (sk < 0) {
891 log_err("fio: accept: %s\n", strerror(errno));
892 return -1;
893 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600894
Jens Axboe122c7722012-03-19 09:13:15 +0100895 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
896
897 pid = fork();
898 if (pid) {
899 close(sk);
900 fio_server_add_conn_pid(pid);
901 continue;
902 }
903
904 /* exits */
905 handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -0600906 }
907
Jens Axboe132159a2011-09-30 15:01:32 -0600908 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600909}
910
Jens Axboe084d1c62012-03-03 20:28:07 +0100911int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600912{
Jens Axboe084d1c62012-03-03 20:28:07 +0100913 struct cmd_text_pdu *pdu;
914 unsigned int tlen;
915 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600916
Jens Axboe084d1c62012-03-03 20:28:07 +0100917 if (server_fd == -1)
918 return log_local_buf(buf, len);
919
920 tlen = sizeof(*pdu) + len;
921 pdu = malloc(tlen);
922
923 pdu->level = __cpu_to_le32(level);
924 pdu->buf_len = __cpu_to_le32(len);
925
926 gettimeofday(&tv, NULL);
927 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
928 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
929
930 memcpy(pdu->buf, buf, len);
931
Jens Axboe40c60512012-03-27 16:03:04 +0200932 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL);
Jens Axboe084d1c62012-03-03 20:28:07 +0100933 free(pdu);
934 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600935}
936
Jens Axboea64e88d2011-10-03 14:20:01 +0200937static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
938{
939 dst->max_val = cpu_to_le64(src->max_val);
940 dst->min_val = cpu_to_le64(src->min_val);
941 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200942
943 /*
944 * Encode to IEEE 754 for network transfer
945 */
946 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
947 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200948}
949
950static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
951{
952 int i;
953
Jens Axboe298921d2012-09-24 18:47:01 +0200954 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200955 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
956 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
957 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
958 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
959 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
960 dst->agg[i] = cpu_to_le64(src->agg[i]);
961 }
962
963 dst->kb_base = cpu_to_le32(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700964 dst->unit_base = cpu_to_le32(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200965 dst->groupid = cpu_to_le32(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +0100966 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200967}
968
969/*
970 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
971 * into a single payload.
972 */
973void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
974{
975 struct cmd_ts_pdu p;
976 int i, j;
977
Jens Axboe60efd142011-10-04 13:30:11 +0200978 dprint(FD_NET, "server sending end stats\n");
979
Jens Axboe317b3c82011-10-03 21:47:27 +0200980 memset(&p, 0, sizeof(p));
981
Jens Axboea64e88d2011-10-03 14:20:01 +0200982 strcpy(p.ts.name, ts->name);
983 strcpy(p.ts.verror, ts->verror);
984 strcpy(p.ts.description, ts->description);
985
Jens Axboe2f122b12012-03-15 13:10:19 +0100986 p.ts.error = cpu_to_le32(ts->error);
987 p.ts.thread_number = cpu_to_le32(ts->thread_number);
988 p.ts.groupid = cpu_to_le32(ts->groupid);
989 p.ts.pid = cpu_to_le32(ts->pid);
990 p.ts.members = cpu_to_le32(ts->members);
Jens Axboe771e58b2013-01-30 12:56:23 +0100991 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200992
Jens Axboe298921d2012-09-24 18:47:01 +0200993 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200994 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
995 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
996 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
997 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
998 }
999
1000 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1001 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1002 p.ts.ctx = cpu_to_le64(ts->ctx);
1003 p.ts.minf = cpu_to_le64(ts->minf);
1004 p.ts.majf = cpu_to_le64(ts->majf);
1005 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +02001006
1007 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +02001008 fio_fp64_t *src = &ts->percentile_list[i];
1009 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +02001010
Jens Axboecfc03e42011-10-12 21:20:42 +02001011 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +02001012 }
Jens Axboea64e88d2011-10-03 14:20:01 +02001013
1014 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1015 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1016 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1017 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1018 }
1019
1020 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1021 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1022 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1023 }
1024
Jens Axboe298921d2012-09-24 18:47:01 +02001025 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +02001026 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1027 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1028
Jens Axboe78799de2013-01-29 20:53:52 +01001029 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001030 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +02001031 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +02001032 }
1033
Jens Axboe93eee042011-10-03 21:08:48 +02001034 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +02001035 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1036
Jens Axboe298921d2012-09-24 18:47:01 +02001037 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001038 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1039 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1040 }
1041
1042 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1043 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1044 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +02001045 p.ts.first_error = cpu_to_le32(ts->first_error);
1046 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -07001047 p.ts.unit_base = cpu_to_le32(ts->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +02001048
1049 convert_gs(&p.rs, rs);
1050
Jens Axboe40c60512012-03-27 16:03:04 +02001051 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL);
Jens Axboea64e88d2011-10-03 14:20:01 +02001052}
1053
1054void fio_server_send_gs(struct group_run_stats *rs)
1055{
1056 struct group_run_stats gs;
1057
Jens Axboe60efd142011-10-04 13:30:11 +02001058 dprint(FD_NET, "server sending group run stats\n");
1059
Jens Axboea64e88d2011-10-03 14:20:01 +02001060 convert_gs(&gs, rs);
Jens Axboe40c60512012-03-27 16:03:04 +02001061 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL);
Jens Axboecf451d12011-10-03 16:48:30 +02001062}
1063
Jens Axboed09a64a2011-10-13 11:38:56 +02001064static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1065{
1066 int i;
1067
1068 for (i = 0; i < 2; i++) {
1069 dst->ios[i] = cpu_to_le32(src->ios[i]);
1070 dst->merges[i] = cpu_to_le32(src->merges[i]);
1071 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1072 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1073 }
1074
1075 dst->io_ticks = cpu_to_le32(src->io_ticks);
1076 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1077 dst->slavecount = cpu_to_le32(src->slavecount);
1078 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1079}
1080
1081static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1082{
1083 int i;
1084
1085 strcpy((char *) dst->name, (char *) src->name);
1086
1087 for (i = 0; i < 2; i++) {
1088 dst->ios[i] = cpu_to_le32(src->ios[i]);
1089 dst->merges[i] = cpu_to_le32(src->merges[i]);
1090 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1091 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1092 }
1093
1094 dst->io_ticks = cpu_to_le32(src->io_ticks);
1095 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1096 dst->msec = cpu_to_le64(src->msec);
1097}
1098
1099void fio_server_send_du(void)
1100{
1101 struct disk_util *du;
1102 struct flist_head *entry;
1103 struct cmd_du_pdu pdu;
1104
1105 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1106
Jens Axboe0766d922011-10-13 12:02:08 +02001107 memset(&pdu, 0, sizeof(pdu));
1108
Jens Axboed09a64a2011-10-13 11:38:56 +02001109 flist_for_each(entry, &disk_list) {
1110 du = flist_entry(entry, struct disk_util, list);
1111
1112 convert_dus(&pdu.dus, &du->dus);
1113 convert_agg(&pdu.agg, &du->agg);
1114
Jens Axboe40c60512012-03-27 16:03:04 +02001115 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboed09a64a2011-10-13 11:38:56 +02001116 }
1117}
1118
Jens Axboe53bd8db2012-03-14 21:51:38 +01001119/*
1120 * Send a command with a separate PDU, not inlined in the command
1121 */
1122static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1123 off_t size, uint64_t tag, uint32_t flags)
1124{
1125 struct fio_net_cmd cmd;
1126 struct iovec iov[2];
1127
1128 iov[0].iov_base = &cmd;
1129 iov[0].iov_len = sizeof(cmd);
1130 iov[1].iov_base = (void *) buf;
1131 iov[1].iov_len = size;
1132
1133 __fio_init_net_cmd(&cmd, opcode, size, tag);
1134 cmd.flags = __cpu_to_le32(flags);
1135 fio_net_cmd_crc_pdu(&cmd, buf);
1136
Jens Axboe8c953072012-03-20 14:35:35 +01001137 return fio_sendv_data(sk, iov, 2);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001138}
1139
Jens Axboe3989b142013-04-12 13:13:24 +02001140static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log)
Jens Axboe1b427252012-03-14 15:03:03 +01001141{
Jens Axboe3989b142013-04-12 13:13:24 +02001142 int ret = 0;
1143#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +01001144 z_stream stream;
1145 void *out_pdu;
Jens Axboe1b427252012-03-14 15:03:03 +01001146
1147 /*
1148 * Dirty - since the log is potentially huge, compress it into
1149 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1150 * side defragment it.
1151 */
1152 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1153
1154 stream.zalloc = Z_NULL;
1155 stream.zfree = Z_NULL;
1156 stream.opaque = Z_NULL;
1157
1158 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001159 ret = 1;
1160 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001161 }
1162
Jens Axboef5ed7652012-03-14 16:28:07 +01001163 stream.next_in = (void *) log->log;
1164 stream.avail_in = log->nr_samples * sizeof(struct io_sample);
Jens Axboe1b427252012-03-14 15:03:03 +01001165
1166 do {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001167 unsigned int this_len, flags = 0;
1168 int ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001169
1170 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1171 stream.next_out = out_pdu;
Jens Axboe3c547fe2012-03-14 21:53:00 +01001172 ret = deflate(&stream, Z_FINISH);
1173 /* may be Z_OK, or Z_STREAM_END */
1174 if (ret < 0)
1175 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001176
1177 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1178
Jens Axboe1b427252012-03-14 15:03:03 +01001179 if (stream.avail_in)
Jens Axboe53bd8db2012-03-14 21:51:38 +01001180 flags = FIO_NET_CMD_F_MORE;
Jens Axboe1b427252012-03-14 15:03:03 +01001181
Jens Axboe53bd8db2012-03-14 21:51:38 +01001182 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
1183 out_pdu, this_len, 0, flags);
1184 if (ret)
1185 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001186 } while (stream.avail_in);
1187
Jens Axboe53bd8db2012-03-14 21:51:38 +01001188err_zlib:
Jens Axboe1b427252012-03-14 15:03:03 +01001189 deflateEnd(&stream);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001190err:
1191 free(out_pdu);
Jens Axboe3989b142013-04-12 13:13:24 +02001192#endif
Jens Axboe53bd8db2012-03-14 21:51:38 +01001193 return ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001194}
1195
Jens Axboe3989b142013-04-12 13:13:24 +02001196int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1197{
1198 struct cmd_iolog_pdu pdu;
1199 int i, ret = 0;
1200
1201 pdu.thread_number = cpu_to_le32(td->thread_number);
1202 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
1203 pdu.log_type = cpu_to_le32(log->log_type);
1204 pdu.compressed = cpu_to_le32(use_zlib);
1205 strcpy((char *) pdu.name, name);
1206
1207 for (i = 0; i < log->nr_samples; i++) {
1208 struct io_sample *s = &log->log[i];
1209
1210 s->time = cpu_to_le64(s->time);
1211 s->val = cpu_to_le64(s->val);
1212 s->ddir = cpu_to_le32(s->ddir);
1213 s->bs = cpu_to_le32(s->bs);
1214 }
1215
1216 /*
1217 * Send header first, it's not compressed.
1218 */
1219 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1220 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1221 if (ret)
1222 return ret;
1223
1224 /*
1225 * Now send actual log, compress if we can, otherwise just plain
1226 */
1227 if (use_zlib)
1228 return fio_send_iolog_gz(&pdu, log);
1229
1230 return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log,
1231 log->nr_samples * sizeof(struct io_sample), 0, 0);
1232}
1233
Jens Axboe2f122b12012-03-15 13:10:19 +01001234void fio_server_send_add_job(struct thread_data *td)
Jens Axboe807f9972012-03-02 10:25:24 +01001235{
1236 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +01001237
Jens Axboe731e30a2012-03-14 16:35:37 +01001238 memset(&pdu, 0, sizeof(pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +01001239 pdu.thread_number = cpu_to_le32(td->thread_number);
1240 pdu.groupid = cpu_to_le32(td->groupid);
1241 convert_thread_options_to_net(&pdu.top, &td->o);
Jens Axboe807f9972012-03-02 10:25:24 +01001242
Jens Axboe40c60512012-03-27 16:03:04 +02001243 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboe807f9972012-03-02 10:25:24 +01001244}
1245
Jens Axboe122c7722012-03-19 09:13:15 +01001246void fio_server_send_start(struct thread_data *td)
1247{
1248 assert(server_fd != -1);
1249
1250 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1251}
1252
Jens Axboe87aa8f12011-10-06 21:24:13 +02001253static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +02001254{
Jens Axboe811826b2011-10-24 09:11:50 +02001255 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001256 socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001257 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +02001258
Jens Axboe811826b2011-10-24 09:11:50 +02001259 if (use_ipv6)
1260 sk = socket(AF_INET6, SOCK_STREAM, 0);
1261 else
1262 sk = socket(AF_INET, SOCK_STREAM, 0);
1263
Jens Axboe81179ee2011-10-04 12:42:06 +02001264 if (sk < 0) {
1265 log_err("fio: socket: %s\n", strerror(errno));
1266 return -1;
1267 }
1268
1269 opt = 1;
Jens Axboe1f819912013-01-23 17:21:41 -07001270 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001271 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001272 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001273 return -1;
1274 }
1275#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +02001276 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001277 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001278 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001279 return -1;
1280 }
1281#endif
1282
Jens Axboe811826b2011-10-24 09:11:50 +02001283 if (use_ipv6) {
1284 addr = (struct sockaddr *) &saddr_in6;
1285 socklen = sizeof(saddr_in6);
1286 saddr_in6.sin6_family = AF_INET6;
1287 } else {
1288 addr = (struct sockaddr *) &saddr_in;
1289 socklen = sizeof(saddr_in);
1290 saddr_in.sin_family = AF_INET;
1291 }
Jens Axboe81179ee2011-10-04 12:42:06 +02001292
Jens Axboe811826b2011-10-24 09:11:50 +02001293 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001294 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001295 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001296 return -1;
1297 }
1298
Jens Axboe87aa8f12011-10-06 21:24:13 +02001299 return sk;
1300}
1301
1302static int fio_init_server_sock(void)
1303{
1304 struct sockaddr_un addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001305 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001306 mode_t mode;
1307 int sk;
1308
1309 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1310 if (sk < 0) {
1311 log_err("fio: socket: %s\n", strerror(errno));
1312 return -1;
1313 }
1314
1315 mode = umask(000);
1316
1317 memset(&addr, 0, sizeof(addr));
1318 addr.sun_family = AF_UNIX;
1319 strcpy(addr.sun_path, bind_sock);
1320 unlink(bind_sock);
1321
1322 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1323
1324 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1325 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001326 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001327 return -1;
1328 }
1329
1330 umask(mode);
1331 return sk;
1332}
1333
1334static int fio_init_server_connection(void)
1335{
Jens Axboebebe6392011-10-07 10:00:51 +02001336 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001337 int sk;
1338
1339 dprint(FD_NET, "starting server\n");
1340
1341 if (!bind_sock)
1342 sk = fio_init_server_ip();
1343 else
1344 sk = fio_init_server_sock();
1345
1346 if (sk < 0)
1347 return sk;
1348
Jens Axboe811826b2011-10-24 09:11:50 +02001349 if (!bind_sock) {
1350 char *p, port[16];
1351 const void *src;
1352 int af;
1353
1354 if (use_ipv6) {
1355 af = AF_INET6;
1356 src = &saddr_in6.sin6_addr;
1357 } else {
1358 af = AF_INET;
1359 src = &saddr_in.sin_addr;
1360 }
1361
1362 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1363
1364 sprintf(port, ",%u", fio_net_port);
1365 if (p)
1366 strcat(p, port);
1367 else
1368 strcpy(bind_str, port);
1369 } else
Jens Axboebebe6392011-10-07 10:00:51 +02001370 strcpy(bind_str, bind_sock);
1371
1372 log_info("fio: server listening on %s\n", bind_str);
1373
Jens Axboe89c17072011-10-11 10:15:51 +02001374 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001375 log_err("fio: listen: %s\n", strerror(errno));
1376 return -1;
1377 }
1378
Jens Axboe87aa8f12011-10-06 21:24:13 +02001379 return sk;
1380}
1381
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001382int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1383 struct in6_addr *inp6)
1384
1385{
1386 int ret = 0;
1387
1388 if (*ipv6)
1389 ret = inet_pton(AF_INET6, host, inp6);
1390 else
1391 ret = inet_pton(AF_INET, host, inp);
1392
1393 if (ret != 1) {
1394 struct hostent *hent;
1395
1396 hent = gethostbyname(host);
1397 if (!hent) {
1398 log_err("fio: failed to resolve <%s>\n", host);
1399 return 0;
1400 }
1401
1402 if (*ipv6) {
1403 if (hent->h_addrtype != AF_INET6) {
1404 log_info("fio: falling back to IPv4\n");
1405 *ipv6 = 0;
1406 } else
1407 memcpy(inp6, hent->h_addr_list[0], 16);
1408 }
1409 if (!*ipv6) {
1410 if (hent->h_addrtype != AF_INET) {
1411 log_err("fio: lookup type mismatch\n");
1412 return 0;
1413 }
1414 memcpy(inp, hent->h_addr_list[0], 4);
1415 }
1416 ret = 1;
1417 }
1418
1419 return !(ret == 1);
1420}
1421
Jens Axboe660a2bf2011-10-24 09:35:06 +02001422/*
1423 * Parse a host/ip/port string. Reads from 'str'.
1424 *
1425 * Outputs:
1426 *
1427 * For IPv4:
1428 * *ptr is the host, *port is the port, inp is the destination.
1429 * For IPv6:
1430 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1431 * For local domain sockets:
1432 * *ptr is the filename, *is_sock is 1.
1433 */
Jens Axboebebe6392011-10-07 10:00:51 +02001434int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001435 int *port, struct in_addr *inp,
1436 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001437{
Jens Axboe76867622011-10-25 09:52:51 +02001438 const char *host = str;
1439 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001440 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001441
Jens Axboebebe6392011-10-07 10:00:51 +02001442 *ptr = NULL;
1443 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001444 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001445 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001446
1447 if (!strncmp(str, "sock:", 5)) {
1448 *ptr = strdup(str + 5);
1449 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001450
Jens Axboe76867622011-10-25 09:52:51 +02001451 return 0;
1452 }
1453
1454 /*
1455 * Is it ip:<ip or host>:port
1456 */
1457 if (!strncmp(host, "ip:", 3))
1458 host += 3;
1459 else if (!strncmp(host, "ip4:", 4))
1460 host += 4;
1461 else if (!strncmp(host, "ip6:", 4)) {
1462 host += 4;
1463 *ipv6 = 1;
1464 } else if (host[0] == ':') {
1465 /* String is :port */
1466 host++;
1467 lport = atoi(host);
1468 if (!lport || lport > 65535) {
1469 log_err("fio: bad server port %u\n", port);
1470 return 1;
1471 }
1472 /* no hostname given, we are done */
1473 *port = lport;
1474 return 0;
1475 }
1476
1477 /*
1478 * If no port seen yet, check if there's a last ':' at the end
1479 */
1480 if (!lport) {
1481 portp = strchr(host, ',');
1482 if (portp) {
1483 *portp = '\0';
1484 portp++;
1485 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001486 if (!lport || lport > 65535) {
1487 log_err("fio: bad server port %u\n", port);
1488 return 1;
1489 }
Jens Axboe76867622011-10-25 09:52:51 +02001490 }
1491 }
1492
1493 if (lport)
1494 *port = lport;
1495
1496 if (!strlen(host))
1497 return 0;
1498
1499 *ptr = strdup(host);
1500
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001501 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1502 free(*ptr);
1503 *ptr = NULL;
1504 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001505 }
1506
1507 if (*port == 0)
1508 *port = fio_net_port;
1509
1510 return 0;
1511}
1512
Jens Axboe87aa8f12011-10-06 21:24:13 +02001513/*
1514 * Server arg should be one of:
1515 *
1516 * sock:/path/to/socket
1517 * ip:1.2.3.4
1518 * 1.2.3.4
1519 *
1520 * Where sock uses unix domain sockets, and ip binds the server to
1521 * a specific interface. If no arguments are given to the server, it
1522 * uses IP and binds to 0.0.0.0.
1523 *
1524 */
1525static int fio_handle_server_arg(void)
1526{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001527 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001528 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001529
Jens Axboe87aa8f12011-10-06 21:24:13 +02001530 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1531
1532 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001533 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001534
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001535 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001536 &port, &saddr_in.sin_addr,
1537 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001538
1539 if (!is_sock && bind_sock) {
1540 free(bind_sock);
1541 bind_sock = NULL;
1542 }
1543
Jens Axboea7de0a12011-10-07 12:55:14 +02001544out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001545 fio_net_port = port;
1546 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001547 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001548 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001549}
1550
1551static int fio_server(void)
1552{
1553 int sk, ret;
1554
1555 dprint(FD_NET, "starting server\n");
1556
1557 if (fio_handle_server_arg())
1558 return -1;
1559
1560 sk = fio_init_server_connection();
1561 if (sk < 0)
1562 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001563
1564 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001565
Jens Axboe81179ee2011-10-04 12:42:06 +02001566 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001567
1568 if (fio_server_arg) {
1569 free(fio_server_arg);
1570 fio_server_arg = NULL;
1571 }
Jens Axboebebe6392011-10-07 10:00:51 +02001572 if (bind_sock)
1573 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001574
Jens Axboe81179ee2011-10-04 12:42:06 +02001575 return ret;
1576}
1577
Jens Axboe7b821682011-10-11 12:16:32 +02001578void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001579{
Jens Axboe7b821682011-10-11 12:16:32 +02001580 if (signal == SIGPIPE)
1581 server_fd = -1;
1582 else {
1583 log_info("\nfio: terminating on signal %d\n", signal);
1584 exit_backend = 1;
1585 }
Jens Axboe9abea482011-10-04 13:21:54 +02001586}
1587
Jens Axboe13755d92011-10-10 19:51:26 +02001588static int check_existing_pidfile(const char *pidfile)
1589{
1590 struct stat sb;
1591 char buf[16];
1592 pid_t pid;
1593 FILE *f;
1594
1595 if (stat(pidfile, &sb))
1596 return 0;
1597
1598 f = fopen(pidfile, "r");
1599 if (!f)
1600 return 0;
1601
Jens Axboebfc3b172011-10-10 21:16:55 +02001602 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001603 fclose(f);
1604 return 1;
1605 }
1606 fclose(f);
1607
1608 pid = atoi(buf);
1609 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001610 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001611
1612 return 1;
1613}
1614
1615static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001616{
1617 FILE *fpid;
1618
1619 fpid = fopen(pidfile, "w");
1620 if (!fpid) {
1621 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001622 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001623 }
1624
1625 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001626 fclose(fpid);
1627 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001628}
1629
1630/*
1631 * If pidfile is specified, background us.
1632 */
1633int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001634{
1635 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001636 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001637
Bruce Cran93bcfd22012-02-20 20:18:19 +01001638#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001639 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +02001640 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001641#endif
1642
Jens Axboe402668f2011-10-10 15:28:58 +02001643 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001644 return fio_server();
1645
Jens Axboe13755d92011-10-10 19:51:26 +02001646 if (check_existing_pidfile(pidfile)) {
1647 log_err("fio: pidfile %s exists and server appears alive\n",
1648 pidfile);
1649 return -1;
1650 }
1651
Jens Axboee46d8092011-10-03 09:11:02 +02001652 pid = fork();
1653 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001654 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001655 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001656 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001657 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001658 int ret = write_pid(pid, pidfile);
1659
1660 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001661 }
Jens Axboee46d8092011-10-03 09:11:02 +02001662
1663 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001664 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1665 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001666 close(STDIN_FILENO);
1667 close(STDOUT_FILENO);
1668 close(STDERR_FILENO);
1669 f_out = NULL;
1670 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001671
1672 ret = fio_server();
1673
1674 closelog();
1675 unlink(pidfile);
1676 free(pidfile);
1677 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001678}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001679
Jens Axboebebe6392011-10-07 10:00:51 +02001680void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001681{
1682 fio_server_arg = strdup(arg);
1683}