blob: f6e4c6814a8a481e16c1f89f761190efc76cb69c [file] [log] [blame]
Jens Axboe50d16972011-09-29 17:45:28 -06001#include <stdio.h>
2#include <stdlib.h>
Jens Axboe142575e2011-09-30 17:35:45 -06003#include <stdarg.h>
Jens Axboe50d16972011-09-29 17:45:28 -06004#include <unistd.h>
5#include <limits.h>
Jens Axboe50d16972011-09-29 17:45:28 -06006#include <errno.h>
7#include <fcntl.h>
8#include <sys/poll.h>
Jens Axboe50d16972011-09-29 17:45:28 -06009#include <sys/types.h>
10#include <sys/wait.h>
Jens Axboed05c4a02011-10-05 00:16:11 +020011#include <sys/socket.h>
Jens Axboe87aa8f12011-10-06 21:24:13 +020012#include <sys/stat.h>
13#include <sys/un.h>
Jens Axboe50d16972011-09-29 17:45:28 -060014#include <netinet/in.h>
15#include <arpa/inet.h>
16#include <netdb.h>
Jens Axboee46d8092011-10-03 09:11:02 +020017#include <syslog.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020018#include <signal.h>
Jens Axboe1b427252012-03-14 15:03:03 +010019#include <zlib.h>
Jens Axboe50d16972011-09-29 17:45:28 -060020
21#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060022#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060023#include "crc/crc16.h"
Jens Axboec7c6cb42011-10-13 14:12:40 +020024#include "lib/ieee754.h"
Jens Axboe50d16972011-09-29 17:45:28 -060025
Stephen M. Cameron5adc2442012-02-24 08:17:31 +010026int fio_net_port = FIO_NET_PORT;
Jens Axboe50d16972011-09-29 17:45:28 -060027
Jens Axboe009b1be2011-09-29 18:27:02 -060028int exit_backend = 0;
29
Jens Axboe46c48f12011-10-01 12:50:51 -060030static int server_fd = -1;
Jens Axboe87aa8f12011-10-06 21:24:13 +020031static char *fio_server_arg;
32static char *bind_sock;
33static struct sockaddr_in saddr_in;
Jens Axboe811826b2011-10-24 09:11:50 +020034static struct sockaddr_in6 saddr_in6;
Jens Axboe811826b2011-10-24 09:11:50 +020035static int use_ipv6;
Jens Axboe37db14f2011-09-30 17:00:42 -060036
Jens Axboe122c7722012-03-19 09:13:15 +010037struct fio_fork_item {
38 struct flist_head list;
39 int exitval;
40 int signal;
41 int exited;
42 pid_t pid;
43};
44
45/* Created on fork on new connection */
46static FLIST_HEAD(conn_list);
47
48/* Created on job fork from connection */
49static FLIST_HEAD(job_list);
50
Jens Axboe89c17072011-10-11 10:15:51 +020051static const char *fio_server_ops[FIO_NET_CMD_NR] = {
52 "",
53 "QUIT",
54 "EXIT",
55 "JOB",
56 "JOBLINE",
57 "TEXT",
58 "TS",
59 "GS",
60 "SEND_ETA",
61 "ETA",
62 "PROBE",
63 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020064 "STOP",
65 "DISK_UTIL",
Jens Axboeb9d2f302012-03-08 20:36:28 +010066 "SERVER_START",
Jens Axboe807f9972012-03-02 10:25:24 +010067 "ADD_JOB",
Jens Axboeb9d2f302012-03-08 20:36:28 +010068 "CMD_RUN"
Jens Axboec70e63e2012-03-15 08:26:18 +010069 "CMD_IOLOG",
Jens Axboe89c17072011-10-11 10:15:51 +020070};
71
72const char *fio_server_op(unsigned int op)
73{
74 static char buf[32];
75
76 if (op < FIO_NET_CMD_NR)
77 return fio_server_ops[op];
78
79 sprintf(buf, "UNKNOWN/%d", op);
80 return buf;
81}
82
Jens Axboe5235f622012-03-14 21:25:51 +010083static ssize_t iov_total_len(const struct iovec *iov, int count)
Jens Axboe132159a2011-09-30 15:01:32 -060084{
Jens Axboe5235f622012-03-14 21:25:51 +010085 ssize_t ret = 0;
86
87 while (count--) {
88 ret += iov->iov_len;
89 iov++;
90 }
91
92 return ret;
93}
94
95static int fio_sendv_data(int sk, struct iovec *iov, int count)
96{
97 ssize_t total_len = iov_total_len(iov, count);
98 ssize_t ret;
Jens Axboe794d69c2011-10-01 08:48:50 -060099
Jens Axboe132159a2011-09-30 15:01:32 -0600100 do {
Jens Axboe5235f622012-03-14 21:25:51 +0100101 ret = writev(sk, iov, count);
Jens Axboe132159a2011-09-30 15:01:32 -0600102 if (ret > 0) {
Jens Axboe5235f622012-03-14 21:25:51 +0100103 total_len -= ret;
104 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600105 break;
Jens Axboe5235f622012-03-14 21:25:51 +0100106
107 while (ret) {
108 if (ret >= iov->iov_len) {
109 ret -= iov->iov_len;
110 iov++;
111 continue;
112 }
113 iov->iov_base += ret;
114 iov->iov_len -= ret;
115 ret = 0;
116 }
Jens Axboe132159a2011-09-30 15:01:32 -0600117 } else if (!ret)
118 break;
119 else if (errno == EAGAIN || errno == EINTR)
120 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200121 else
122 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600123 } while (!exit_backend);
124
Jens Axboe5235f622012-03-14 21:25:51 +0100125 if (!total_len)
Jens Axboe132159a2011-09-30 15:01:32 -0600126 return 0;
127
Jens Axboe8b4e61b2012-03-09 17:10:54 +0100128 if (errno)
129 return -errno;
130
Jens Axboe132159a2011-09-30 15:01:32 -0600131 return 1;
132}
133
Jens Axboe5235f622012-03-14 21:25:51 +0100134int fio_send_data(int sk, const void *p, unsigned int len)
135{
136 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
137
138 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
139
140 return fio_sendv_data(sk, &iov, 1);
141}
142
Jens Axboe132159a2011-09-30 15:01:32 -0600143int fio_recv_data(int sk, void *p, unsigned int len)
144{
145 do {
146 int ret = recv(sk, p, len, MSG_WAITALL);
147
148 if (ret > 0) {
149 len -= ret;
150 if (!len)
151 break;
152 p += ret;
153 continue;
154 } else if (!ret)
155 break;
156 else if (errno == EAGAIN || errno == EINTR)
157 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200158 else
159 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600160 } while (!exit_backend);
161
162 if (!len)
163 return 0;
164
165 return -1;
166}
167
168static int verify_convert_cmd(struct fio_net_cmd *cmd)
169{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600170 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600171
Jens Axboefcee5ff2011-09-30 22:50:39 -0600172 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
173 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600174
Jens Axboe25dfa842012-02-29 10:01:34 +0100175 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600176 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600177 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600178 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600179 return 1;
180 }
181
182 cmd->version = le16_to_cpu(cmd->version);
183 cmd->opcode = le16_to_cpu(cmd->opcode);
184 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200185 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600186 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
187
188 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200189 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600190 break;
191 default:
192 log_err("fio: bad server cmd version %d\n", cmd->version);
193 return 1;
194 }
195
Jens Axboeb9d2f302012-03-08 20:36:28 +0100196 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600197 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
198 return 1;
199 }
200
201 return 0;
202}
203
Jens Axboea64e88d2011-10-03 14:20:01 +0200204/*
205 * Read (and defragment, if necessary) incoming commands
206 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200207struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600208{
Jens Axboea64e88d2011-10-03 14:20:01 +0200209 struct fio_net_cmd cmd, *cmdret = NULL;
210 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600211 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200212 int ret, first = 1;
213 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600214
Jens Axboea64e88d2011-10-03 14:20:01 +0200215 do {
216 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
217 if (ret)
218 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600219
Jens Axboea64e88d2011-10-03 14:20:01 +0200220 /* We have a command, verify it and swap if need be */
221 ret = verify_convert_cmd(&cmd);
222 if (ret)
223 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600224
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200225 if (first) {
226 /* if this is text, add room for \0 at the end */
227 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
228 assert(!cmdret);
229 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200230 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600231
Jens Axboea64e88d2011-10-03 14:20:01 +0200232 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600233
Jens Axboea64e88d2011-10-03 14:20:01 +0200234 if (first)
235 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200236 else if (cmdret->opcode != cmd.opcode) {
237 log_err("fio: fragment opcode mismatch (%d != %d)\n",
238 cmdret->opcode, cmd.opcode);
239 ret = 1;
240 break;
241 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200242
243 if (!cmd.pdu_len)
244 break;
245
246 /* There's payload, get it */
247 pdu = (void *) cmdret->payload + pdu_offset;
248 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
249 if (ret)
250 break;
251
252 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100253 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200254 if (crc != cmd.pdu_crc16) {
255 log_err("fio: server bad crc on payload ");
256 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
257 ret = 1;
258 break;
259 }
260
261 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200262 if (!first)
263 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200264 first = 0;
265 } while (cmd.flags & FIO_NET_CMD_F_MORE);
266
267 if (ret) {
268 free(cmdret);
269 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200270 } else if (cmdret) {
271 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100272 if (cmdret->pdu_len) {
273 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
274 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
275 char *buf = (char *) pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200276
Jens Axboe3c3ed072012-03-27 09:12:39 +0200277 buf[pdu->buf_len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100278 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
Jens Axboe46bcd492012-03-14 11:31:21 +0100279 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
280 char *buf = (char *) pdu->buf;
281 int len = le32_to_cpu(pdu->buf_len);
Jens Axboe084d1c62012-03-03 20:28:07 +0100282
Jens Axboe46bcd492012-03-14 11:31:21 +0100283 buf[len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100284 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200285 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100286
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200287 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200288 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200289 }
Jens Axboe132159a2011-09-30 15:01:32 -0600290
Jens Axboea64e88d2011-10-03 14:20:01 +0200291 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600292}
293
Jens Axboe40c60512012-03-27 16:03:04 +0200294static void add_reply(uint64_t tag, struct flist_head *list)
295{
296 struct fio_net_cmd_reply *reply = (struct fio_net_cmd_reply *) tag;
297
298 flist_add_tail(&reply->list, list);
299}
300
301static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
302{
303 struct fio_net_cmd_reply *reply;
304
305 reply = calloc(1, sizeof(*reply));
306 INIT_FLIST_HEAD(&reply->list);
307 gettimeofday(&reply->tv, NULL);
308 reply->saved_tag = tag;
309 reply->opcode = opcode;
310
311 return (uintptr_t) reply;
312}
313
314static void free_reply(uint64_t tag)
315{
316 struct fio_net_cmd_reply *reply = (struct fio_net_cmd_reply *) tag;
317
318 free(reply);
319}
320
Jens Axboe53bd8db2012-03-14 21:51:38 +0100321void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
Jens Axboe132159a2011-09-30 15:01:32 -0600322{
323 uint32_t pdu_len;
324
Jens Axboe25dfa842012-02-29 10:01:34 +0100325 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600326
327 pdu_len = le32_to_cpu(cmd->pdu_len);
Jens Axboe1b427252012-03-14 15:03:03 +0100328 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
329}
330
331void fio_net_cmd_crc(struct fio_net_cmd *cmd)
332{
333 fio_net_cmd_crc_pdu(cmd, cmd->payload);
Jens Axboe132159a2011-09-30 15:01:32 -0600334}
335
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200336int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
Jens Axboe40c60512012-03-27 16:03:04 +0200337 uint64_t *tagptr, struct flist_head *list)
Jens Axboe794d69c2011-10-01 08:48:50 -0600338{
Jens Axboe7f868312011-10-10 09:55:21 +0200339 struct fio_net_cmd *cmd = NULL;
340 size_t this_len, cur_len = 0;
Jens Axboe40c60512012-03-27 16:03:04 +0200341 uint64_t tag;
Jens Axboe794d69c2011-10-01 08:48:50 -0600342 int ret;
343
Jens Axboe40c60512012-03-27 16:03:04 +0200344 if (list) {
345 assert(tagptr);
346 tag = *tagptr = alloc_reply(*tagptr, opcode);
347 } else
348 tag = tagptr ? *tagptr : 0;
349
Jens Axboe794d69c2011-10-01 08:48:50 -0600350 do {
351 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100352 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
353 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600354
Jens Axboe7f868312011-10-10 09:55:21 +0200355 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
356 if (cmd)
357 free(cmd);
358
359 cur_len = sizeof(*cmd) + this_len;
360 cmd = malloc(cur_len);
361 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600362
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200363 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600364
365 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200366 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600367
368 fio_net_cmd_crc(cmd);
369
370 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600371 size -= this_len;
372 buf += this_len;
373 } while (!ret && size);
374
Jens Axboe40c60512012-03-27 16:03:04 +0200375 if (list) {
376 if (ret)
377 free_reply(tag);
378 else
379 add_reply(tag, list);
380 }
381
Jens Axboe7f868312011-10-10 09:55:21 +0200382 if (cmd)
383 free(cmd);
384
Jens Axboe794d69c2011-10-01 08:48:50 -0600385 return ret;
386}
387
Jens Axboe89c17072011-10-11 10:15:51 +0200388static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600389{
Jens Axboe178cde92011-10-05 22:14:31 +0200390 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600391
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200392 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600393 fio_net_cmd_crc(&cmd);
394
395 return fio_send_data(sk, &cmd, sizeof(cmd));
396}
397
Jens Axboe89c17072011-10-11 10:15:51 +0200398/*
399 * If 'list' is non-NULL, then allocate and store the sent command for
400 * later verification.
401 */
402int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
403 struct flist_head *list)
404{
Jens Axboe89c17072011-10-11 10:15:51 +0200405 int ret;
406
Jens Axboe40c60512012-03-27 16:03:04 +0200407 if (list)
408 tag = alloc_reply(tag, opcode);
Jens Axboe89c17072011-10-11 10:15:51 +0200409
Jens Axboe40c60512012-03-27 16:03:04 +0200410 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
Jens Axboe89c17072011-10-11 10:15:51 +0200411 if (ret) {
Jens Axboe40c60512012-03-27 16:03:04 +0200412 if (list)
413 free_reply(tag);
414
Jens Axboe89c17072011-10-11 10:15:51 +0200415 return ret;
416 }
417
Jens Axboe40c60512012-03-27 16:03:04 +0200418 if (list)
419 add_reply(tag, list);
420
Jens Axboe89c17072011-10-11 10:15:51 +0200421 return 0;
422}
423
Jens Axboe122c7722012-03-19 09:13:15 +0100424int fio_net_send_quit(int sk)
Jens Axboe437377e2011-10-01 08:31:30 -0600425{
Jens Axboe46c48f12011-10-01 12:50:51 -0600426 dprint(FD_NET, "server: sending quit\n");
Jens Axboe122c7722012-03-19 09:13:15 +0100427
Jens Axboe8c953072012-03-20 14:35:35 +0100428 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600429}
430
Jens Axboef58bd2a2012-03-27 10:06:42 +0200431static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error,
432 int signal)
Jens Axboe122c7722012-03-19 09:13:15 +0100433{
434 struct cmd_end_pdu epdu;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200435 uint64_t tag = 0;
Jens Axboe122c7722012-03-19 09:13:15 +0100436
Jens Axboef58bd2a2012-03-27 10:06:42 +0200437 if (cmd)
438 tag = cmd->tag;
Jens Axboe122c7722012-03-19 09:13:15 +0100439
440 epdu.error = __cpu_to_le32(error);
441 epdu.signal = __cpu_to_le32(signal);
Jens Axboe40c60512012-03-27 16:03:04 +0200442 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200443}
444
445int fio_net_send_stop(int sk, int error, int signal)
446{
447 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
448 return fio_net_send_ack(sk, NULL, error, signal);
Jens Axboe122c7722012-03-19 09:13:15 +0100449}
450
451static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
452{
453 struct fio_fork_item *ffi;
454
455 ffi = malloc(sizeof(*ffi));
456 ffi->exitval = 0;
457 ffi->signal = 0;
458 ffi->exited = 0;
459 ffi->pid = pid;
460 flist_add_tail(&ffi->list, list);
461}
462
463static void fio_server_add_conn_pid(pid_t pid)
464{
465 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", pid);
466 fio_server_add_fork_item(pid, &conn_list);
467}
468
469static void fio_server_add_job_pid(pid_t pid)
470{
471 dprint(FD_NET, "server: forked off job job (pid=%u)\n", pid);
472 fio_server_add_fork_item(pid, &job_list);
473}
474
475static void fio_server_check_fork_item(struct fio_fork_item *ffi)
476{
477 int ret, status;
478
479 ret = waitpid(ffi->pid, &status, WNOHANG);
480 if (ret < 0) {
481 if (errno == ECHILD) {
482 log_err("fio: connection pid %u disappeared\n", ffi->pid);
483 ffi->exited = 1;
484 } else
485 log_err("fio: waitpid: %s\n", strerror(errno));
486 } else if (ret == ffi->pid) {
487 if (WIFSIGNALED(status)) {
488 ffi->signal = WTERMSIG(status);
489 ffi->exited = 1;
490 }
491 if (WIFEXITED(status)) {
492 if (WEXITSTATUS(status))
493 ffi->exitval = WEXITSTATUS(status);
494 ffi->exited = 1;
495 }
496 }
497}
498
499static void fio_server_fork_item_done(struct fio_fork_item *ffi)
500{
501 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", ffi->pid, ffi->signal, ffi->exitval);
502
503 /*
504 * Fold STOP and QUIT...
505 */
506 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
507 fio_net_send_quit(server_fd);
508 flist_del(&ffi->list);
509 free(ffi);
510}
511
Jens Axboe54163252012-03-23 12:25:18 +0100512static void fio_server_check_fork_items(struct flist_head *list)
Jens Axboe122c7722012-03-19 09:13:15 +0100513{
514 struct flist_head *entry, *tmp;
515 struct fio_fork_item *ffi;
516
517 flist_for_each_safe(entry, tmp, list) {
518 ffi = flist_entry(entry, struct fio_fork_item, list);
519
520 fio_server_check_fork_item(ffi);
521
522 if (ffi->exited)
523 fio_server_fork_item_done(ffi);
524 }
525}
526
527static void fio_server_check_jobs(void)
528{
Jens Axboe54163252012-03-23 12:25:18 +0100529 fio_server_check_fork_items(&job_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100530}
531
532static void fio_server_check_conns(void)
533{
Jens Axboe54163252012-03-23 12:25:18 +0100534 fio_server_check_fork_items(&conn_list);
Jens Axboe122c7722012-03-19 09:13:15 +0100535}
536
Jens Axboeb9d2f302012-03-08 20:36:28 +0100537static int handle_run_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600538{
Jens Axboe122c7722012-03-19 09:13:15 +0100539 pid_t pid;
Jens Axboea64e88d2011-10-03 14:20:01 +0200540 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600541
Jens Axboe122c7722012-03-19 09:13:15 +0100542 set_genesis_time();
543
544 pid = fork();
545 if (pid) {
546 fio_server_add_job_pid(pid);
547 return 0;
548 }
549
Jens Axboe2e1df072012-02-09 11:15:02 +0100550 ret = fio_backend();
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200551 free_threads_shm();
Jens Axboe122c7722012-03-19 09:13:15 +0100552 _exit(ret);
Jens Axboe81179ee2011-10-04 12:42:06 +0200553}
554
Jens Axboeb9d2f302012-03-08 20:36:28 +0100555static int handle_job_cmd(struct fio_net_cmd *cmd)
556{
Jens Axboe46bcd492012-03-14 11:31:21 +0100557 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
558 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100559 struct cmd_start_pdu spdu;
560
Jens Axboe46bcd492012-03-14 11:31:21 +0100561 pdu->buf_len = le32_to_cpu(pdu->buf_len);
562 pdu->client_type = le32_to_cpu(pdu->client_type);
563
564 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100565 fio_net_send_quit(server_fd);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100566 return -1;
567 }
568
569 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe108fea72012-11-14 13:09:45 -0700570 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200571 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100572 return 0;
573}
574
Jens Axboe81179ee2011-10-04 12:42:06 +0200575static int handle_jobline_cmd(struct fio_net_cmd *cmd)
576{
Jens Axboefa2ea802011-10-08 21:07:29 +0200577 void *pdu = cmd->payload;
578 struct cmd_single_line_pdu *cslp;
579 struct cmd_line_pdu *clp;
580 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100581 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200582 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100583 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200584
Jens Axboefa2ea802011-10-08 21:07:29 +0200585 clp = pdu;
586 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100587 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200588 argv = malloc(clp->lines * sizeof(char *));
589 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200590
Jens Axboefa2ea802011-10-08 21:07:29 +0200591 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200592
Jens Axboefa2ea802011-10-08 21:07:29 +0200593 for (i = 0; i < clp->lines; i++) {
594 cslp = pdu + offset;
595 argv[i] = (char *) cslp->text;
596
597 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200598 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
599 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200600
Jens Axboe46bcd492012-03-14 11:31:21 +0100601 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboe122c7722012-03-19 09:13:15 +0100602 fio_net_send_quit(server_fd);
Jens Axboefa2ea802011-10-08 21:07:29 +0200603 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200604 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200605 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200606
Jens Axboefa2ea802011-10-08 21:07:29 +0200607 free(argv);
608
Jens Axboeb9d2f302012-03-08 20:36:28 +0100609 spdu.jobs = cpu_to_le32(thread_number);
Jens Axboe1e5324e2012-11-14 14:25:31 -0700610 spdu.stat_outputs = cpu_to_le32(stat_number);
Jens Axboe40c60512012-03-27 16:03:04 +0200611 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
Jens Axboeb9d2f302012-03-08 20:36:28 +0100612 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600613}
614
Jens Axboec28e8e82011-10-04 08:57:39 +0200615static int handle_probe_cmd(struct fio_net_cmd *cmd)
616{
617 struct cmd_probe_pdu probe;
Jens Axboe40c60512012-03-27 16:03:04 +0200618 uint64_t tag = cmd->tag;
Jens Axboec28e8e82011-10-04 08:57:39 +0200619
Jens Axboe89c17072011-10-11 10:15:51 +0200620 dprint(FD_NET, "server: sending probe reply\n");
621
Jens Axboec28e8e82011-10-04 08:57:39 +0200622 memset(&probe, 0, sizeof(probe));
623 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe0dcebdf2013-01-23 15:42:16 -0700624#ifdef CONFIG_BIG_ENDIAN
Jens Axboe6eb24792011-10-04 15:00:30 +0200625 probe.bigendian = 1;
626#endif
Jens Axboe750db472012-04-16 11:44:48 +0200627 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
Jens Axboec28e8e82011-10-04 08:57:39 +0200628
Jens Axboecca84642011-10-07 12:47:57 +0200629 probe.os = FIO_OS;
630 probe.arch = FIO_ARCH;
Jens Axboe38fdef22011-10-11 14:30:06 +0200631 probe.bpp = sizeof(void *);
Jens Axboed31e26d2012-03-28 21:38:24 +0200632 probe.cpus = __cpu_to_le32(cpus_online());
633 probe.flags = 0;
Jens Axboe38fdef22011-10-11 14:30:06 +0200634
Jens Axboe40c60512012-03-27 16:03:04 +0200635 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200636}
637
638static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
639{
640 struct jobs_eta *je;
641 size_t size;
Jens Axboe40c60512012-03-27 16:03:04 +0200642 uint64_t tag = cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200643 int i;
644
Jens Axboeb814fb22011-10-12 09:20:34 +0200645 if (!thread_number)
646 return 0;
647
648 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200649 je = malloc(size);
650 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200651
652 if (!calc_thread_status(je, 1)) {
653 free(je);
654 return 0;
655 }
656
657 dprint(FD_NET, "server sending status\n");
658
659 je->nr_running = cpu_to_le32(je->nr_running);
660 je->nr_ramp = cpu_to_le32(je->nr_ramp);
661 je->nr_pending = cpu_to_le32(je->nr_pending);
662 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200663
Jens Axboe298921d2012-09-24 18:47:01 +0200664 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100665 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
666 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
667 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
668 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboe16725bb2013-04-11 12:43:05 +0200669 je->rate[i] = cpu_to_le32(je->rate[i]);
670 je->iops[i] = cpu_to_le32(je->iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200671 }
672
Jens Axboec1970b92012-03-06 15:37:40 +0100673 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200674 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100675 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200676 je->is_pow2 = cpu_to_le32(je->is_pow2);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200677
Jens Axboe40c60512012-03-27 16:03:04 +0200678 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200679 free(je);
680 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200681}
682
Jens Axboe40c60512012-03-27 16:03:04 +0200683static int send_update_job_reply(int fd, uint64_t __tag, int error)
684{
685 uint64_t tag = __tag;
686 uint32_t pdu_error;
687
688 pdu_error = __cpu_to_le32(error);
689 return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL);
690}
691
Jens Axboef58bd2a2012-03-27 10:06:42 +0200692static int handle_update_job_cmd(struct fio_net_cmd *cmd)
693{
694 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
695 struct thread_data *td;
696 uint32_t tnumber;
697
698 tnumber = le32_to_cpu(pdu->thread_number);
699
700 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
701
Jens Axboe30ffacb2012-03-28 09:15:05 +0200702 if (!tnumber || tnumber > thread_number) {
Jens Axboe40c60512012-03-27 16:03:04 +0200703 send_update_job_reply(server_fd, cmd->tag, ENODEV);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200704 return 0;
705 }
706
Jens Axboe30ffacb2012-03-28 09:15:05 +0200707 td = &threads[tnumber - 1];
Jens Axboef58bd2a2012-03-27 10:06:42 +0200708 convert_thread_options_to_cpu(&td->o, &pdu->top);
Jens Axboe40c60512012-03-27 16:03:04 +0200709 send_update_job_reply(server_fd, cmd->tag, 0);
Jens Axboef58bd2a2012-03-27 10:06:42 +0200710 return 0;
711}
712
Jens Axboe132159a2011-09-30 15:01:32 -0600713static int handle_command(struct fio_net_cmd *cmd)
714{
715 int ret;
716
Jens Axboe4b91ee82013-02-25 10:18:33 +0100717 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
718 fio_server_op(cmd->opcode), cmd->pdu_len,
719 (unsigned long long) cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600720
Jens Axboe132159a2011-09-30 15:01:32 -0600721 switch (cmd->opcode) {
722 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200723 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200724 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200725 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600726 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200727 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600728 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200729 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600730 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200731 case FIO_NET_CMD_JOBLINE:
732 ret = handle_jobline_cmd(cmd);
733 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200734 case FIO_NET_CMD_PROBE:
735 ret = handle_probe_cmd(cmd);
736 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200737 case FIO_NET_CMD_SEND_ETA:
738 ret = handle_send_eta_cmd(cmd);
739 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100740 case FIO_NET_CMD_RUN:
741 ret = handle_run_cmd(cmd);
742 break;
Jens Axboef58bd2a2012-03-27 10:06:42 +0200743 case FIO_NET_CMD_UPDATE_JOB:
744 ret = handle_update_job_cmd(cmd);
745 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600746 default:
Jens Axboe3c3ed072012-03-27 09:12:39 +0200747 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600748 ret = 1;
749 }
750
751 return ret;
752}
753
Jens Axboe122c7722012-03-19 09:13:15 +0100754static int handle_connection(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600755{
756 struct fio_net_cmd *cmd = NULL;
757 int ret = 0;
758
Jens Axboe122c7722012-03-19 09:13:15 +0100759 reset_fio_state();
760 INIT_FLIST_HEAD(&job_list);
761 server_fd = sk;
762
Jens Axboe132159a2011-09-30 15:01:32 -0600763 /* read forever */
764 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200765 struct pollfd pfd = {
766 .fd = sk,
767 .events = POLLIN,
768 };
769
770 ret = 0;
771 do {
Jens Axboe54163252012-03-23 12:25:18 +0100772 int timeout = 1000;
773
774 if (!flist_empty(&job_list))
775 timeout = 100;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200776
Jens Axboe54163252012-03-23 12:25:18 +0100777 ret = poll(&pfd, 1, timeout);
Jens Axboee951bdc2011-10-05 21:58:45 +0200778 if (ret < 0) {
779 if (errno == EINTR)
780 break;
781 log_err("fio: poll: %s\n", strerror(errno));
782 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200783 } else if (!ret) {
Jens Axboe122c7722012-03-19 09:13:15 +0100784 fio_server_check_jobs();
Jens Axboee951bdc2011-10-05 21:58:45 +0200785 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200786 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200787
788 if (pfd.revents & POLLIN)
789 break;
790 if (pfd.revents & (POLLERR|POLLHUP)) {
791 ret = 1;
792 break;
793 }
Jens Axboe19c65172011-10-05 22:05:37 +0200794 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200795
Jens Axboe122c7722012-03-19 09:13:15 +0100796 fio_server_check_jobs();
797
Jens Axboee951bdc2011-10-05 21:58:45 +0200798 if (ret < 0)
799 break;
800
801 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600802 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200803 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600804 break;
805 }
806
Jens Axboe132159a2011-09-30 15:01:32 -0600807 ret = handle_command(cmd);
808 if (ret)
809 break;
810
811 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400812 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600813 }
814
815 if (cmd)
816 free(cmd);
817
Jens Axboe122c7722012-03-19 09:13:15 +0100818 close(sk);
819 _exit(ret);
Jens Axboecc0df002011-10-03 20:53:32 +0200820}
821
Jens Axboe50d16972011-09-29 17:45:28 -0600822static int accept_loop(int listen_sk)
823{
Jens Axboebb447a22011-10-04 15:06:42 +0200824 struct sockaddr_in addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100825 socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600826 struct pollfd pfd;
Jens Axboe122c7722012-03-19 09:13:15 +0100827 int ret = 0, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600828
Jens Axboe60efd142011-10-04 13:30:11 +0200829 dprint(FD_NET, "server enter accept loop\n");
830
Jens Axboe009b1be2011-09-29 18:27:02 -0600831 flags = fcntl(listen_sk, F_GETFL);
832 flags |= O_NONBLOCK;
833 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe122c7722012-03-19 09:13:15 +0100834
835 while (!exit_backend) {
836 pid_t pid;
837
838 pfd.fd = listen_sk;
839 pfd.events = POLLIN;
840 do {
Jens Axboe54163252012-03-23 12:25:18 +0100841 int timeout = 1000;
842
843 if (!flist_empty(&conn_list))
844 timeout = 100;
845
846 ret = poll(&pfd, 1, timeout);
Jens Axboe122c7722012-03-19 09:13:15 +0100847 if (ret < 0) {
848 if (errno == EINTR)
849 break;
850 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600851 break;
Jens Axboe122c7722012-03-19 09:13:15 +0100852 } else if (!ret) {
853 fio_server_check_conns();
854 continue;
855 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600856
Jens Axboe122c7722012-03-19 09:13:15 +0100857 if (pfd.revents & POLLIN)
858 break;
859 } while (!exit_backend);
860
861 fio_server_check_conns();
862
863 if (exit_backend || ret < 0)
Jens Axboe009b1be2011-09-29 18:27:02 -0600864 break;
Jens Axboe009b1be2011-09-29 18:27:02 -0600865
Jens Axboe122c7722012-03-19 09:13:15 +0100866 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
867 if (sk < 0) {
868 log_err("fio: accept: %s\n", strerror(errno));
869 return -1;
870 }
Jens Axboe009b1be2011-09-29 18:27:02 -0600871
Jens Axboe122c7722012-03-19 09:13:15 +0100872 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
873
874 pid = fork();
875 if (pid) {
876 close(sk);
877 fio_server_add_conn_pid(pid);
878 continue;
879 }
880
881 /* exits */
882 handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -0600883 }
884
Jens Axboe132159a2011-09-30 15:01:32 -0600885 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600886}
887
Jens Axboe084d1c62012-03-03 20:28:07 +0100888int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600889{
Jens Axboe084d1c62012-03-03 20:28:07 +0100890 struct cmd_text_pdu *pdu;
891 unsigned int tlen;
892 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600893
Jens Axboe084d1c62012-03-03 20:28:07 +0100894 if (server_fd == -1)
895 return log_local_buf(buf, len);
896
897 tlen = sizeof(*pdu) + len;
898 pdu = malloc(tlen);
899
900 pdu->level = __cpu_to_le32(level);
901 pdu->buf_len = __cpu_to_le32(len);
902
903 gettimeofday(&tv, NULL);
904 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
905 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
906
907 memcpy(pdu->buf, buf, len);
908
Jens Axboe40c60512012-03-27 16:03:04 +0200909 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL);
Jens Axboe084d1c62012-03-03 20:28:07 +0100910 free(pdu);
911 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600912}
913
Jens Axboea64e88d2011-10-03 14:20:01 +0200914static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
915{
916 dst->max_val = cpu_to_le64(src->max_val);
917 dst->min_val = cpu_to_le64(src->min_val);
918 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200919
920 /*
921 * Encode to IEEE 754 for network transfer
922 */
923 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
924 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200925}
926
927static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
928{
929 int i;
930
Jens Axboe298921d2012-09-24 18:47:01 +0200931 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200932 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
933 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
934 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
935 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
936 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
937 dst->agg[i] = cpu_to_le64(src->agg[i]);
938 }
939
940 dst->kb_base = cpu_to_le32(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700941 dst->unit_base = cpu_to_le32(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200942 dst->groupid = cpu_to_le32(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +0100943 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200944}
945
946/*
947 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
948 * into a single payload.
949 */
950void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
951{
952 struct cmd_ts_pdu p;
953 int i, j;
954
Jens Axboe60efd142011-10-04 13:30:11 +0200955 dprint(FD_NET, "server sending end stats\n");
956
Jens Axboe317b3c82011-10-03 21:47:27 +0200957 memset(&p, 0, sizeof(p));
958
Jens Axboea64e88d2011-10-03 14:20:01 +0200959 strcpy(p.ts.name, ts->name);
960 strcpy(p.ts.verror, ts->verror);
961 strcpy(p.ts.description, ts->description);
962
Jens Axboe2f122b12012-03-15 13:10:19 +0100963 p.ts.error = cpu_to_le32(ts->error);
964 p.ts.thread_number = cpu_to_le32(ts->thread_number);
965 p.ts.groupid = cpu_to_le32(ts->groupid);
966 p.ts.pid = cpu_to_le32(ts->pid);
967 p.ts.members = cpu_to_le32(ts->members);
Jens Axboe771e58b2013-01-30 12:56:23 +0100968 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200969
Jens Axboe298921d2012-09-24 18:47:01 +0200970 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200971 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
972 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
973 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
974 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
975 }
976
977 p.ts.usr_time = cpu_to_le64(ts->usr_time);
978 p.ts.sys_time = cpu_to_le64(ts->sys_time);
979 p.ts.ctx = cpu_to_le64(ts->ctx);
980 p.ts.minf = cpu_to_le64(ts->minf);
981 p.ts.majf = cpu_to_le64(ts->majf);
982 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200983
984 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200985 fio_fp64_t *src = &ts->percentile_list[i];
986 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200987
Jens Axboecfc03e42011-10-12 21:20:42 +0200988 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200989 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200990
991 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
992 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
993 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
994 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
995 }
996
997 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
998 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
999 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1000 }
1001
Jens Axboe298921d2012-09-24 18:47:01 +02001002 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +02001003 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1004 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1005
Jens Axboe78799de2013-01-29 20:53:52 +01001006 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001007 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +02001008 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +02001009 }
1010
Jens Axboe93eee042011-10-03 21:08:48 +02001011 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +02001012 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1013
Jens Axboe298921d2012-09-24 18:47:01 +02001014 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +02001015 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1016 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1017 }
1018
1019 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1020 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1021 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +02001022 p.ts.first_error = cpu_to_le32(ts->first_error);
1023 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -07001024 p.ts.unit_base = cpu_to_le32(ts->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +02001025
1026 convert_gs(&p.rs, rs);
1027
Jens Axboe40c60512012-03-27 16:03:04 +02001028 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL);
Jens Axboea64e88d2011-10-03 14:20:01 +02001029}
1030
1031void fio_server_send_gs(struct group_run_stats *rs)
1032{
1033 struct group_run_stats gs;
1034
Jens Axboe60efd142011-10-04 13:30:11 +02001035 dprint(FD_NET, "server sending group run stats\n");
1036
Jens Axboea64e88d2011-10-03 14:20:01 +02001037 convert_gs(&gs, rs);
Jens Axboe40c60512012-03-27 16:03:04 +02001038 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL);
Jens Axboecf451d12011-10-03 16:48:30 +02001039}
1040
Jens Axboed09a64a2011-10-13 11:38:56 +02001041static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1042{
1043 int i;
1044
1045 for (i = 0; i < 2; i++) {
1046 dst->ios[i] = cpu_to_le32(src->ios[i]);
1047 dst->merges[i] = cpu_to_le32(src->merges[i]);
1048 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1049 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1050 }
1051
1052 dst->io_ticks = cpu_to_le32(src->io_ticks);
1053 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1054 dst->slavecount = cpu_to_le32(src->slavecount);
1055 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1056}
1057
1058static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1059{
1060 int i;
1061
1062 strcpy((char *) dst->name, (char *) src->name);
1063
1064 for (i = 0; i < 2; i++) {
1065 dst->ios[i] = cpu_to_le32(src->ios[i]);
1066 dst->merges[i] = cpu_to_le32(src->merges[i]);
1067 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1068 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1069 }
1070
1071 dst->io_ticks = cpu_to_le32(src->io_ticks);
1072 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1073 dst->msec = cpu_to_le64(src->msec);
1074}
1075
1076void fio_server_send_du(void)
1077{
1078 struct disk_util *du;
1079 struct flist_head *entry;
1080 struct cmd_du_pdu pdu;
1081
1082 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1083
Jens Axboe0766d922011-10-13 12:02:08 +02001084 memset(&pdu, 0, sizeof(pdu));
1085
Jens Axboed09a64a2011-10-13 11:38:56 +02001086 flist_for_each(entry, &disk_list) {
1087 du = flist_entry(entry, struct disk_util, list);
1088
1089 convert_dus(&pdu.dus, &du->dus);
1090 convert_agg(&pdu.agg, &du->agg);
1091
Jens Axboe40c60512012-03-27 16:03:04 +02001092 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboed09a64a2011-10-13 11:38:56 +02001093 }
1094}
1095
Jens Axboe53bd8db2012-03-14 21:51:38 +01001096/*
1097 * Send a command with a separate PDU, not inlined in the command
1098 */
1099static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1100 off_t size, uint64_t tag, uint32_t flags)
1101{
1102 struct fio_net_cmd cmd;
1103 struct iovec iov[2];
1104
1105 iov[0].iov_base = &cmd;
1106 iov[0].iov_len = sizeof(cmd);
1107 iov[1].iov_base = (void *) buf;
1108 iov[1].iov_len = size;
1109
1110 __fio_init_net_cmd(&cmd, opcode, size, tag);
1111 cmd.flags = __cpu_to_le32(flags);
1112 fio_net_cmd_crc_pdu(&cmd, buf);
1113
Jens Axboe8c953072012-03-20 14:35:35 +01001114 return fio_sendv_data(sk, iov, 2);
Jens Axboe53bd8db2012-03-14 21:51:38 +01001115}
1116
Jens Axboe1b427252012-03-14 15:03:03 +01001117int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1118{
Jens Axboef5ed7652012-03-14 16:28:07 +01001119 struct cmd_iolog_pdu pdu;
Jens Axboe1b427252012-03-14 15:03:03 +01001120 z_stream stream;
1121 void *out_pdu;
Jens Axboe53bd8db2012-03-14 21:51:38 +01001122 int i, ret = 0;
Jens Axboe1b427252012-03-14 15:03:03 +01001123
Jens Axboe2f122b12012-03-15 13:10:19 +01001124 pdu.thread_number = cpu_to_le32(td->thread_number);
Jens Axboef5ed7652012-03-14 16:28:07 +01001125 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
1126 pdu.log_type = cpu_to_le32(log->log_type);
1127 strcpy((char *) pdu.name, name);
Jens Axboe1b427252012-03-14 15:03:03 +01001128
1129 for (i = 0; i < log->nr_samples; i++) {
Jens Axboef5ed7652012-03-14 16:28:07 +01001130 struct io_sample *s = &log->log[i];
Jens Axboe1b427252012-03-14 15:03:03 +01001131
Jens Axboef5ed7652012-03-14 16:28:07 +01001132 s->time = cpu_to_le64(s->time);
1133 s->val = cpu_to_le64(s->val);
1134 s->ddir = cpu_to_le32(s->ddir);
1135 s->bs = cpu_to_le32(s->bs);
Jens Axboe1b427252012-03-14 15:03:03 +01001136 }
1137
1138 /*
1139 * Dirty - since the log is potentially huge, compress it into
1140 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1141 * side defragment it.
1142 */
1143 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1144
1145 stream.zalloc = Z_NULL;
1146 stream.zfree = Z_NULL;
1147 stream.opaque = Z_NULL;
1148
1149 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
Jens Axboe53bd8db2012-03-14 21:51:38 +01001150 ret = 1;
1151 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001152 }
1153
1154 /*
Jens Axboef5ed7652012-03-14 16:28:07 +01001155 * Send header first, it's not compressed.
Jens Axboe1b427252012-03-14 15:03:03 +01001156 */
Jens Axboe53bd8db2012-03-14 21:51:38 +01001157 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1158 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1159 if (ret)
1160 goto err_zlib;
Jens Axboe1b427252012-03-14 15:03:03 +01001161
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);
1191 return ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001192}
1193
Jens Axboe2f122b12012-03-15 13:10:19 +01001194void fio_server_send_add_job(struct thread_data *td)
Jens Axboe807f9972012-03-02 10:25:24 +01001195{
1196 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +01001197
Jens Axboe731e30a2012-03-14 16:35:37 +01001198 memset(&pdu, 0, sizeof(pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +01001199 pdu.thread_number = cpu_to_le32(td->thread_number);
1200 pdu.groupid = cpu_to_le32(td->groupid);
1201 convert_thread_options_to_net(&pdu.top, &td->o);
Jens Axboe807f9972012-03-02 10:25:24 +01001202
Jens Axboe40c60512012-03-27 16:03:04 +02001203 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL);
Jens Axboe807f9972012-03-02 10:25:24 +01001204}
1205
Jens Axboe122c7722012-03-19 09:13:15 +01001206void fio_server_send_start(struct thread_data *td)
1207{
1208 assert(server_fd != -1);
1209
1210 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1211}
1212
Jens Axboe87aa8f12011-10-06 21:24:13 +02001213static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +02001214{
Jens Axboe811826b2011-10-24 09:11:50 +02001215 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001216 socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001217 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +02001218
Jens Axboe811826b2011-10-24 09:11:50 +02001219 if (use_ipv6)
1220 sk = socket(AF_INET6, SOCK_STREAM, 0);
1221 else
1222 sk = socket(AF_INET, SOCK_STREAM, 0);
1223
Jens Axboe81179ee2011-10-04 12:42:06 +02001224 if (sk < 0) {
1225 log_err("fio: socket: %s\n", strerror(errno));
1226 return -1;
1227 }
1228
1229 opt = 1;
Jens Axboe1f819912013-01-23 17:21:41 -07001230 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001231 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001232 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001233 return -1;
1234 }
1235#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +02001236 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001237 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001238 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001239 return -1;
1240 }
1241#endif
1242
Jens Axboe811826b2011-10-24 09:11:50 +02001243 if (use_ipv6) {
1244 addr = (struct sockaddr *) &saddr_in6;
1245 socklen = sizeof(saddr_in6);
1246 saddr_in6.sin6_family = AF_INET6;
1247 } else {
1248 addr = (struct sockaddr *) &saddr_in;
1249 socklen = sizeof(saddr_in);
1250 saddr_in.sin_family = AF_INET;
1251 }
Jens Axboe81179ee2011-10-04 12:42:06 +02001252
Jens Axboe811826b2011-10-24 09:11:50 +02001253 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001254 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001255 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +02001256 return -1;
1257 }
1258
Jens Axboe87aa8f12011-10-06 21:24:13 +02001259 return sk;
1260}
1261
1262static int fio_init_server_sock(void)
1263{
1264 struct sockaddr_un addr;
Jens Axboe67bf9822013-01-10 11:23:19 +01001265 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001266 mode_t mode;
1267 int sk;
1268
1269 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1270 if (sk < 0) {
1271 log_err("fio: socket: %s\n", strerror(errno));
1272 return -1;
1273 }
1274
1275 mode = umask(000);
1276
1277 memset(&addr, 0, sizeof(addr));
1278 addr.sun_family = AF_UNIX;
1279 strcpy(addr.sun_path, bind_sock);
1280 unlink(bind_sock);
1281
1282 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1283
1284 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1285 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001286 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001287 return -1;
1288 }
1289
1290 umask(mode);
1291 return sk;
1292}
1293
1294static int fio_init_server_connection(void)
1295{
Jens Axboebebe6392011-10-07 10:00:51 +02001296 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001297 int sk;
1298
1299 dprint(FD_NET, "starting server\n");
1300
1301 if (!bind_sock)
1302 sk = fio_init_server_ip();
1303 else
1304 sk = fio_init_server_sock();
1305
1306 if (sk < 0)
1307 return sk;
1308
Jens Axboe811826b2011-10-24 09:11:50 +02001309 if (!bind_sock) {
1310 char *p, port[16];
1311 const void *src;
1312 int af;
1313
1314 if (use_ipv6) {
1315 af = AF_INET6;
1316 src = &saddr_in6.sin6_addr;
1317 } else {
1318 af = AF_INET;
1319 src = &saddr_in.sin_addr;
1320 }
1321
1322 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1323
1324 sprintf(port, ",%u", fio_net_port);
1325 if (p)
1326 strcat(p, port);
1327 else
1328 strcpy(bind_str, port);
1329 } else
Jens Axboebebe6392011-10-07 10:00:51 +02001330 strcpy(bind_str, bind_sock);
1331
1332 log_info("fio: server listening on %s\n", bind_str);
1333
Jens Axboe89c17072011-10-11 10:15:51 +02001334 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001335 log_err("fio: listen: %s\n", strerror(errno));
1336 return -1;
1337 }
1338
Jens Axboe87aa8f12011-10-06 21:24:13 +02001339 return sk;
1340}
1341
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001342int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1343 struct in6_addr *inp6)
1344
1345{
1346 int ret = 0;
1347
1348 if (*ipv6)
1349 ret = inet_pton(AF_INET6, host, inp6);
1350 else
1351 ret = inet_pton(AF_INET, host, inp);
1352
1353 if (ret != 1) {
1354 struct hostent *hent;
1355
1356 hent = gethostbyname(host);
1357 if (!hent) {
1358 log_err("fio: failed to resolve <%s>\n", host);
1359 return 0;
1360 }
1361
1362 if (*ipv6) {
1363 if (hent->h_addrtype != AF_INET6) {
1364 log_info("fio: falling back to IPv4\n");
1365 *ipv6 = 0;
1366 } else
1367 memcpy(inp6, hent->h_addr_list[0], 16);
1368 }
1369 if (!*ipv6) {
1370 if (hent->h_addrtype != AF_INET) {
1371 log_err("fio: lookup type mismatch\n");
1372 return 0;
1373 }
1374 memcpy(inp, hent->h_addr_list[0], 4);
1375 }
1376 ret = 1;
1377 }
1378
1379 return !(ret == 1);
1380}
1381
Jens Axboe660a2bf2011-10-24 09:35:06 +02001382/*
1383 * Parse a host/ip/port string. Reads from 'str'.
1384 *
1385 * Outputs:
1386 *
1387 * For IPv4:
1388 * *ptr is the host, *port is the port, inp is the destination.
1389 * For IPv6:
1390 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1391 * For local domain sockets:
1392 * *ptr is the filename, *is_sock is 1.
1393 */
Jens Axboebebe6392011-10-07 10:00:51 +02001394int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001395 int *port, struct in_addr *inp,
1396 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001397{
Jens Axboe76867622011-10-25 09:52:51 +02001398 const char *host = str;
1399 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001400 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001401
Jens Axboebebe6392011-10-07 10:00:51 +02001402 *ptr = NULL;
1403 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001404 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001405 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001406
1407 if (!strncmp(str, "sock:", 5)) {
1408 *ptr = strdup(str + 5);
1409 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001410
Jens Axboe76867622011-10-25 09:52:51 +02001411 return 0;
1412 }
1413
1414 /*
1415 * Is it ip:<ip or host>:port
1416 */
1417 if (!strncmp(host, "ip:", 3))
1418 host += 3;
1419 else if (!strncmp(host, "ip4:", 4))
1420 host += 4;
1421 else if (!strncmp(host, "ip6:", 4)) {
1422 host += 4;
1423 *ipv6 = 1;
1424 } else if (host[0] == ':') {
1425 /* String is :port */
1426 host++;
1427 lport = atoi(host);
1428 if (!lport || lport > 65535) {
1429 log_err("fio: bad server port %u\n", port);
1430 return 1;
1431 }
1432 /* no hostname given, we are done */
1433 *port = lport;
1434 return 0;
1435 }
1436
1437 /*
1438 * If no port seen yet, check if there's a last ':' at the end
1439 */
1440 if (!lport) {
1441 portp = strchr(host, ',');
1442 if (portp) {
1443 *portp = '\0';
1444 portp++;
1445 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001446 if (!lport || lport > 65535) {
1447 log_err("fio: bad server port %u\n", port);
1448 return 1;
1449 }
Jens Axboe76867622011-10-25 09:52:51 +02001450 }
1451 }
1452
1453 if (lport)
1454 *port = lport;
1455
1456 if (!strlen(host))
1457 return 0;
1458
1459 *ptr = strdup(host);
1460
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001461 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1462 free(*ptr);
1463 *ptr = NULL;
1464 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001465 }
1466
1467 if (*port == 0)
1468 *port = fio_net_port;
1469
1470 return 0;
1471}
1472
Jens Axboe87aa8f12011-10-06 21:24:13 +02001473/*
1474 * Server arg should be one of:
1475 *
1476 * sock:/path/to/socket
1477 * ip:1.2.3.4
1478 * 1.2.3.4
1479 *
1480 * Where sock uses unix domain sockets, and ip binds the server to
1481 * a specific interface. If no arguments are given to the server, it
1482 * uses IP and binds to 0.0.0.0.
1483 *
1484 */
1485static int fio_handle_server_arg(void)
1486{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001487 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001488 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001489
Jens Axboe87aa8f12011-10-06 21:24:13 +02001490 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1491
1492 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001493 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001494
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001495 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001496 &port, &saddr_in.sin_addr,
1497 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001498
1499 if (!is_sock && bind_sock) {
1500 free(bind_sock);
1501 bind_sock = NULL;
1502 }
1503
Jens Axboea7de0a12011-10-07 12:55:14 +02001504out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001505 fio_net_port = port;
1506 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001507 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001508 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001509}
1510
1511static int fio_server(void)
1512{
1513 int sk, ret;
1514
1515 dprint(FD_NET, "starting server\n");
1516
1517 if (fio_handle_server_arg())
1518 return -1;
1519
1520 sk = fio_init_server_connection();
1521 if (sk < 0)
1522 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001523
1524 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001525
Jens Axboe81179ee2011-10-04 12:42:06 +02001526 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001527
1528 if (fio_server_arg) {
1529 free(fio_server_arg);
1530 fio_server_arg = NULL;
1531 }
Jens Axboebebe6392011-10-07 10:00:51 +02001532 if (bind_sock)
1533 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001534
Jens Axboe81179ee2011-10-04 12:42:06 +02001535 return ret;
1536}
1537
Jens Axboe7b821682011-10-11 12:16:32 +02001538void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001539{
Jens Axboe7b821682011-10-11 12:16:32 +02001540 if (signal == SIGPIPE)
1541 server_fd = -1;
1542 else {
1543 log_info("\nfio: terminating on signal %d\n", signal);
1544 exit_backend = 1;
1545 }
Jens Axboe9abea482011-10-04 13:21:54 +02001546}
1547
Jens Axboe13755d92011-10-10 19:51:26 +02001548static int check_existing_pidfile(const char *pidfile)
1549{
1550 struct stat sb;
1551 char buf[16];
1552 pid_t pid;
1553 FILE *f;
1554
1555 if (stat(pidfile, &sb))
1556 return 0;
1557
1558 f = fopen(pidfile, "r");
1559 if (!f)
1560 return 0;
1561
Jens Axboebfc3b172011-10-10 21:16:55 +02001562 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001563 fclose(f);
1564 return 1;
1565 }
1566 fclose(f);
1567
1568 pid = atoi(buf);
1569 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001570 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001571
1572 return 1;
1573}
1574
1575static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001576{
1577 FILE *fpid;
1578
1579 fpid = fopen(pidfile, "w");
1580 if (!fpid) {
1581 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001582 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001583 }
1584
1585 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001586 fclose(fpid);
1587 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001588}
1589
1590/*
1591 * If pidfile is specified, background us.
1592 */
1593int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001594{
1595 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001596 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001597
Bruce Cran93bcfd22012-02-20 20:18:19 +01001598#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001599 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +02001600 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001601#endif
1602
Jens Axboe402668f2011-10-10 15:28:58 +02001603 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001604 return fio_server();
1605
Jens Axboe13755d92011-10-10 19:51:26 +02001606 if (check_existing_pidfile(pidfile)) {
1607 log_err("fio: pidfile %s exists and server appears alive\n",
1608 pidfile);
1609 return -1;
1610 }
1611
Jens Axboee46d8092011-10-03 09:11:02 +02001612 pid = fork();
1613 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001614 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001615 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001616 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001617 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001618 int ret = write_pid(pid, pidfile);
1619
1620 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001621 }
Jens Axboee46d8092011-10-03 09:11:02 +02001622
1623 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001624 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1625 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001626 close(STDIN_FILENO);
1627 close(STDOUT_FILENO);
1628 close(STDERR_FILENO);
1629 f_out = NULL;
1630 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001631
1632 ret = fio_server();
1633
1634 closelog();
1635 unlink(pidfile);
1636 free(pidfile);
1637 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001638}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001639
Jens Axboebebe6392011-10-07 10:00:51 +02001640void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001641{
1642 fio_server_arg = strdup(arg);
1643}