blob: f586235fc1a44b16a6b3ae2f488410e38a0bf124 [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 Axboe50d16972011-09-29 17:45:28 -060012#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
Jens Axboee46d8092011-10-03 09:11:02 +020015#include <syslog.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020016#include <signal.h>
Jens Axboe50d16972011-09-29 17:45:28 -060017
18#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060019#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060020#include "crc/crc16.h"
Jens Axboe802ad4a2011-10-05 09:51:58 +020021#include "ieee754.h"
Jens Axboe50d16972011-09-29 17:45:28 -060022
Jens Axboe132159a2011-09-30 15:01:32 -060023int fio_net_port = 8765;
Jens Axboe50d16972011-09-29 17:45:28 -060024
Jens Axboe009b1be2011-09-29 18:27:02 -060025int exit_backend = 0;
26
Jens Axboe46c48f12011-10-01 12:50:51 -060027static int server_fd = -1;
Jens Axboe37db14f2011-09-30 17:00:42 -060028
Jens Axboe132159a2011-09-30 15:01:32 -060029int fio_send_data(int sk, const void *p, unsigned int len)
30{
Jens Axboe794d69c2011-10-01 08:48:50 -060031 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU);
32
Jens Axboe132159a2011-09-30 15:01:32 -060033 do {
34 int ret = send(sk, p, len, 0);
35
36 if (ret > 0) {
37 len -= ret;
38 if (!len)
39 break;
40 p += ret;
41 continue;
42 } else if (!ret)
43 break;
44 else if (errno == EAGAIN || errno == EINTR)
45 continue;
46 } while (!exit_backend);
47
48 if (!len)
49 return 0;
50
51 return 1;
52}
53
54int fio_recv_data(int sk, void *p, unsigned int len)
55{
56 do {
57 int ret = recv(sk, p, len, MSG_WAITALL);
58
59 if (ret > 0) {
60 len -= ret;
61 if (!len)
62 break;
63 p += ret;
64 continue;
65 } else if (!ret)
66 break;
67 else if (errno == EAGAIN || errno == EINTR)
68 continue;
69 } while (!exit_backend);
70
71 if (!len)
72 return 0;
73
74 return -1;
75}
76
77static int verify_convert_cmd(struct fio_net_cmd *cmd)
78{
Jens Axboefcee5ff2011-09-30 22:50:39 -060079 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -060080
Jens Axboefcee5ff2011-09-30 22:50:39 -060081 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
82 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -060083
Jens Axboefcee5ff2011-09-30 22:50:39 -060084 crc = crc16(cmd, FIO_NET_CMD_CRC_SZ);
85 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -060086 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -060087 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -060088 return 1;
89 }
90
91 cmd->version = le16_to_cpu(cmd->version);
92 cmd->opcode = le16_to_cpu(cmd->opcode);
93 cmd->flags = le32_to_cpu(cmd->flags);
94 cmd->serial = le64_to_cpu(cmd->serial);
95 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
96
97 switch (cmd->version) {
98 case FIO_SERVER_VER1:
99 break;
100 default:
101 log_err("fio: bad server cmd version %d\n", cmd->version);
102 return 1;
103 }
104
105 if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
106 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
107 return 1;
108 }
109
110 return 0;
111}
112
Jens Axboea64e88d2011-10-03 14:20:01 +0200113/*
114 * Read (and defragment, if necessary) incoming commands
115 */
Jens Axboe70e0c312011-10-04 09:18:30 +0200116struct fio_net_cmd *fio_net_recv_cmd(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600117{
Jens Axboea64e88d2011-10-03 14:20:01 +0200118 struct fio_net_cmd cmd, *cmdret = NULL;
119 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600120 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200121 int ret, first = 1;
122 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600123
Jens Axboea64e88d2011-10-03 14:20:01 +0200124 do {
Jens Axboecc0df002011-10-03 20:53:32 +0200125 struct pollfd pfd;
126
127 pfd.fd = sk;
128 pfd.events = POLLIN;
129 ret = 0;
130 do {
Jens Axboe70e0c312011-10-04 09:18:30 +0200131 int timeo = block ? 100 : 10;
132
133 ret = poll(&pfd, 1, timeo);
Jens Axboecc0df002011-10-03 20:53:32 +0200134 if (ret < 0) {
Jens Axboe5fc58b02011-10-03 21:09:14 +0200135 if (errno == EINTR)
136 break;
Jens Axboecc0df002011-10-03 20:53:32 +0200137 log_err("fio: poll: %s\n", strerror(errno));
138 break;
Jens Axboe70e0c312011-10-04 09:18:30 +0200139 } else if (!ret) {
140 if (!block)
141 return NULL;
Jens Axboecc0df002011-10-03 20:53:32 +0200142 continue;
Jens Axboe70e0c312011-10-04 09:18:30 +0200143 }
Jens Axboecc0df002011-10-03 20:53:32 +0200144
145 if (pfd.revents & POLLIN)
146 break;
147 if (pfd.revents & (POLLERR|POLLHUP)) {
148 ret = 1;
149 break;
150 }
Jens Axboe70e0c312011-10-04 09:18:30 +0200151 } while (ret >= 0 && block);
Jens Axboecc0df002011-10-03 20:53:32 +0200152
153 if (ret < 0)
154 break;
155
Jens Axboea64e88d2011-10-03 14:20:01 +0200156 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
157 if (ret)
158 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600159
Jens Axboea64e88d2011-10-03 14:20:01 +0200160 /* We have a command, verify it and swap if need be */
161 ret = verify_convert_cmd(&cmd);
162 if (ret)
163 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600164
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200165 if (first) {
166 /* if this is text, add room for \0 at the end */
167 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
168 assert(!cmdret);
169 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200170 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600171
Jens Axboea64e88d2011-10-03 14:20:01 +0200172 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600173
Jens Axboea64e88d2011-10-03 14:20:01 +0200174 if (first)
175 memcpy(cmdret, &cmd, sizeof(cmd));
176 else
177 assert(cmdret->opcode == cmd.opcode);
178
179 if (!cmd.pdu_len)
180 break;
181
182 /* There's payload, get it */
183 pdu = (void *) cmdret->payload + pdu_offset;
184 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
185 if (ret)
186 break;
187
188 /* Verify payload crc */
189 crc = crc16(pdu, cmd.pdu_len);
190 if (crc != cmd.pdu_crc16) {
191 log_err("fio: server bad crc on payload ");
192 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
193 ret = 1;
194 break;
195 }
196
197 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200198 if (!first)
199 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200200 first = 0;
201 } while (cmd.flags & FIO_NET_CMD_F_MORE);
202
203 if (ret) {
204 free(cmdret);
205 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200206 } else if (cmdret) {
207 /* zero-terminate text input */
208 if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT ||
209 cmdret->opcode == FIO_NET_CMD_JOB)) {
210 char *buf = (char *) cmdret->payload;
211
212 buf[cmdret->pdu_len ] = '\0';
213 }
214 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200215 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200216 }
Jens Axboe132159a2011-09-30 15:01:32 -0600217
Jens Axboea64e88d2011-10-03 14:20:01 +0200218 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600219}
220
221void fio_net_cmd_crc(struct fio_net_cmd *cmd)
222{
223 uint32_t pdu_len;
224
Jens Axboeddcc0b62011-10-03 14:45:27 +0200225 cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600226
227 pdu_len = le32_to_cpu(cmd->pdu_len);
228 if (pdu_len)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200229 cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600230}
231
Jens Axboea64e88d2011-10-03 14:20:01 +0200232int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size)
Jens Axboe794d69c2011-10-01 08:48:50 -0600233{
234 struct fio_net_cmd *cmd;
235 size_t this_len;
236 int ret;
237
238 do {
239 this_len = size;
240 if (this_len > FIO_SERVER_MAX_PDU)
241 this_len = FIO_SERVER_MAX_PDU;
242
243 cmd = malloc(sizeof(*cmd) + this_len);
244
245 fio_init_net_cmd(cmd, opcode, buf, this_len);
246
247 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200248 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600249
250 fio_net_cmd_crc(cmd);
251
252 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
253 free(cmd);
254 size -= this_len;
255 buf += this_len;
256 } while (!ret && size);
257
258 return ret;
259}
260
Jens Axboecc0df002011-10-03 20:53:32 +0200261int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t serial)
Jens Axboe132159a2011-09-30 15:01:32 -0600262{
263 struct fio_net_cmd cmd = {
Jens Axboeddcc0b62011-10-03 14:45:27 +0200264 .version = __cpu_to_le16(FIO_SERVER_VER1),
Jens Axboe132159a2011-09-30 15:01:32 -0600265 .opcode = cpu_to_le16(opcode),
Jens Axboebdab4442011-09-30 22:58:13 -0600266 .serial = cpu_to_le64(serial),
Jens Axboe132159a2011-09-30 15:01:32 -0600267 };
268
269 fio_net_cmd_crc(&cmd);
270
271 return fio_send_data(sk, &cmd, sizeof(cmd));
272}
273
Jens Axboe9abea482011-10-04 13:21:54 +0200274static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600275{
Jens Axboe46c48f12011-10-01 12:50:51 -0600276 dprint(FD_NET, "server: sending quit\n");
Jens Axboecc0df002011-10-03 20:53:32 +0200277 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0);
Jens Axboe437377e2011-10-01 08:31:30 -0600278}
279
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200280static int handle_job_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600281{
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200282 char *buf = (char *) cmd->payload;
Jens Axboea64e88d2011-10-03 14:20:01 +0200283 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600284
Jens Axboee6d1c662011-10-05 20:41:06 +0200285 if (parse_jobs_ini(buf, 1, 0)) {
286 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200287 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200288 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200289
290 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0);
291
292 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200293 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200294 reset_fio_state();
295 return ret;
296}
297
298static int handle_jobline_cmd(struct fio_net_cmd *cmd)
299{
300 struct cmd_line_pdu *pdu = (struct cmd_line_pdu *) cmd->payload;
301 char *argv[FIO_NET_CMD_JOBLINE_ARGV];
302 int ret, i;
303
304 pdu->argc = le16_to_cpu(pdu->argc);
305
Jens Axboe39e8e012011-10-04 13:46:08 +0200306 dprint(FD_NET, "server: %d command line args\n", pdu->argc);
307
308 for (i = 0; i < pdu->argc; i++) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200309 argv[i] = (char *) pdu->argv[i];
Jens Axboe39e8e012011-10-04 13:46:08 +0200310 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
311 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200312
Jens Axboee6d1c662011-10-05 20:41:06 +0200313 if (parse_cmd_line(pdu->argc, argv)) {
314 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200315 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200316 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200317
318 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0);
319
Jens Axboe794d69c2011-10-01 08:48:50 -0600320 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200321 fio_server_send_quit_cmd();
Jens Axboe794d69c2011-10-01 08:48:50 -0600322 reset_fio_state();
Jens Axboe132159a2011-09-30 15:01:32 -0600323 return ret;
324}
325
Jens Axboec28e8e82011-10-04 08:57:39 +0200326static int handle_probe_cmd(struct fio_net_cmd *cmd)
327{
328 struct cmd_probe_pdu probe;
329
330 memset(&probe, 0, sizeof(probe));
331 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200332#ifdef FIO_BIG_ENDIAN
333 probe.bigendian = 1;
334#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200335 probe.fio_major = FIO_MAJOR;
336 probe.fio_minor = FIO_MINOR;
337 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200338
339 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe));
340}
341
Jens Axboe132159a2011-09-30 15:01:32 -0600342static int handle_command(struct fio_net_cmd *cmd)
343{
344 int ret;
345
Jens Axboe46c48f12011-10-01 12:50:51 -0600346 dprint(FD_NET, "server: got opcode %d\n", cmd->opcode);
347
Jens Axboe132159a2011-09-30 15:01:32 -0600348 switch (cmd->opcode) {
349 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200350 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200351 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200352 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600353 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200354 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600355 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200356 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600357 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200358 case FIO_NET_CMD_JOBLINE:
359 ret = handle_jobline_cmd(cmd);
360 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200361 case FIO_NET_CMD_PROBE:
362 ret = handle_probe_cmd(cmd);
363 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600364 default:
365 log_err("fio: unknown opcode: %d\n", cmd->opcode);
366 ret = 1;
367 }
368
369 return ret;
370}
371
Jens Axboe70e0c312011-10-04 09:18:30 +0200372static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600373{
374 struct fio_net_cmd *cmd = NULL;
375 int ret = 0;
376
377 /* read forever */
378 while (!exit_backend) {
Jens Axboe70e0c312011-10-04 09:18:30 +0200379 cmd = fio_net_recv_cmd(sk, block);
Jens Axboe132159a2011-09-30 15:01:32 -0600380 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200381 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600382 break;
383 }
384
Jens Axboe132159a2011-09-30 15:01:32 -0600385 ret = handle_command(cmd);
386 if (ret)
387 break;
388
389 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400390 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600391 }
392
393 if (cmd)
394 free(cmd);
395
396 return ret;
397}
398
Jens Axboecc0df002011-10-03 20:53:32 +0200399void fio_server_idle_loop(void)
400{
401 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200402 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200403}
404
Jens Axboe50d16972011-09-29 17:45:28 -0600405static int accept_loop(int listen_sk)
406{
Jens Axboebb447a22011-10-04 15:06:42 +0200407 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200408 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600409 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600410 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600411
Jens Axboe60efd142011-10-04 13:30:11 +0200412 dprint(FD_NET, "server enter accept loop\n");
413
Jens Axboe009b1be2011-09-29 18:27:02 -0600414 flags = fcntl(listen_sk, F_GETFL);
415 flags |= O_NONBLOCK;
416 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600417again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600418 pfd.fd = listen_sk;
419 pfd.events = POLLIN;
420 do {
421 ret = poll(&pfd, 1, 100);
422 if (ret < 0) {
423 if (errno == EINTR)
424 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600425 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600426 goto out;
427 } else if (!ret)
428 continue;
429
430 if (pfd.revents & POLLIN)
431 break;
432 } while (!exit_backend);
433
434 if (exit_backend)
435 goto out;
436
Jens Axboe6b976bf2011-10-04 15:07:43 +0200437 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600438 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600439 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600440 return -1;
441 }
442
Jens Axboebb447a22011-10-04 15:06:42 +0200443 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600444
Jens Axboe37db14f2011-09-30 17:00:42 -0600445 server_fd = sk;
446
Jens Axboe70e0c312011-10-04 09:18:30 +0200447 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600448
Jens Axboe37db14f2011-09-30 17:00:42 -0600449 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600450 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600451
Jens Axboe009b1be2011-09-29 18:27:02 -0600452 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600453 goto again;
454
Jens Axboe009b1be2011-09-29 18:27:02 -0600455out:
Jens Axboe132159a2011-09-30 15:01:32 -0600456 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600457}
458
Jens Axboe142575e2011-09-30 17:35:45 -0600459int fio_server_text_output(const char *buf, unsigned int len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600460{
Jens Axboe337d75a2011-10-01 12:36:32 -0600461 if (server_fd != -1)
462 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len);
463
Jens Axboe5037b842011-10-04 17:32:16 +0200464 return fwrite(buf, len, 1, f_err);
Jens Axboe142575e2011-09-30 17:35:45 -0600465}
466
Jens Axboea64e88d2011-10-03 14:20:01 +0200467static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
468{
469 dst->max_val = cpu_to_le64(src->max_val);
470 dst->min_val = cpu_to_le64(src->min_val);
471 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200472
473 /*
474 * Encode to IEEE 754 for network transfer
475 */
476 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
477 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200478}
479
480static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
481{
482 int i;
483
484 for (i = 0; i < 2; i++) {
485 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
486 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
487 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
488 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
489 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
490 dst->agg[i] = cpu_to_le64(src->agg[i]);
491 }
492
493 dst->kb_base = cpu_to_le32(src->kb_base);
494 dst->groupid = cpu_to_le32(src->groupid);
495}
496
497/*
498 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
499 * into a single payload.
500 */
501void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
502{
503 struct cmd_ts_pdu p;
504 int i, j;
505
Jens Axboe60efd142011-10-04 13:30:11 +0200506 dprint(FD_NET, "server sending end stats\n");
507
Jens Axboe317b3c82011-10-03 21:47:27 +0200508 memset(&p, 0, sizeof(p));
509
Jens Axboea64e88d2011-10-03 14:20:01 +0200510 strcpy(p.ts.name, ts->name);
511 strcpy(p.ts.verror, ts->verror);
512 strcpy(p.ts.description, ts->description);
513
Jens Axboeddcc0b62011-10-03 14:45:27 +0200514 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200515 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200516 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200517 p.ts.members = cpu_to_le32(ts->members);
518
519 for (i = 0; i < 2; i++) {
520 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
521 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
522 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
523 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
524 }
525
526 p.ts.usr_time = cpu_to_le64(ts->usr_time);
527 p.ts.sys_time = cpu_to_le64(ts->sys_time);
528 p.ts.ctx = cpu_to_le64(ts->ctx);
529 p.ts.minf = cpu_to_le64(ts->minf);
530 p.ts.majf = cpu_to_le64(ts->majf);
531 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200532
533 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
534 fio_fp64_t *fp = &p.ts.percentile_list[i];
535
536 fp->u.i = __cpu_to_le64(fio_double_to_uint64(fp->u.f));
537 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200538
539 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
540 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
541 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
542 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
543 }
544
545 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
546 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
547 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
548 }
549
550 for (i = 0; i < 2; i++)
551 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
552 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
553
554 for (i = 0; i < 3; i++) {
555 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200556 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200557 }
558
Jens Axboe93eee042011-10-03 21:08:48 +0200559 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200560 p.ts.total_complete = cpu_to_le64(ts->total_complete);
561
562 for (i = 0; i < 2; i++) {
563 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
564 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
565 }
566
567 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
568 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
569 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200570 p.ts.first_error = cpu_to_le32(ts->first_error);
571 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200572
573 convert_gs(&p.rs, rs);
574
575 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p));
576}
577
578void fio_server_send_gs(struct group_run_stats *rs)
579{
580 struct group_run_stats gs;
581
Jens Axboe60efd142011-10-04 13:30:11 +0200582 dprint(FD_NET, "server sending group run stats\n");
583
Jens Axboea64e88d2011-10-03 14:20:01 +0200584 convert_gs(&gs, rs);
585 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs));
586}
587
Jens Axboecf451d12011-10-03 16:48:30 +0200588void fio_server_send_status(void)
589{
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200590 struct jobs_eta *je;
591 size_t size;
592 void *buf;
Jens Axboecf451d12011-10-03 16:48:30 +0200593 int i;
594
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200595 size = sizeof(*je) + thread_number * sizeof(char);
596 buf = malloc(size);
597 memset(buf, 0, size);
598 je = buf;
599
600 if (!calc_thread_status(je)) {
601 free(je);
Jens Axboecf451d12011-10-03 16:48:30 +0200602 return;
Jens Axboecf451d12011-10-03 16:48:30 +0200603 }
604
Jens Axboe60efd142011-10-04 13:30:11 +0200605 dprint(FD_NET, "server sending status\n");
606
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200607 je->nr_running = cpu_to_le32(je->nr_running);
608 je->nr_ramp = cpu_to_le32(je->nr_ramp);
609 je->nr_pending = cpu_to_le32(je->nr_pending);
610 je->files_open = cpu_to_le32(je->files_open);
611 je->m_rate = cpu_to_le32(je->m_rate);
612 je->t_rate = cpu_to_le32(je->t_rate);
613 je->m_iops = cpu_to_le32(je->m_iops);
614 je->t_iops = cpu_to_le32(je->t_iops);
Jens Axboecf451d12011-10-03 16:48:30 +0200615
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200616 for (i = 0; i < 2; i++) {
617 je->rate[i] = cpu_to_le32(je->rate[i]);
618 je->iops[i] = cpu_to_le32(je->iops[i]);
619 }
620
621 je->elapsed_sec = cpu_to_le32(je->nr_running);
622 je->eta_sec = cpu_to_le64(je->eta_sec);
623
624 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, buf, size);
625 free(je);
Jens Axboecf451d12011-10-03 16:48:30 +0200626}
627
Jens Axboe142575e2011-09-30 17:35:45 -0600628int fio_server_log(const char *format, ...)
629{
630 char buffer[1024];
631 va_list args;
Jens Axboe82fa6b22011-09-30 22:29:08 -0600632 size_t len;
Jens Axboe142575e2011-09-30 17:35:45 -0600633
Jens Axboe60efd142011-10-04 13:30:11 +0200634 dprint(FD_NET, "server log\n");
635
Jens Axboe142575e2011-09-30 17:35:45 -0600636 va_start(args, format);
Jens Axboe82fa6b22011-09-30 22:29:08 -0600637 len = vsnprintf(buffer, sizeof(buffer), format, args);
Jens Axboe142575e2011-09-30 17:35:45 -0600638 va_end(args);
639
Jens Axboe82fa6b22011-09-30 22:29:08 -0600640 return fio_server_text_output(buffer, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600641}
Jens Axboee46d8092011-10-03 09:11:02 +0200642
Jens Axboe81179ee2011-10-04 12:42:06 +0200643static int fio_server(void)
644{
645 struct sockaddr_in saddr_in;
646 struct sockaddr addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200647 fio_socklen_t len;
Jens Axboe81179ee2011-10-04 12:42:06 +0200648 int sk, opt, ret;
649
650 dprint(FD_NET, "starting server\n");
651
652 sk = socket(AF_INET, SOCK_STREAM, 0);
653 if (sk < 0) {
654 log_err("fio: socket: %s\n", strerror(errno));
655 return -1;
656 }
657
658 opt = 1;
659 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
660 log_err("fio: setsockopt: %s\n", strerror(errno));
661 return -1;
662 }
663#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200664 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200665 log_err("fio: setsockopt: %s\n", strerror(errno));
666 return -1;
667 }
668#endif
669
670 saddr_in.sin_family = AF_INET;
671 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
672 saddr_in.sin_port = htons(fio_net_port);
673
674 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
675 log_err("fio: bind: %s\n", strerror(errno));
676 return -1;
677 }
678
679 if (listen(sk, 1) < 0) {
680 log_err("fio: listen: %s\n", strerror(errno));
681 return -1;
682 }
683
684 len = sizeof(addr);
685 if (getsockname(sk, &addr, &len) < 0) {
686 log_err("fio: getsockname: %s\n", strerror(errno));
687 return -1;
688 }
689
690 ret = accept_loop(sk);
691 close(sk);
692 return ret;
693}
694
Jens Axboe9abea482011-10-04 13:21:54 +0200695static void sig_int(int sig)
696{
697 fio_terminate_threads(TERMINATE_ALL);
698 exit_backend = 1;
699}
700
701static void server_signal_handler(void)
702{
703 struct sigaction act;
704
705 memset(&act, 0, sizeof(act));
706 act.sa_handler = sig_int;
707 act.sa_flags = SA_RESTART;
708 sigaction(SIGINT, &act, NULL);
709
710 memset(&act, 0, sizeof(act));
711 act.sa_handler = sig_int;
712 act.sa_flags = SA_RESTART;
713 sigaction(SIGTERM, &act, NULL);
714}
715
Jens Axboee46d8092011-10-03 09:11:02 +0200716int fio_start_server(int daemonize)
717{
718 pid_t pid;
719
Jens Axboe9abea482011-10-04 13:21:54 +0200720 server_signal_handler();
721
Jens Axboee46d8092011-10-03 09:11:02 +0200722 if (!daemonize)
723 return fio_server();
724
725 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
726 pid = fork();
727 if (pid < 0) {
728 syslog(LOG_ERR, "failed server fork");
Jens Axboec28e8e82011-10-04 08:57:39 +0200729 return -1;
Jens Axboee46d8092011-10-03 09:11:02 +0200730 } else if (pid)
731 exit(0);
732
733 setsid();
734 close(STDIN_FILENO);
735 close(STDOUT_FILENO);
736 close(STDERR_FILENO);
737 f_out = NULL;
738 f_err = NULL;
739 log_syslog = 1;
740 return fio_server();
741}