blob: f7be89a7ef8262e273daea43fc70d9f4e9cb6246 [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 Axboe50d16972011-09-29 17:45:28 -060019
20#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060021#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060022#include "crc/crc16.h"
Jens Axboe802ad4a2011-10-05 09:51:58 +020023#include "ieee754.h"
Jens Axboe50d16972011-09-29 17:45:28 -060024
Jens Axboe89cf1482011-10-07 10:10:18 +020025#include "fio_version.h"
26
Jens Axboe132159a2011-09-30 15:01:32 -060027int fio_net_port = 8765;
Jens Axboe50d16972011-09-29 17:45:28 -060028
Jens Axboe009b1be2011-09-29 18:27:02 -060029int exit_backend = 0;
30
Jens Axboe46c48f12011-10-01 12:50:51 -060031static int server_fd = -1;
Jens Axboe87aa8f12011-10-06 21:24:13 +020032static char *fio_server_arg;
33static char *bind_sock;
34static struct sockaddr_in saddr_in;
Jens Axboe37db14f2011-09-30 17:00:42 -060035
Jens Axboe132159a2011-09-30 15:01:32 -060036int fio_send_data(int sk, const void *p, unsigned int len)
37{
Jens Axboe794d69c2011-10-01 08:48:50 -060038 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU);
39
Jens Axboe132159a2011-09-30 15:01:32 -060040 do {
41 int ret = send(sk, p, len, 0);
42
43 if (ret > 0) {
44 len -= ret;
45 if (!len)
46 break;
47 p += ret;
48 continue;
49 } else if (!ret)
50 break;
51 else if (errno == EAGAIN || errno == EINTR)
52 continue;
53 } while (!exit_backend);
54
55 if (!len)
56 return 0;
57
58 return 1;
59}
60
61int fio_recv_data(int sk, void *p, unsigned int len)
62{
63 do {
64 int ret = recv(sk, p, len, MSG_WAITALL);
65
66 if (ret > 0) {
67 len -= ret;
68 if (!len)
69 break;
70 p += ret;
71 continue;
72 } else if (!ret)
73 break;
74 else if (errno == EAGAIN || errno == EINTR)
75 continue;
76 } while (!exit_backend);
77
78 if (!len)
79 return 0;
80
81 return -1;
82}
83
84static int verify_convert_cmd(struct fio_net_cmd *cmd)
85{
Jens Axboefcee5ff2011-09-30 22:50:39 -060086 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -060087
Jens Axboefcee5ff2011-09-30 22:50:39 -060088 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
89 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -060090
Jens Axboefcee5ff2011-09-30 22:50:39 -060091 crc = crc16(cmd, FIO_NET_CMD_CRC_SZ);
92 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -060093 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -060094 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -060095 return 1;
96 }
97
98 cmd->version = le16_to_cpu(cmd->version);
99 cmd->opcode = le16_to_cpu(cmd->opcode);
100 cmd->flags = le32_to_cpu(cmd->flags);
101 cmd->serial = le64_to_cpu(cmd->serial);
102 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
103
104 switch (cmd->version) {
Jens Axboe5cf6be72011-10-07 10:12:47 +0200105 case FIO_SERVER_VER3:
Jens Axboe132159a2011-09-30 15:01:32 -0600106 break;
107 default:
108 log_err("fio: bad server cmd version %d\n", cmd->version);
109 return 1;
110 }
111
112 if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
113 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
114 return 1;
115 }
116
117 return 0;
118}
119
Jens Axboea64e88d2011-10-03 14:20:01 +0200120/*
121 * Read (and defragment, if necessary) incoming commands
122 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200123struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600124{
Jens Axboea64e88d2011-10-03 14:20:01 +0200125 struct fio_net_cmd cmd, *cmdret = NULL;
126 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600127 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200128 int ret, first = 1;
129 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600130
Jens Axboea64e88d2011-10-03 14:20:01 +0200131 do {
132 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
133 if (ret)
134 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600135
Jens Axboea64e88d2011-10-03 14:20:01 +0200136 /* We have a command, verify it and swap if need be */
137 ret = verify_convert_cmd(&cmd);
138 if (ret)
139 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600140
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200141 if (first) {
142 /* if this is text, add room for \0 at the end */
143 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
144 assert(!cmdret);
145 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200146 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600147
Jens Axboea64e88d2011-10-03 14:20:01 +0200148 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600149
Jens Axboea64e88d2011-10-03 14:20:01 +0200150 if (first)
151 memcpy(cmdret, &cmd, sizeof(cmd));
152 else
153 assert(cmdret->opcode == cmd.opcode);
154
155 if (!cmd.pdu_len)
156 break;
157
158 /* There's payload, get it */
159 pdu = (void *) cmdret->payload + pdu_offset;
160 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
161 if (ret)
162 break;
163
164 /* Verify payload crc */
165 crc = crc16(pdu, cmd.pdu_len);
166 if (crc != cmd.pdu_crc16) {
167 log_err("fio: server bad crc on payload ");
168 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
169 ret = 1;
170 break;
171 }
172
173 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200174 if (!first)
175 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200176 first = 0;
177 } while (cmd.flags & FIO_NET_CMD_F_MORE);
178
179 if (ret) {
180 free(cmdret);
181 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200182 } else if (cmdret) {
183 /* zero-terminate text input */
184 if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT ||
185 cmdret->opcode == FIO_NET_CMD_JOB)) {
186 char *buf = (char *) cmdret->payload;
187
188 buf[cmdret->pdu_len ] = '\0';
189 }
190 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200191 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200192 }
Jens Axboe132159a2011-09-30 15:01:32 -0600193
Jens Axboea64e88d2011-10-03 14:20:01 +0200194 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600195}
196
197void fio_net_cmd_crc(struct fio_net_cmd *cmd)
198{
199 uint32_t pdu_len;
200
Jens Axboeddcc0b62011-10-03 14:45:27 +0200201 cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600202
203 pdu_len = le32_to_cpu(cmd->pdu_len);
204 if (pdu_len)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200205 cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600206}
207
Jens Axboea64e88d2011-10-03 14:20:01 +0200208int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size)
Jens Axboe794d69c2011-10-01 08:48:50 -0600209{
210 struct fio_net_cmd *cmd;
211 size_t this_len;
212 int ret;
213
214 do {
215 this_len = size;
216 if (this_len > FIO_SERVER_MAX_PDU)
217 this_len = FIO_SERVER_MAX_PDU;
218
219 cmd = malloc(sizeof(*cmd) + this_len);
220
221 fio_init_net_cmd(cmd, opcode, buf, this_len);
222
223 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200224 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600225
226 fio_net_cmd_crc(cmd);
227
228 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
229 free(cmd);
230 size -= this_len;
231 buf += this_len;
232 } while (!ret && size);
233
234 return ret;
235}
236
Jens Axboecc0df002011-10-03 20:53:32 +0200237int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t serial)
Jens Axboe132159a2011-09-30 15:01:32 -0600238{
Jens Axboe178cde92011-10-05 22:14:31 +0200239 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600240
Jens Axboe178cde92011-10-05 22:14:31 +0200241 fio_init_net_cmd(&cmd, opcode, NULL, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600242 fio_net_cmd_crc(&cmd);
243
244 return fio_send_data(sk, &cmd, sizeof(cmd));
245}
246
Jens Axboe9abea482011-10-04 13:21:54 +0200247static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600248{
Jens Axboe46c48f12011-10-01 12:50:51 -0600249 dprint(FD_NET, "server: sending quit\n");
Jens Axboecc0df002011-10-03 20:53:32 +0200250 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0);
Jens Axboe437377e2011-10-01 08:31:30 -0600251}
252
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200253static int handle_job_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600254{
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200255 char *buf = (char *) cmd->payload;
Jens Axboea64e88d2011-10-03 14:20:01 +0200256 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600257
Jens Axboee6d1c662011-10-05 20:41:06 +0200258 if (parse_jobs_ini(buf, 1, 0)) {
259 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200260 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200261 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200262
263 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0);
264
265 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200266 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200267 reset_fio_state();
268 return ret;
269}
270
271static int handle_jobline_cmd(struct fio_net_cmd *cmd)
272{
273 struct cmd_line_pdu *pdu = (struct cmd_line_pdu *) cmd->payload;
274 char *argv[FIO_NET_CMD_JOBLINE_ARGV];
275 int ret, i;
276
277 pdu->argc = le16_to_cpu(pdu->argc);
278
Jens Axboe39e8e012011-10-04 13:46:08 +0200279 dprint(FD_NET, "server: %d command line args\n", pdu->argc);
280
281 for (i = 0; i < pdu->argc; i++) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200282 argv[i] = (char *) pdu->argv[i];
Jens Axboe39e8e012011-10-04 13:46:08 +0200283 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
284 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200285
Jens Axboee6d1c662011-10-05 20:41:06 +0200286 if (parse_cmd_line(pdu->argc, argv)) {
287 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200288 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200289 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200290
291 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0);
292
Jens Axboe794d69c2011-10-01 08:48:50 -0600293 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200294 fio_server_send_quit_cmd();
Jens Axboe794d69c2011-10-01 08:48:50 -0600295 reset_fio_state();
Jens Axboe132159a2011-09-30 15:01:32 -0600296 return ret;
297}
298
Jens Axboec28e8e82011-10-04 08:57:39 +0200299static int handle_probe_cmd(struct fio_net_cmd *cmd)
300{
301 struct cmd_probe_pdu probe;
302
303 memset(&probe, 0, sizeof(probe));
304 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200305#ifdef FIO_BIG_ENDIAN
306 probe.bigendian = 1;
307#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200308 probe.fio_major = FIO_MAJOR;
309 probe.fio_minor = FIO_MINOR;
310 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200311
Jens Axboecca84642011-10-07 12:47:57 +0200312 probe.os = FIO_OS;
313 probe.arch = FIO_ARCH;
314
Jens Axboec28e8e82011-10-04 08:57:39 +0200315 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe));
316}
317
Jens Axboe132159a2011-09-30 15:01:32 -0600318static int handle_command(struct fio_net_cmd *cmd)
319{
320 int ret;
321
Jens Axboe46c48f12011-10-01 12:50:51 -0600322 dprint(FD_NET, "server: got opcode %d\n", cmd->opcode);
323
Jens Axboe132159a2011-09-30 15:01:32 -0600324 switch (cmd->opcode) {
325 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200326 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200327 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200328 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600329 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200330 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600331 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200332 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600333 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200334 case FIO_NET_CMD_JOBLINE:
335 ret = handle_jobline_cmd(cmd);
336 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200337 case FIO_NET_CMD_PROBE:
338 ret = handle_probe_cmd(cmd);
339 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600340 default:
341 log_err("fio: unknown opcode: %d\n", cmd->opcode);
342 ret = 1;
343 }
344
345 return ret;
346}
347
Jens Axboe70e0c312011-10-04 09:18:30 +0200348static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600349{
350 struct fio_net_cmd *cmd = NULL;
351 int ret = 0;
352
353 /* read forever */
354 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200355 struct pollfd pfd = {
356 .fd = sk,
357 .events = POLLIN,
358 };
359
360 ret = 0;
361 do {
362 ret = poll(&pfd, 1, 100);
363 if (ret < 0) {
364 if (errno == EINTR)
365 break;
366 log_err("fio: poll: %s\n", strerror(errno));
367 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200368 } else if (!ret) {
369 if (!block)
370 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200371 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200372 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200373
374 if (pfd.revents & POLLIN)
375 break;
376 if (pfd.revents & (POLLERR|POLLHUP)) {
377 ret = 1;
378 break;
379 }
Jens Axboe19c65172011-10-05 22:05:37 +0200380 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200381
382 if (ret < 0)
383 break;
384
385 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600386 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200387 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600388 break;
389 }
390
Jens Axboe132159a2011-09-30 15:01:32 -0600391 ret = handle_command(cmd);
392 if (ret)
393 break;
394
395 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400396 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600397 }
398
399 if (cmd)
400 free(cmd);
401
402 return ret;
403}
404
Jens Axboecc0df002011-10-03 20:53:32 +0200405void fio_server_idle_loop(void)
406{
407 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200408 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200409}
410
Jens Axboe50d16972011-09-29 17:45:28 -0600411static int accept_loop(int listen_sk)
412{
Jens Axboebb447a22011-10-04 15:06:42 +0200413 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200414 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600415 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600416 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600417
Jens Axboe60efd142011-10-04 13:30:11 +0200418 dprint(FD_NET, "server enter accept loop\n");
419
Jens Axboe009b1be2011-09-29 18:27:02 -0600420 flags = fcntl(listen_sk, F_GETFL);
421 flags |= O_NONBLOCK;
422 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600423again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600424 pfd.fd = listen_sk;
425 pfd.events = POLLIN;
426 do {
427 ret = poll(&pfd, 1, 100);
428 if (ret < 0) {
429 if (errno == EINTR)
430 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600431 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600432 goto out;
433 } else if (!ret)
434 continue;
435
436 if (pfd.revents & POLLIN)
437 break;
438 } while (!exit_backend);
439
440 if (exit_backend)
441 goto out;
442
Jens Axboe6b976bf2011-10-04 15:07:43 +0200443 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600444 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600445 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600446 return -1;
447 }
448
Jens Axboebb447a22011-10-04 15:06:42 +0200449 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600450
Jens Axboe37db14f2011-09-30 17:00:42 -0600451 server_fd = sk;
452
Jens Axboe70e0c312011-10-04 09:18:30 +0200453 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600454
Jens Axboe37db14f2011-09-30 17:00:42 -0600455 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600456 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600457
Jens Axboe009b1be2011-09-29 18:27:02 -0600458 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600459 goto again;
460
Jens Axboe009b1be2011-09-29 18:27:02 -0600461out:
Jens Axboe132159a2011-09-30 15:01:32 -0600462 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600463}
464
Jens Axboe142575e2011-09-30 17:35:45 -0600465int fio_server_text_output(const char *buf, unsigned int len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600466{
Jens Axboe337d75a2011-10-01 12:36:32 -0600467 if (server_fd != -1)
468 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len);
469
Jens Axboe5037b842011-10-04 17:32:16 +0200470 return fwrite(buf, len, 1, f_err);
Jens Axboe142575e2011-09-30 17:35:45 -0600471}
472
Jens Axboea64e88d2011-10-03 14:20:01 +0200473static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
474{
475 dst->max_val = cpu_to_le64(src->max_val);
476 dst->min_val = cpu_to_le64(src->min_val);
477 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200478
479 /*
480 * Encode to IEEE 754 for network transfer
481 */
482 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
483 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200484}
485
486static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
487{
488 int i;
489
490 for (i = 0; i < 2; i++) {
491 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
492 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
493 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
494 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
495 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
496 dst->agg[i] = cpu_to_le64(src->agg[i]);
497 }
498
499 dst->kb_base = cpu_to_le32(src->kb_base);
500 dst->groupid = cpu_to_le32(src->groupid);
501}
502
503/*
504 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
505 * into a single payload.
506 */
507void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
508{
509 struct cmd_ts_pdu p;
510 int i, j;
511
Jens Axboe60efd142011-10-04 13:30:11 +0200512 dprint(FD_NET, "server sending end stats\n");
513
Jens Axboe317b3c82011-10-03 21:47:27 +0200514 memset(&p, 0, sizeof(p));
515
Jens Axboea64e88d2011-10-03 14:20:01 +0200516 strcpy(p.ts.name, ts->name);
517 strcpy(p.ts.verror, ts->verror);
518 strcpy(p.ts.description, ts->description);
519
Jens Axboeddcc0b62011-10-03 14:45:27 +0200520 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200521 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200522 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200523 p.ts.members = cpu_to_le32(ts->members);
524
525 for (i = 0; i < 2; i++) {
526 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
527 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
528 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
529 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
530 }
531
532 p.ts.usr_time = cpu_to_le64(ts->usr_time);
533 p.ts.sys_time = cpu_to_le64(ts->sys_time);
534 p.ts.ctx = cpu_to_le64(ts->ctx);
535 p.ts.minf = cpu_to_le64(ts->minf);
536 p.ts.majf = cpu_to_le64(ts->majf);
537 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200538
539 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
540 fio_fp64_t *fp = &p.ts.percentile_list[i];
541
542 fp->u.i = __cpu_to_le64(fio_double_to_uint64(fp->u.f));
543 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200544
545 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
546 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
547 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
548 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
549 }
550
551 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
552 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
553 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
554 }
555
556 for (i = 0; i < 2; i++)
557 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
558 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
559
560 for (i = 0; i < 3; i++) {
561 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200562 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200563 }
564
Jens Axboe93eee042011-10-03 21:08:48 +0200565 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200566 p.ts.total_complete = cpu_to_le64(ts->total_complete);
567
568 for (i = 0; i < 2; i++) {
569 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
570 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
571 }
572
573 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
574 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
575 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200576 p.ts.first_error = cpu_to_le32(ts->first_error);
577 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200578
579 convert_gs(&p.rs, rs);
580
581 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p));
582}
583
584void fio_server_send_gs(struct group_run_stats *rs)
585{
586 struct group_run_stats gs;
587
Jens Axboe60efd142011-10-04 13:30:11 +0200588 dprint(FD_NET, "server sending group run stats\n");
589
Jens Axboea64e88d2011-10-03 14:20:01 +0200590 convert_gs(&gs, rs);
591 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs));
592}
593
Jens Axboecf451d12011-10-03 16:48:30 +0200594void fio_server_send_status(void)
595{
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200596 struct jobs_eta *je;
597 size_t size;
598 void *buf;
Jens Axboecf451d12011-10-03 16:48:30 +0200599 int i;
600
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200601 size = sizeof(*je) + thread_number * sizeof(char);
602 buf = malloc(size);
603 memset(buf, 0, size);
604 je = buf;
605
606 if (!calc_thread_status(je)) {
607 free(je);
Jens Axboecf451d12011-10-03 16:48:30 +0200608 return;
Jens Axboecf451d12011-10-03 16:48:30 +0200609 }
610
Jens Axboe60efd142011-10-04 13:30:11 +0200611 dprint(FD_NET, "server sending status\n");
612
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200613 je->nr_running = cpu_to_le32(je->nr_running);
614 je->nr_ramp = cpu_to_le32(je->nr_ramp);
615 je->nr_pending = cpu_to_le32(je->nr_pending);
616 je->files_open = cpu_to_le32(je->files_open);
617 je->m_rate = cpu_to_le32(je->m_rate);
618 je->t_rate = cpu_to_le32(je->t_rate);
619 je->m_iops = cpu_to_le32(je->m_iops);
620 je->t_iops = cpu_to_le32(je->t_iops);
Jens Axboecf451d12011-10-03 16:48:30 +0200621
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200622 for (i = 0; i < 2; i++) {
623 je->rate[i] = cpu_to_le32(je->rate[i]);
624 je->iops[i] = cpu_to_le32(je->iops[i]);
625 }
626
627 je->elapsed_sec = cpu_to_le32(je->nr_running);
628 je->eta_sec = cpu_to_le64(je->eta_sec);
629
630 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, buf, size);
631 free(je);
Jens Axboecf451d12011-10-03 16:48:30 +0200632}
633
Jens Axboe142575e2011-09-30 17:35:45 -0600634int fio_server_log(const char *format, ...)
635{
636 char buffer[1024];
637 va_list args;
Jens Axboe82fa6b22011-09-30 22:29:08 -0600638 size_t len;
Jens Axboe142575e2011-09-30 17:35:45 -0600639
Jens Axboe60efd142011-10-04 13:30:11 +0200640 dprint(FD_NET, "server log\n");
641
Jens Axboe142575e2011-09-30 17:35:45 -0600642 va_start(args, format);
Jens Axboe82fa6b22011-09-30 22:29:08 -0600643 len = vsnprintf(buffer, sizeof(buffer), format, args);
Jens Axboe142575e2011-09-30 17:35:45 -0600644 va_end(args);
645
Jens Axboe82fa6b22011-09-30 22:29:08 -0600646 return fio_server_text_output(buffer, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600647}
Jens Axboee46d8092011-10-03 09:11:02 +0200648
Jens Axboe87aa8f12011-10-06 21:24:13 +0200649static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200650{
Jens Axboe87aa8f12011-10-06 21:24:13 +0200651 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200652
653 sk = socket(AF_INET, SOCK_STREAM, 0);
654 if (sk < 0) {
655 log_err("fio: socket: %s\n", strerror(errno));
656 return -1;
657 }
658
659 opt = 1;
660 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
661 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200662 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200663 return -1;
664 }
665#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200666 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200667 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200668 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200669 return -1;
670 }
671#endif
672
673 saddr_in.sin_family = AF_INET;
Jens Axboe81179ee2011-10-04 12:42:06 +0200674
675 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
676 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200677 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200678 return -1;
679 }
680
Jens Axboe87aa8f12011-10-06 21:24:13 +0200681 return sk;
682}
683
684static int fio_init_server_sock(void)
685{
686 struct sockaddr_un addr;
687 fio_socklen_t len;
688 mode_t mode;
689 int sk;
690
691 sk = socket(AF_UNIX, SOCK_STREAM, 0);
692 if (sk < 0) {
693 log_err("fio: socket: %s\n", strerror(errno));
694 return -1;
695 }
696
697 mode = umask(000);
698
699 memset(&addr, 0, sizeof(addr));
700 addr.sun_family = AF_UNIX;
701 strcpy(addr.sun_path, bind_sock);
702 unlink(bind_sock);
703
704 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
705
706 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
707 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200708 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200709 return -1;
710 }
711
712 umask(mode);
713 return sk;
714}
715
716static int fio_init_server_connection(void)
717{
Jens Axboebebe6392011-10-07 10:00:51 +0200718 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +0200719 int sk;
720
721 dprint(FD_NET, "starting server\n");
722
723 if (!bind_sock)
724 sk = fio_init_server_ip();
725 else
726 sk = fio_init_server_sock();
727
728 if (sk < 0)
729 return sk;
730
Jens Axboebebe6392011-10-07 10:00:51 +0200731 if (!bind_sock)
732 sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port);
733 else
734 strcpy(bind_str, bind_sock);
735
736 log_info("fio: server listening on %s\n", bind_str);
737
Jens Axboe81179ee2011-10-04 12:42:06 +0200738 if (listen(sk, 1) < 0) {
739 log_err("fio: listen: %s\n", strerror(errno));
740 return -1;
741 }
742
Jens Axboe87aa8f12011-10-06 21:24:13 +0200743 return sk;
744}
745
Jens Axboebebe6392011-10-07 10:00:51 +0200746int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
747 int *port, struct in_addr *inp)
748{
749 *ptr = NULL;
750 *is_sock = 0;
751 *port = 0;
752
753 if (!strncmp(str, "sock:", 5)) {
754 *ptr = strdup(str + 5);
755 *is_sock = 1;
756 } else {
757 const char *host = str;
758 char *portp;
759 int lport = 0;
760
761 /*
762 * Is it ip:<ip or host>:port
763 */
764 if (!strncmp(host, "ip:", 3))
765 host += 3;
766 else if (host[0] == ':') {
767 /* String is :port */
768 host++;
769 lport = atoi(host);
770 if (!lport || lport > 65535) {
771 log_err("fio: bad server port %u\n", port);
772 return 1;
773 }
774 /* no hostname given, we are done */
775 *port = lport;
776 return 0;
777 }
778
779 /*
780 * If no port seen yet, check if there's a last ':' at the end
781 */
782 if (!lport) {
783 portp = strchr(host, ':');
784 if (portp) {
785 *portp = '\0';
786 portp++;
787 lport = atoi(portp);
788 if (!lport || lport > 65535) {
789 log_err("fio: bad server port %u\n", port);
790 return 1;
791 }
792 }
793 }
794
795 if (lport)
796 *port = lport;
797
798 *ptr = strdup(host);
799
800 if (inet_aton(host, inp) != 1) {
801 struct hostent *hent;
802
803 hent = gethostbyname(host);
804 if (!hent) {
Jens Axboebebe6392011-10-07 10:00:51 +0200805 free(*ptr);
806 *ptr = NULL;
807 return 1;
808 }
809
810 memcpy(inp, hent->h_addr, 4);
811 }
812 }
813
814 if (*port == 0)
815 *port = fio_net_port;
816
817 return 0;
818}
819
Jens Axboe87aa8f12011-10-06 21:24:13 +0200820/*
821 * Server arg should be one of:
822 *
823 * sock:/path/to/socket
824 * ip:1.2.3.4
825 * 1.2.3.4
826 *
827 * Where sock uses unix domain sockets, and ip binds the server to
828 * a specific interface. If no arguments are given to the server, it
829 * uses IP and binds to 0.0.0.0.
830 *
831 */
832static int fio_handle_server_arg(void)
833{
Jens Axboea7de0a12011-10-07 12:55:14 +0200834 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +0200835
Jens Axboe87aa8f12011-10-06 21:24:13 +0200836 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
837
838 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +0200839 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200840
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200841 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboebebe6392011-10-07 10:00:51 +0200842 &fio_net_port, &saddr_in.sin_addr);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200843
844 if (!is_sock && bind_sock) {
845 free(bind_sock);
846 bind_sock = NULL;
847 }
848
Jens Axboea7de0a12011-10-07 12:55:14 +0200849out:
850 saddr_in.sin_port = htons(fio_net_port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200851 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200852}
853
854static int fio_server(void)
855{
856 int sk, ret;
857
858 dprint(FD_NET, "starting server\n");
859
860 if (fio_handle_server_arg())
861 return -1;
862
863 sk = fio_init_server_connection();
864 if (sk < 0)
865 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200866
867 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200868
Jens Axboe81179ee2011-10-04 12:42:06 +0200869 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200870
871 if (fio_server_arg) {
872 free(fio_server_arg);
873 fio_server_arg = NULL;
874 }
Jens Axboebebe6392011-10-07 10:00:51 +0200875 if (bind_sock)
876 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200877
Jens Axboe81179ee2011-10-04 12:42:06 +0200878 return ret;
879}
880
Jens Axboe9abea482011-10-04 13:21:54 +0200881static void sig_int(int sig)
882{
883 fio_terminate_threads(TERMINATE_ALL);
884 exit_backend = 1;
885}
886
887static void server_signal_handler(void)
888{
889 struct sigaction act;
890
891 memset(&act, 0, sizeof(act));
892 act.sa_handler = sig_int;
893 act.sa_flags = SA_RESTART;
894 sigaction(SIGINT, &act, NULL);
895
896 memset(&act, 0, sizeof(act));
897 act.sa_handler = sig_int;
898 act.sa_flags = SA_RESTART;
899 sigaction(SIGTERM, &act, NULL);
900}
901
Jens Axboee46d8092011-10-03 09:11:02 +0200902int fio_start_server(int daemonize)
903{
904 pid_t pid;
905
Jens Axboe9abea482011-10-04 13:21:54 +0200906 server_signal_handler();
907
Jens Axboee46d8092011-10-03 09:11:02 +0200908 if (!daemonize)
909 return fio_server();
910
911 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
912 pid = fork();
913 if (pid < 0) {
914 syslog(LOG_ERR, "failed server fork");
Jens Axboec28e8e82011-10-04 08:57:39 +0200915 return -1;
Jens Axboee46d8092011-10-03 09:11:02 +0200916 } else if (pid)
917 exit(0);
918
919 setsid();
920 close(STDIN_FILENO);
921 close(STDOUT_FILENO);
922 close(STDERR_FILENO);
923 f_out = NULL;
924 f_err = NULL;
925 log_syslog = 1;
926 return fio_server();
927}
Jens Axboe87aa8f12011-10-06 21:24:13 +0200928
Jens Axboebebe6392011-10-07 10:00:51 +0200929void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200930{
931 fio_server_arg = strdup(arg);
932}