blob: 116b3d605d8da1aac6c9b39ee0469cad9db51817 [file] [log] [blame]
Jens Axboe50d16972011-09-29 17:45:28 -06001#include <stdio.h>
2#include <stdlib.h>
Jens Axboe142575e2011-09-30 17:35:45 -06003#include <stdarg.h>
Jens Axboe50d16972011-09-29 17:45:28 -06004#include <unistd.h>
5#include <limits.h>
Jens Axboe50d16972011-09-29 17:45:28 -06006#include <errno.h>
7#include <fcntl.h>
8#include <sys/poll.h>
Jens Axboe50d16972011-09-29 17:45:28 -06009#include <sys/types.h>
10#include <sys/wait.h>
Jens Axboed05c4a02011-10-05 00:16:11 +020011#include <sys/socket.h>
Jens Axboe87aa8f12011-10-06 21:24:13 +020012#include <sys/stat.h>
13#include <sys/un.h>
Jens Axboe50d16972011-09-29 17:45:28 -060014#include <netinet/in.h>
15#include <arpa/inet.h>
16#include <netdb.h>
Jens Axboee46d8092011-10-03 09:11:02 +020017#include <syslog.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020018#include <signal.h>
Jens Axboe1b427252012-03-14 15:03:03 +010019#include <zlib.h>
Jens Axboe50d16972011-09-29 17:45:28 -060020
21#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060022#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060023#include "crc/crc16.h"
Jens Axboec7c6cb42011-10-13 14:12:40 +020024#include "lib/ieee754.h"
Jens Axboe50d16972011-09-29 17:45:28 -060025
Jens Axboe89cf1482011-10-07 10:10:18 +020026#include "fio_version.h"
27
Stephen M. Cameron5adc2442012-02-24 08:17:31 +010028int fio_net_port = FIO_NET_PORT;
Jens Axboe50d16972011-09-29 17:45:28 -060029
Jens Axboe009b1be2011-09-29 18:27:02 -060030int exit_backend = 0;
31
Jens Axboe46c48f12011-10-01 12:50:51 -060032static int server_fd = -1;
Jens Axboe87aa8f12011-10-06 21:24:13 +020033static char *fio_server_arg;
34static char *bind_sock;
35static struct sockaddr_in saddr_in;
Jens Axboe811826b2011-10-24 09:11:50 +020036static struct sockaddr_in6 saddr_in6;
Jens Axboe01be0382011-10-15 14:43:41 +020037static int first_cmd_check;
Jens Axboe811826b2011-10-24 09:11:50 +020038static int use_ipv6;
Jens Axboe37db14f2011-09-30 17:00:42 -060039
Jens Axboe89c17072011-10-11 10:15:51 +020040static const char *fio_server_ops[FIO_NET_CMD_NR] = {
41 "",
42 "QUIT",
43 "EXIT",
44 "JOB",
45 "JOBLINE",
46 "TEXT",
47 "TS",
48 "GS",
49 "SEND_ETA",
50 "ETA",
51 "PROBE",
52 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020053 "STOP",
54 "DISK_UTIL",
Jens Axboeb9d2f302012-03-08 20:36:28 +010055 "SERVER_START",
Jens Axboe807f9972012-03-02 10:25:24 +010056 "ADD_JOB",
Jens Axboeb9d2f302012-03-08 20:36:28 +010057 "CMD_RUN"
Jens Axboe89c17072011-10-11 10:15:51 +020058};
59
60const char *fio_server_op(unsigned int op)
61{
62 static char buf[32];
63
64 if (op < FIO_NET_CMD_NR)
65 return fio_server_ops[op];
66
67 sprintf(buf, "UNKNOWN/%d", op);
68 return buf;
69}
70
Jens Axboe132159a2011-09-30 15:01:32 -060071int fio_send_data(int sk, const void *p, unsigned int len)
72{
Jens Axboeb9d2f302012-03-08 20:36:28 +010073 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
Jens Axboe794d69c2011-10-01 08:48:50 -060074
Jens Axboe132159a2011-09-30 15:01:32 -060075 do {
76 int ret = send(sk, p, len, 0);
77
78 if (ret > 0) {
79 len -= ret;
80 if (!len)
81 break;
82 p += ret;
83 continue;
84 } else if (!ret)
85 break;
86 else if (errno == EAGAIN || errno == EINTR)
87 continue;
Jens Axboe7b821682011-10-11 12:16:32 +020088 else
89 break;
Jens Axboe132159a2011-09-30 15:01:32 -060090 } while (!exit_backend);
91
92 if (!len)
93 return 0;
94
Jens Axboe8b4e61b2012-03-09 17:10:54 +010095 if (errno)
96 return -errno;
97
Jens Axboe132159a2011-09-30 15:01:32 -060098 return 1;
99}
100
101int fio_recv_data(int sk, void *p, unsigned int len)
102{
103 do {
104 int ret = recv(sk, p, len, MSG_WAITALL);
105
106 if (ret > 0) {
107 len -= ret;
108 if (!len)
109 break;
110 p += ret;
111 continue;
112 } else if (!ret)
113 break;
114 else if (errno == EAGAIN || errno == EINTR)
115 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200116 else
117 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600118 } while (!exit_backend);
119
120 if (!len)
121 return 0;
122
123 return -1;
124}
125
126static int verify_convert_cmd(struct fio_net_cmd *cmd)
127{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600128 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600129
Jens Axboefcee5ff2011-09-30 22:50:39 -0600130 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
131 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600132
Jens Axboe25dfa842012-02-29 10:01:34 +0100133 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600134 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600135 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600136 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600137 return 1;
138 }
139
140 cmd->version = le16_to_cpu(cmd->version);
141 cmd->opcode = le16_to_cpu(cmd->opcode);
142 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200143 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600144 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
145
146 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200147 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600148 break;
149 default:
150 log_err("fio: bad server cmd version %d\n", cmd->version);
151 return 1;
152 }
153
Jens Axboeb9d2f302012-03-08 20:36:28 +0100154 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600155 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
156 return 1;
157 }
158
159 return 0;
160}
161
Jens Axboea64e88d2011-10-03 14:20:01 +0200162/*
163 * Read (and defragment, if necessary) incoming commands
164 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200165struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600166{
Jens Axboea64e88d2011-10-03 14:20:01 +0200167 struct fio_net_cmd cmd, *cmdret = NULL;
168 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600169 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200170 int ret, first = 1;
171 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600172
Jens Axboea64e88d2011-10-03 14:20:01 +0200173 do {
174 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
175 if (ret)
176 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600177
Jens Axboea64e88d2011-10-03 14:20:01 +0200178 /* We have a command, verify it and swap if need be */
179 ret = verify_convert_cmd(&cmd);
180 if (ret)
181 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600182
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200183 if (first) {
184 /* if this is text, add room for \0 at the end */
185 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
186 assert(!cmdret);
187 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200188 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600189
Jens Axboea64e88d2011-10-03 14:20:01 +0200190 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600191
Jens Axboea64e88d2011-10-03 14:20:01 +0200192 if (first)
193 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200194 else if (cmdret->opcode != cmd.opcode) {
195 log_err("fio: fragment opcode mismatch (%d != %d)\n",
196 cmdret->opcode, cmd.opcode);
197 ret = 1;
198 break;
199 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200200
201 if (!cmd.pdu_len)
202 break;
203
204 /* There's payload, get it */
205 pdu = (void *) cmdret->payload + pdu_offset;
206 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
207 if (ret)
208 break;
209
210 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100211 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200212 if (crc != cmd.pdu_crc16) {
213 log_err("fio: server bad crc on payload ");
214 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
215 ret = 1;
216 break;
217 }
218
219 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200220 if (!first)
221 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200222 first = 0;
223 } while (cmd.flags & FIO_NET_CMD_F_MORE);
224
225 if (ret) {
226 free(cmdret);
227 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200228 } else if (cmdret) {
229 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100230 if (cmdret->pdu_len) {
231 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
232 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
233 char *buf = (char *) pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200234
Jens Axboe084d1c62012-03-03 20:28:07 +0100235 buf[pdu->buf_len ] = '\0';
236 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
Jens Axboe46bcd492012-03-14 11:31:21 +0100237 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
238 char *buf = (char *) pdu->buf;
239 int len = le32_to_cpu(pdu->buf_len);
Jens Axboe084d1c62012-03-03 20:28:07 +0100240
Jens Axboe46bcd492012-03-14 11:31:21 +0100241 buf[len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100242 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200243 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100244
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200245 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200246 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200247 }
Jens Axboe132159a2011-09-30 15:01:32 -0600248
Jens Axboea64e88d2011-10-03 14:20:01 +0200249 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600250}
251
Jens Axboe1b427252012-03-14 15:03:03 +0100252void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, void *pdu)
Jens Axboe132159a2011-09-30 15:01:32 -0600253{
254 uint32_t pdu_len;
255
Jens Axboe25dfa842012-02-29 10:01:34 +0100256 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600257
258 pdu_len = le32_to_cpu(cmd->pdu_len);
Jens Axboe1b427252012-03-14 15:03:03 +0100259 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
260}
261
262void fio_net_cmd_crc(struct fio_net_cmd *cmd)
263{
264 fio_net_cmd_crc_pdu(cmd, cmd->payload);
Jens Axboe132159a2011-09-30 15:01:32 -0600265}
266
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200267int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
268 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600269{
Jens Axboe7f868312011-10-10 09:55:21 +0200270 struct fio_net_cmd *cmd = NULL;
271 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600272 int ret;
273
274 do {
275 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100276 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
277 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600278
Jens Axboe7f868312011-10-10 09:55:21 +0200279 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
280 if (cmd)
281 free(cmd);
282
283 cur_len = sizeof(*cmd) + this_len;
284 cmd = malloc(cur_len);
285 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600286
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200287 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600288
289 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200290 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600291
292 fio_net_cmd_crc(cmd);
293
294 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600295 size -= this_len;
296 buf += this_len;
297 } while (!ret && size);
298
Jens Axboe7f868312011-10-10 09:55:21 +0200299 if (cmd)
300 free(cmd);
301
Jens Axboe794d69c2011-10-01 08:48:50 -0600302 return ret;
303}
304
Jens Axboe89c17072011-10-11 10:15:51 +0200305static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600306{
Jens Axboe178cde92011-10-05 22:14:31 +0200307 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600308
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200309 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600310 fio_net_cmd_crc(&cmd);
311
312 return fio_send_data(sk, &cmd, sizeof(cmd));
313}
314
Jens Axboe89c17072011-10-11 10:15:51 +0200315/*
316 * If 'list' is non-NULL, then allocate and store the sent command for
317 * later verification.
318 */
319int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
320 struct flist_head *list)
321{
322 struct fio_net_int_cmd *cmd;
323 int ret;
324
325 if (!list)
326 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
327
328 cmd = malloc(sizeof(*cmd));
329
Jens Axboedf380932011-10-11 14:25:08 +0200330 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
Jens Axboe89c17072011-10-11 10:15:51 +0200331 fio_net_cmd_crc(&cmd->cmd);
332
333 INIT_FLIST_HEAD(&cmd->list);
334 gettimeofday(&cmd->tv, NULL);
335 cmd->saved_tag = tag;
336
337 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
338 if (ret) {
339 free(cmd);
340 return ret;
341 }
342
343 flist_add_tail(&cmd->list, list);
344 return 0;
345}
346
Jens Axboe9abea482011-10-04 13:21:54 +0200347static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600348{
Jens Axboe46c48f12011-10-01 12:50:51 -0600349 dprint(FD_NET, "server: sending quit\n");
Jens Axboe89c17072011-10-11 10:15:51 +0200350 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600351}
352
Jens Axboeb9d2f302012-03-08 20:36:28 +0100353static int handle_run_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600354{
Jens Axboe11e950b2011-10-16 21:34:14 +0200355 struct cmd_end_pdu epdu;
Jens Axboea64e88d2011-10-03 14:20:01 +0200356 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600357
Jens Axboe2e1df072012-02-09 11:15:02 +0100358 ret = fio_backend();
Jens Axboe11e950b2011-10-16 21:34:14 +0200359
360 epdu.error = ret;
361 fio_net_send_cmd(server_fd, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0);
362
Jens Axboe9abea482011-10-04 13:21:54 +0200363 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200364 reset_fio_state();
Jens Axboe8663ea62012-03-02 14:04:30 +0100365 first_cmd_check = 0;
Jens Axboe81179ee2011-10-04 12:42:06 +0200366 return ret;
367}
368
Jens Axboeb9d2f302012-03-08 20:36:28 +0100369static int handle_job_cmd(struct fio_net_cmd *cmd)
370{
Jens Axboe46bcd492012-03-14 11:31:21 +0100371 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
372 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100373 struct cmd_start_pdu spdu;
374
Jens Axboe46bcd492012-03-14 11:31:21 +0100375 pdu->buf_len = le32_to_cpu(pdu->buf_len);
376 pdu->client_type = le32_to_cpu(pdu->client_type);
377
378 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboeb9d2f302012-03-08 20:36:28 +0100379 fio_server_send_quit_cmd();
380 return -1;
381 }
382
383 spdu.jobs = cpu_to_le32(thread_number);
384 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
385 return 0;
386}
387
Jens Axboe81179ee2011-10-04 12:42:06 +0200388static int handle_jobline_cmd(struct fio_net_cmd *cmd)
389{
Jens Axboefa2ea802011-10-08 21:07:29 +0200390 void *pdu = cmd->payload;
391 struct cmd_single_line_pdu *cslp;
392 struct cmd_line_pdu *clp;
393 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100394 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200395 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100396 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200397
Jens Axboefa2ea802011-10-08 21:07:29 +0200398 clp = pdu;
399 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100400 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200401 argv = malloc(clp->lines * sizeof(char *));
402 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200403
Jens Axboefa2ea802011-10-08 21:07:29 +0200404 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200405
Jens Axboefa2ea802011-10-08 21:07:29 +0200406 for (i = 0; i < clp->lines; i++) {
407 cslp = pdu + offset;
408 argv[i] = (char *) cslp->text;
409
410 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200411 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
412 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200413
Jens Axboe46bcd492012-03-14 11:31:21 +0100414 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200415 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200416 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200417 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200418 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200419
Jens Axboefa2ea802011-10-08 21:07:29 +0200420 free(argv);
421
Jens Axboeb9d2f302012-03-08 20:36:28 +0100422 spdu.jobs = cpu_to_le32(thread_number);
423 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
424 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600425}
426
Jens Axboec28e8e82011-10-04 08:57:39 +0200427static int handle_probe_cmd(struct fio_net_cmd *cmd)
428{
429 struct cmd_probe_pdu probe;
430
Jens Axboe89c17072011-10-11 10:15:51 +0200431 dprint(FD_NET, "server: sending probe reply\n");
432
Jens Axboec28e8e82011-10-04 08:57:39 +0200433 memset(&probe, 0, sizeof(probe));
434 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200435#ifdef FIO_BIG_ENDIAN
436 probe.bigendian = 1;
437#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200438 probe.fio_major = FIO_MAJOR;
439 probe.fio_minor = FIO_MINOR;
440 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200441
Jens Axboecca84642011-10-07 12:47:57 +0200442 probe.os = FIO_OS;
443 probe.arch = FIO_ARCH;
444
Jens Axboe38fdef22011-10-11 14:30:06 +0200445 probe.bpp = sizeof(void *);
446
Jens Axboe89c17072011-10-11 10:15:51 +0200447 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200448}
449
450static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
451{
452 struct jobs_eta *je;
453 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200454 int i;
455
Jens Axboeb814fb22011-10-12 09:20:34 +0200456 if (!thread_number)
457 return 0;
458
459 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200460 je = malloc(size);
461 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200462
463 if (!calc_thread_status(je, 1)) {
464 free(je);
465 return 0;
466 }
467
468 dprint(FD_NET, "server sending status\n");
469
470 je->nr_running = cpu_to_le32(je->nr_running);
471 je->nr_ramp = cpu_to_le32(je->nr_ramp);
472 je->nr_pending = cpu_to_le32(je->nr_pending);
473 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200474
475 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100476 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
477 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
478 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
479 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200480 je->rate[i] = cpu_to_le32(je->rate[i]);
481 je->iops[i] = cpu_to_le32(je->iops[i]);
482 }
483
Jens Axboec1970b92012-03-06 15:37:40 +0100484 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200485 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100486 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200487
Jens Axboe7f868312011-10-10 09:55:21 +0200488 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200489 free(je);
490 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200491}
492
Jens Axboe132159a2011-09-30 15:01:32 -0600493static int handle_command(struct fio_net_cmd *cmd)
494{
495 int ret;
496
Jens Axboe89c17072011-10-11 10:15:51 +0200497 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
498 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600499
Jens Axboe132159a2011-09-30 15:01:32 -0600500 switch (cmd->opcode) {
501 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200502 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200503 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200504 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600505 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200506 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600507 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200508 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600509 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200510 case FIO_NET_CMD_JOBLINE:
511 ret = handle_jobline_cmd(cmd);
512 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200513 case FIO_NET_CMD_PROBE:
514 ret = handle_probe_cmd(cmd);
515 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200516 case FIO_NET_CMD_SEND_ETA:
517 ret = handle_send_eta_cmd(cmd);
518 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100519 case FIO_NET_CMD_RUN:
520 ret = handle_run_cmd(cmd);
521 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600522 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200523 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600524 ret = 1;
525 }
526
527 return ret;
528}
529
Jens Axboe70e0c312011-10-04 09:18:30 +0200530static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600531{
532 struct fio_net_cmd *cmd = NULL;
533 int ret = 0;
534
535 /* read forever */
536 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200537 struct pollfd pfd = {
538 .fd = sk,
539 .events = POLLIN,
540 };
541
542 ret = 0;
543 do {
544 ret = poll(&pfd, 1, 100);
545 if (ret < 0) {
546 if (errno == EINTR)
547 break;
548 log_err("fio: poll: %s\n", strerror(errno));
549 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200550 } else if (!ret) {
551 if (!block)
552 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200553 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200554 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200555
556 if (pfd.revents & POLLIN)
557 break;
558 if (pfd.revents & (POLLERR|POLLHUP)) {
559 ret = 1;
560 break;
561 }
Jens Axboe19c65172011-10-05 22:05:37 +0200562 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200563
564 if (ret < 0)
565 break;
566
567 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600568 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200569 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600570 break;
571 }
572
Jens Axboe132159a2011-09-30 15:01:32 -0600573 ret = handle_command(cmd);
574 if (ret)
575 break;
576
577 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400578 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600579 }
580
581 if (cmd)
582 free(cmd);
583
584 return ret;
585}
586
Jens Axboecc0df002011-10-03 20:53:32 +0200587void fio_server_idle_loop(void)
588{
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100589 if (!first_cmd_check) {
Jens Axboe5d7793a2012-03-08 19:59:16 +0100590 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100591 first_cmd_check = 1;
592 }
Jens Axboecc0df002011-10-03 20:53:32 +0200593 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200594 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200595}
596
Jens Axboe50d16972011-09-29 17:45:28 -0600597static int accept_loop(int listen_sk)
598{
Jens Axboebb447a22011-10-04 15:06:42 +0200599 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200600 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600601 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600602 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600603
Jens Axboe60efd142011-10-04 13:30:11 +0200604 dprint(FD_NET, "server enter accept loop\n");
605
Jens Axboe009b1be2011-09-29 18:27:02 -0600606 flags = fcntl(listen_sk, F_GETFL);
607 flags |= O_NONBLOCK;
608 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600609again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600610 pfd.fd = listen_sk;
611 pfd.events = POLLIN;
612 do {
613 ret = poll(&pfd, 1, 100);
614 if (ret < 0) {
615 if (errno == EINTR)
616 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600617 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600618 goto out;
619 } else if (!ret)
620 continue;
621
622 if (pfd.revents & POLLIN)
623 break;
624 } while (!exit_backend);
625
626 if (exit_backend)
627 goto out;
628
Jens Axboe6b976bf2011-10-04 15:07:43 +0200629 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600630 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600631 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600632 return -1;
633 }
634
Jens Axboebb447a22011-10-04 15:06:42 +0200635 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600636
Jens Axboe37db14f2011-09-30 17:00:42 -0600637 server_fd = sk;
638
Jens Axboe70e0c312011-10-04 09:18:30 +0200639 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600640
Jens Axboe37db14f2011-09-30 17:00:42 -0600641 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600642 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600643
Jens Axboe009b1be2011-09-29 18:27:02 -0600644 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600645 goto again;
646
Jens Axboe009b1be2011-09-29 18:27:02 -0600647out:
Jens Axboe132159a2011-09-30 15:01:32 -0600648 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600649}
650
Jens Axboe084d1c62012-03-03 20:28:07 +0100651int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600652{
Jens Axboe084d1c62012-03-03 20:28:07 +0100653 struct cmd_text_pdu *pdu;
654 unsigned int tlen;
655 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600656
Jens Axboe084d1c62012-03-03 20:28:07 +0100657 if (server_fd == -1)
658 return log_local_buf(buf, len);
659
660 tlen = sizeof(*pdu) + len;
661 pdu = malloc(tlen);
662
663 pdu->level = __cpu_to_le32(level);
664 pdu->buf_len = __cpu_to_le32(len);
665
666 gettimeofday(&tv, NULL);
667 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
668 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
669
670 memcpy(pdu->buf, buf, len);
671
672 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, 0);
673 free(pdu);
674 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600675}
676
Jens Axboea64e88d2011-10-03 14:20:01 +0200677static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
678{
679 dst->max_val = cpu_to_le64(src->max_val);
680 dst->min_val = cpu_to_le64(src->min_val);
681 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200682
683 /*
684 * Encode to IEEE 754 for network transfer
685 */
686 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
687 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200688}
689
690static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
691{
692 int i;
693
694 for (i = 0; i < 2; i++) {
695 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
696 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
697 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
698 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
699 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
700 dst->agg[i] = cpu_to_le64(src->agg[i]);
701 }
702
703 dst->kb_base = cpu_to_le32(src->kb_base);
704 dst->groupid = cpu_to_le32(src->groupid);
705}
706
707/*
708 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
709 * into a single payload.
710 */
711void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
712{
713 struct cmd_ts_pdu p;
714 int i, j;
715
Jens Axboe60efd142011-10-04 13:30:11 +0200716 dprint(FD_NET, "server sending end stats\n");
717
Jens Axboe317b3c82011-10-03 21:47:27 +0200718 memset(&p, 0, sizeof(p));
719
Jens Axboea64e88d2011-10-03 14:20:01 +0200720 strcpy(p.ts.name, ts->name);
721 strcpy(p.ts.verror, ts->verror);
722 strcpy(p.ts.description, ts->description);
723
Jens Axboeddcc0b62011-10-03 14:45:27 +0200724 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200725 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200726 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200727 p.ts.members = cpu_to_le32(ts->members);
728
729 for (i = 0; i < 2; i++) {
730 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
731 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
732 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
733 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
734 }
735
736 p.ts.usr_time = cpu_to_le64(ts->usr_time);
737 p.ts.sys_time = cpu_to_le64(ts->sys_time);
738 p.ts.ctx = cpu_to_le64(ts->ctx);
739 p.ts.minf = cpu_to_le64(ts->minf);
740 p.ts.majf = cpu_to_le64(ts->majf);
741 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200742
743 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200744 fio_fp64_t *src = &ts->percentile_list[i];
745 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200746
Jens Axboecfc03e42011-10-12 21:20:42 +0200747 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200748 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200749
750 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
751 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
752 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
753 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
754 }
755
756 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
757 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
758 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
759 }
760
761 for (i = 0; i < 2; i++)
762 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
763 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
764
765 for (i = 0; i < 3; i++) {
766 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200767 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200768 }
769
Jens Axboe93eee042011-10-03 21:08:48 +0200770 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200771 p.ts.total_complete = cpu_to_le64(ts->total_complete);
772
773 for (i = 0; i < 2; i++) {
774 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
775 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
776 }
777
778 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
779 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
780 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200781 p.ts.first_error = cpu_to_le32(ts->first_error);
782 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200783
784 convert_gs(&p.rs, rs);
785
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200786 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200787}
788
789void fio_server_send_gs(struct group_run_stats *rs)
790{
791 struct group_run_stats gs;
792
Jens Axboe60efd142011-10-04 13:30:11 +0200793 dprint(FD_NET, "server sending group run stats\n");
794
Jens Axboea64e88d2011-10-03 14:20:01 +0200795 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200796 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200797}
798
Jens Axboed09a64a2011-10-13 11:38:56 +0200799static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
800{
801 int i;
802
803 for (i = 0; i < 2; i++) {
804 dst->ios[i] = cpu_to_le32(src->ios[i]);
805 dst->merges[i] = cpu_to_le32(src->merges[i]);
806 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
807 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
808 }
809
810 dst->io_ticks = cpu_to_le32(src->io_ticks);
811 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
812 dst->slavecount = cpu_to_le32(src->slavecount);
813 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
814}
815
816static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
817{
818 int i;
819
820 strcpy((char *) dst->name, (char *) src->name);
821
822 for (i = 0; i < 2; i++) {
823 dst->ios[i] = cpu_to_le32(src->ios[i]);
824 dst->merges[i] = cpu_to_le32(src->merges[i]);
825 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
826 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
827 }
828
829 dst->io_ticks = cpu_to_le32(src->io_ticks);
830 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
831 dst->msec = cpu_to_le64(src->msec);
832}
833
834void fio_server_send_du(void)
835{
836 struct disk_util *du;
837 struct flist_head *entry;
838 struct cmd_du_pdu pdu;
839
840 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
841
Jens Axboe0766d922011-10-13 12:02:08 +0200842 memset(&pdu, 0, sizeof(pdu));
843
Jens Axboed09a64a2011-10-13 11:38:56 +0200844 flist_for_each(entry, &disk_list) {
845 du = flist_entry(entry, struct disk_util, list);
846
847 convert_dus(&pdu.dus, &du->dus);
848 convert_agg(&pdu.agg, &du->agg);
849
850 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
851 }
852}
853
Jens Axboe1b427252012-03-14 15:03:03 +0100854int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
855{
Jens Axboef5ed7652012-03-14 16:28:07 +0100856 struct cmd_iolog_pdu pdu;
Jens Axboe1b427252012-03-14 15:03:03 +0100857 struct fio_net_cmd cmd;
858 z_stream stream;
859 void *out_pdu;
Jens Axboe1b427252012-03-14 15:03:03 +0100860 int i;
861
Jens Axboef5ed7652012-03-14 16:28:07 +0100862 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
863 pdu.log_type = cpu_to_le32(log->log_type);
864 strcpy((char *) pdu.name, name);
Jens Axboe1b427252012-03-14 15:03:03 +0100865
866 for (i = 0; i < log->nr_samples; i++) {
Jens Axboef5ed7652012-03-14 16:28:07 +0100867 struct io_sample *s = &log->log[i];
Jens Axboe1b427252012-03-14 15:03:03 +0100868
Jens Axboef5ed7652012-03-14 16:28:07 +0100869 s->time = cpu_to_le64(s->time);
870 s->val = cpu_to_le64(s->val);
871 s->ddir = cpu_to_le32(s->ddir);
872 s->bs = cpu_to_le32(s->bs);
Jens Axboe1b427252012-03-14 15:03:03 +0100873 }
874
875 /*
876 * Dirty - since the log is potentially huge, compress it into
877 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
878 * side defragment it.
879 */
880 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
881
882 stream.zalloc = Z_NULL;
883 stream.zfree = Z_NULL;
884 stream.opaque = Z_NULL;
885
886 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
887 free(out_pdu);
Jens Axboe1b427252012-03-14 15:03:03 +0100888 return 1;
889 }
890
891 /*
Jens Axboef5ed7652012-03-14 16:28:07 +0100892 * Send header first, it's not compressed.
Jens Axboe1b427252012-03-14 15:03:03 +0100893 */
Jens Axboef5ed7652012-03-14 16:28:07 +0100894 __fio_init_net_cmd(&cmd, FIO_NET_CMD_IOLOG, sizeof(pdu), 0);
Jens Axboe1b427252012-03-14 15:03:03 +0100895 cmd.flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboef5ed7652012-03-14 16:28:07 +0100896 fio_net_cmd_crc_pdu(&cmd, &pdu);
Jens Axboe1b427252012-03-14 15:03:03 +0100897 fio_send_data(server_fd, &cmd, sizeof(cmd));
Jens Axboef5ed7652012-03-14 16:28:07 +0100898 fio_send_data(server_fd, &pdu, sizeof(pdu));
Jens Axboe1b427252012-03-14 15:03:03 +0100899
Jens Axboef5ed7652012-03-14 16:28:07 +0100900 stream.next_in = (void *) log->log;
901 stream.avail_in = log->nr_samples * sizeof(struct io_sample);
Jens Axboe1b427252012-03-14 15:03:03 +0100902
903 do {
904 unsigned int this_len;
905
906 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
907 stream.next_out = out_pdu;
Jens Axboef5ed7652012-03-14 16:28:07 +0100908 assert(deflate(&stream, Z_FINISH) == Z_OK);
Jens Axboe1b427252012-03-14 15:03:03 +0100909
910 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
911
912 __fio_init_net_cmd(&cmd, FIO_NET_CMD_IOLOG, this_len, 0);
913
914 if (stream.avail_in)
915 cmd.flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
916
917 fio_net_cmd_crc_pdu(&cmd, out_pdu);
918
919 fio_send_data(server_fd, &cmd, sizeof(cmd));
920 fio_send_data(server_fd, out_pdu, this_len);
921 } while (stream.avail_in);
922
Jens Axboe1b427252012-03-14 15:03:03 +0100923 free(out_pdu);
924 deflateEnd(&stream);
925 return 0;
926}
927
Jens Axboe807f9972012-03-02 10:25:24 +0100928void fio_server_send_add_job(struct thread_options *o, const char *ioengine)
929{
930 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +0100931
Jens Axboe731e30a2012-03-14 16:35:37 +0100932 memset(&pdu, 0, sizeof(pdu));
Jens Axboedcaeb602012-03-08 19:45:37 +0100933 convert_thread_options_to_net(&pdu.top, o);
Jens Axboe807f9972012-03-02 10:25:24 +0100934
935 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), 0);
936}
937
Jens Axboe87aa8f12011-10-06 21:24:13 +0200938static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200939{
Jens Axboe811826b2011-10-24 09:11:50 +0200940 struct sockaddr *addr;
941 fio_socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200942 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200943
Jens Axboe811826b2011-10-24 09:11:50 +0200944 if (use_ipv6)
945 sk = socket(AF_INET6, SOCK_STREAM, 0);
946 else
947 sk = socket(AF_INET, SOCK_STREAM, 0);
948
Jens Axboe81179ee2011-10-04 12:42:06 +0200949 if (sk < 0) {
950 log_err("fio: socket: %s\n", strerror(errno));
951 return -1;
952 }
953
954 opt = 1;
955 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
956 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200957 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200958 return -1;
959 }
960#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200961 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200962 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200963 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200964 return -1;
965 }
966#endif
967
Jens Axboe811826b2011-10-24 09:11:50 +0200968 if (use_ipv6) {
969 addr = (struct sockaddr *) &saddr_in6;
970 socklen = sizeof(saddr_in6);
971 saddr_in6.sin6_family = AF_INET6;
972 } else {
973 addr = (struct sockaddr *) &saddr_in;
974 socklen = sizeof(saddr_in);
975 saddr_in.sin_family = AF_INET;
976 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200977
Jens Axboe811826b2011-10-24 09:11:50 +0200978 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200979 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200980 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200981 return -1;
982 }
983
Jens Axboe87aa8f12011-10-06 21:24:13 +0200984 return sk;
985}
986
987static int fio_init_server_sock(void)
988{
989 struct sockaddr_un addr;
990 fio_socklen_t len;
991 mode_t mode;
992 int sk;
993
994 sk = socket(AF_UNIX, SOCK_STREAM, 0);
995 if (sk < 0) {
996 log_err("fio: socket: %s\n", strerror(errno));
997 return -1;
998 }
999
1000 mode = umask(000);
1001
1002 memset(&addr, 0, sizeof(addr));
1003 addr.sun_family = AF_UNIX;
1004 strcpy(addr.sun_path, bind_sock);
1005 unlink(bind_sock);
1006
1007 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1008
1009 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1010 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001011 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001012 return -1;
1013 }
1014
1015 umask(mode);
1016 return sk;
1017}
1018
1019static int fio_init_server_connection(void)
1020{
Jens Axboebebe6392011-10-07 10:00:51 +02001021 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +02001022 int sk;
1023
1024 dprint(FD_NET, "starting server\n");
1025
1026 if (!bind_sock)
1027 sk = fio_init_server_ip();
1028 else
1029 sk = fio_init_server_sock();
1030
1031 if (sk < 0)
1032 return sk;
1033
Jens Axboe811826b2011-10-24 09:11:50 +02001034 if (!bind_sock) {
1035 char *p, port[16];
1036 const void *src;
1037 int af;
1038
1039 if (use_ipv6) {
1040 af = AF_INET6;
1041 src = &saddr_in6.sin6_addr;
1042 } else {
1043 af = AF_INET;
1044 src = &saddr_in.sin_addr;
1045 }
1046
1047 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1048
1049 sprintf(port, ",%u", fio_net_port);
1050 if (p)
1051 strcat(p, port);
1052 else
1053 strcpy(bind_str, port);
1054 } else
Jens Axboebebe6392011-10-07 10:00:51 +02001055 strcpy(bind_str, bind_sock);
1056
1057 log_info("fio: server listening on %s\n", bind_str);
1058
Jens Axboe89c17072011-10-11 10:15:51 +02001059 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +02001060 log_err("fio: listen: %s\n", strerror(errno));
1061 return -1;
1062 }
1063
Jens Axboe87aa8f12011-10-06 21:24:13 +02001064 return sk;
1065}
1066
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001067int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1068 struct in6_addr *inp6)
1069
1070{
1071 int ret = 0;
1072
1073 if (*ipv6)
1074 ret = inet_pton(AF_INET6, host, inp6);
1075 else
1076 ret = inet_pton(AF_INET, host, inp);
1077
1078 if (ret != 1) {
1079 struct hostent *hent;
1080
1081 hent = gethostbyname(host);
1082 if (!hent) {
1083 log_err("fio: failed to resolve <%s>\n", host);
1084 return 0;
1085 }
1086
1087 if (*ipv6) {
1088 if (hent->h_addrtype != AF_INET6) {
1089 log_info("fio: falling back to IPv4\n");
1090 *ipv6 = 0;
1091 } else
1092 memcpy(inp6, hent->h_addr_list[0], 16);
1093 }
1094 if (!*ipv6) {
1095 if (hent->h_addrtype != AF_INET) {
1096 log_err("fio: lookup type mismatch\n");
1097 return 0;
1098 }
1099 memcpy(inp, hent->h_addr_list[0], 4);
1100 }
1101 ret = 1;
1102 }
1103
1104 return !(ret == 1);
1105}
1106
Jens Axboe660a2bf2011-10-24 09:35:06 +02001107/*
1108 * Parse a host/ip/port string. Reads from 'str'.
1109 *
1110 * Outputs:
1111 *
1112 * For IPv4:
1113 * *ptr is the host, *port is the port, inp is the destination.
1114 * For IPv6:
1115 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1116 * For local domain sockets:
1117 * *ptr is the filename, *is_sock is 1.
1118 */
Jens Axboebebe6392011-10-07 10:00:51 +02001119int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001120 int *port, struct in_addr *inp,
1121 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001122{
Jens Axboe76867622011-10-25 09:52:51 +02001123 const char *host = str;
1124 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001125 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001126
Jens Axboebebe6392011-10-07 10:00:51 +02001127 *ptr = NULL;
1128 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001129 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001130 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001131
1132 if (!strncmp(str, "sock:", 5)) {
1133 *ptr = strdup(str + 5);
1134 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001135
Jens Axboe76867622011-10-25 09:52:51 +02001136 return 0;
1137 }
1138
1139 /*
1140 * Is it ip:<ip or host>:port
1141 */
1142 if (!strncmp(host, "ip:", 3))
1143 host += 3;
1144 else if (!strncmp(host, "ip4:", 4))
1145 host += 4;
1146 else if (!strncmp(host, "ip6:", 4)) {
1147 host += 4;
1148 *ipv6 = 1;
1149 } else if (host[0] == ':') {
1150 /* String is :port */
1151 host++;
1152 lport = atoi(host);
1153 if (!lport || lport > 65535) {
1154 log_err("fio: bad server port %u\n", port);
1155 return 1;
1156 }
1157 /* no hostname given, we are done */
1158 *port = lport;
1159 return 0;
1160 }
1161
1162 /*
1163 * If no port seen yet, check if there's a last ':' at the end
1164 */
1165 if (!lport) {
1166 portp = strchr(host, ',');
1167 if (portp) {
1168 *portp = '\0';
1169 portp++;
1170 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001171 if (!lport || lport > 65535) {
1172 log_err("fio: bad server port %u\n", port);
1173 return 1;
1174 }
Jens Axboe76867622011-10-25 09:52:51 +02001175 }
1176 }
1177
1178 if (lport)
1179 *port = lport;
1180
1181 if (!strlen(host))
1182 return 0;
1183
1184 *ptr = strdup(host);
1185
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001186 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1187 free(*ptr);
1188 *ptr = NULL;
1189 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001190 }
1191
1192 if (*port == 0)
1193 *port = fio_net_port;
1194
1195 return 0;
1196}
1197
Jens Axboe87aa8f12011-10-06 21:24:13 +02001198/*
1199 * Server arg should be one of:
1200 *
1201 * sock:/path/to/socket
1202 * ip:1.2.3.4
1203 * 1.2.3.4
1204 *
1205 * Where sock uses unix domain sockets, and ip binds the server to
1206 * a specific interface. If no arguments are given to the server, it
1207 * uses IP and binds to 0.0.0.0.
1208 *
1209 */
1210static int fio_handle_server_arg(void)
1211{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001212 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001213 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001214
Jens Axboe87aa8f12011-10-06 21:24:13 +02001215 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1216
1217 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001218 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001219
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001220 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001221 &port, &saddr_in.sin_addr,
1222 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001223
1224 if (!is_sock && bind_sock) {
1225 free(bind_sock);
1226 bind_sock = NULL;
1227 }
1228
Jens Axboea7de0a12011-10-07 12:55:14 +02001229out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001230 fio_net_port = port;
1231 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001232 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001233 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001234}
1235
1236static int fio_server(void)
1237{
1238 int sk, ret;
1239
1240 dprint(FD_NET, "starting server\n");
1241
1242 if (fio_handle_server_arg())
1243 return -1;
1244
1245 sk = fio_init_server_connection();
1246 if (sk < 0)
1247 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001248
1249 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001250
Jens Axboe81179ee2011-10-04 12:42:06 +02001251 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001252
1253 if (fio_server_arg) {
1254 free(fio_server_arg);
1255 fio_server_arg = NULL;
1256 }
Jens Axboebebe6392011-10-07 10:00:51 +02001257 if (bind_sock)
1258 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001259
Jens Axboe81179ee2011-10-04 12:42:06 +02001260 return ret;
1261}
1262
Jens Axboe7b821682011-10-11 12:16:32 +02001263void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001264{
Jens Axboe7b821682011-10-11 12:16:32 +02001265 if (signal == SIGPIPE)
1266 server_fd = -1;
1267 else {
1268 log_info("\nfio: terminating on signal %d\n", signal);
1269 exit_backend = 1;
1270 }
Jens Axboe9abea482011-10-04 13:21:54 +02001271}
1272
Jens Axboe13755d92011-10-10 19:51:26 +02001273static int check_existing_pidfile(const char *pidfile)
1274{
1275 struct stat sb;
1276 char buf[16];
1277 pid_t pid;
1278 FILE *f;
1279
1280 if (stat(pidfile, &sb))
1281 return 0;
1282
1283 f = fopen(pidfile, "r");
1284 if (!f)
1285 return 0;
1286
Jens Axboebfc3b172011-10-10 21:16:55 +02001287 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001288 fclose(f);
1289 return 1;
1290 }
1291 fclose(f);
1292
1293 pid = atoi(buf);
1294 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001295 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001296
1297 return 1;
1298}
1299
1300static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001301{
1302 FILE *fpid;
1303
1304 fpid = fopen(pidfile, "w");
1305 if (!fpid) {
1306 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001307 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001308 }
1309
1310 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001311 fclose(fpid);
1312 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001313}
1314
1315/*
1316 * If pidfile is specified, background us.
1317 */
1318int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001319{
1320 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001321 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001322
Bruce Cran93bcfd22012-02-20 20:18:19 +01001323#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001324 WSADATA wsd;
1325 WSAStartup(MAKEWORD(2,2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001326#endif
1327
Jens Axboe402668f2011-10-10 15:28:58 +02001328 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001329 return fio_server();
1330
Jens Axboe13755d92011-10-10 19:51:26 +02001331 if (check_existing_pidfile(pidfile)) {
1332 log_err("fio: pidfile %s exists and server appears alive\n",
1333 pidfile);
1334 return -1;
1335 }
1336
Jens Axboee46d8092011-10-03 09:11:02 +02001337 pid = fork();
1338 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001339 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001340 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001341 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001342 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001343 int ret = write_pid(pid, pidfile);
1344
1345 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001346 }
Jens Axboee46d8092011-10-03 09:11:02 +02001347
1348 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001349 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1350 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001351 close(STDIN_FILENO);
1352 close(STDOUT_FILENO);
1353 close(STDERR_FILENO);
1354 f_out = NULL;
1355 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001356
1357 ret = fio_server();
1358
1359 closelog();
1360 unlink(pidfile);
1361 free(pidfile);
1362 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001363}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001364
Jens Axboebebe6392011-10-07 10:00:51 +02001365void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001366{
1367 fio_server_arg = strdup(arg);
1368}