blob: e7107d448072afd4d1a2678a74a76c2ad1721c56 [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 Axboec7c6cb42011-10-13 14:12:40 +020023#include "lib/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 Axboe811826b2011-10-24 09:11:50 +020035static struct sockaddr_in6 saddr_in6;
Jens Axboe01be0382011-10-15 14:43:41 +020036static int first_cmd_check;
Jens Axboe811826b2011-10-24 09:11:50 +020037static int use_ipv6;
Jens Axboe37db14f2011-09-30 17:00:42 -060038
Jens Axboe89c17072011-10-11 10:15:51 +020039static const char *fio_server_ops[FIO_NET_CMD_NR] = {
40 "",
41 "QUIT",
42 "EXIT",
43 "JOB",
44 "JOBLINE",
45 "TEXT",
46 "TS",
47 "GS",
48 "SEND_ETA",
49 "ETA",
50 "PROBE",
51 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020052 "STOP",
53 "DISK_UTIL",
Jens Axboe01be0382011-10-15 14:43:41 +020054 "RUN",
Jens Axboe89c17072011-10-11 10:15:51 +020055};
56
57const char *fio_server_op(unsigned int op)
58{
59 static char buf[32];
60
61 if (op < FIO_NET_CMD_NR)
62 return fio_server_ops[op];
63
64 sprintf(buf, "UNKNOWN/%d", op);
65 return buf;
66}
67
Jens Axboe132159a2011-09-30 15:01:32 -060068int fio_send_data(int sk, const void *p, unsigned int len)
69{
Jens Axboe794d69c2011-10-01 08:48:50 -060070 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU);
71
Jens Axboe132159a2011-09-30 15:01:32 -060072 do {
73 int ret = send(sk, p, len, 0);
74
75 if (ret > 0) {
76 len -= ret;
77 if (!len)
78 break;
79 p += ret;
80 continue;
81 } else if (!ret)
82 break;
83 else if (errno == EAGAIN || errno == EINTR)
84 continue;
Jens Axboe7b821682011-10-11 12:16:32 +020085 else
86 break;
Jens Axboe132159a2011-09-30 15:01:32 -060087 } while (!exit_backend);
88
89 if (!len)
90 return 0;
91
92 return 1;
93}
94
95int fio_recv_data(int sk, void *p, unsigned int len)
96{
97 do {
98 int ret = recv(sk, p, len, MSG_WAITALL);
99
100 if (ret > 0) {
101 len -= ret;
102 if (!len)
103 break;
104 p += ret;
105 continue;
106 } else if (!ret)
107 break;
108 else if (errno == EAGAIN || errno == EINTR)
109 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200110 else
111 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600112 } while (!exit_backend);
113
114 if (!len)
115 return 0;
116
117 return -1;
118}
119
120static int verify_convert_cmd(struct fio_net_cmd *cmd)
121{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600122 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600123
Jens Axboefcee5ff2011-09-30 22:50:39 -0600124 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
125 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600126
Jens Axboefcee5ff2011-09-30 22:50:39 -0600127 crc = crc16(cmd, FIO_NET_CMD_CRC_SZ);
128 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600129 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600130 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600131 return 1;
132 }
133
134 cmd->version = le16_to_cpu(cmd->version);
135 cmd->opcode = le16_to_cpu(cmd->opcode);
136 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200137 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600138 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
139
140 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200141 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600142 break;
143 default:
144 log_err("fio: bad server cmd version %d\n", cmd->version);
145 return 1;
146 }
147
148 if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
149 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
150 return 1;
151 }
152
153 return 0;
154}
155
Jens Axboea64e88d2011-10-03 14:20:01 +0200156/*
157 * Read (and defragment, if necessary) incoming commands
158 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200159struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600160{
Jens Axboea64e88d2011-10-03 14:20:01 +0200161 struct fio_net_cmd cmd, *cmdret = NULL;
162 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600163 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200164 int ret, first = 1;
165 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600166
Jens Axboea64e88d2011-10-03 14:20:01 +0200167 do {
168 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
169 if (ret)
170 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600171
Jens Axboea64e88d2011-10-03 14:20:01 +0200172 /* We have a command, verify it and swap if need be */
173 ret = verify_convert_cmd(&cmd);
174 if (ret)
175 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600176
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200177 if (first) {
178 /* if this is text, add room for \0 at the end */
179 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
180 assert(!cmdret);
181 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200182 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600183
Jens Axboea64e88d2011-10-03 14:20:01 +0200184 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600185
Jens Axboea64e88d2011-10-03 14:20:01 +0200186 if (first)
187 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200188 else if (cmdret->opcode != cmd.opcode) {
189 log_err("fio: fragment opcode mismatch (%d != %d)\n",
190 cmdret->opcode, cmd.opcode);
191 ret = 1;
192 break;
193 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200194
195 if (!cmd.pdu_len)
196 break;
197
198 /* There's payload, get it */
199 pdu = (void *) cmdret->payload + pdu_offset;
200 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
201 if (ret)
202 break;
203
204 /* Verify payload crc */
205 crc = crc16(pdu, cmd.pdu_len);
206 if (crc != cmd.pdu_crc16) {
207 log_err("fio: server bad crc on payload ");
208 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
209 ret = 1;
210 break;
211 }
212
213 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200214 if (!first)
215 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200216 first = 0;
217 } while (cmd.flags & FIO_NET_CMD_F_MORE);
218
219 if (ret) {
220 free(cmdret);
221 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200222 } else if (cmdret) {
223 /* zero-terminate text input */
224 if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT ||
225 cmdret->opcode == FIO_NET_CMD_JOB)) {
226 char *buf = (char *) cmdret->payload;
227
228 buf[cmdret->pdu_len ] = '\0';
229 }
230 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200231 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200232 }
Jens Axboe132159a2011-09-30 15:01:32 -0600233
Jens Axboea64e88d2011-10-03 14:20:01 +0200234 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600235}
236
237void fio_net_cmd_crc(struct fio_net_cmd *cmd)
238{
239 uint32_t pdu_len;
240
Jens Axboeddcc0b62011-10-03 14:45:27 +0200241 cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600242
243 pdu_len = le32_to_cpu(cmd->pdu_len);
244 if (pdu_len)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200245 cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600246}
247
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200248int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
249 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600250{
Jens Axboe7f868312011-10-10 09:55:21 +0200251 struct fio_net_cmd *cmd = NULL;
252 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600253 int ret;
254
255 do {
256 this_len = size;
257 if (this_len > FIO_SERVER_MAX_PDU)
258 this_len = FIO_SERVER_MAX_PDU;
259
Jens Axboe7f868312011-10-10 09:55:21 +0200260 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
261 if (cmd)
262 free(cmd);
263
264 cur_len = sizeof(*cmd) + this_len;
265 cmd = malloc(cur_len);
266 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600267
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200268 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600269
270 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200271 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600272
273 fio_net_cmd_crc(cmd);
274
275 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600276 size -= this_len;
277 buf += this_len;
278 } while (!ret && size);
279
Jens Axboe7f868312011-10-10 09:55:21 +0200280 if (cmd)
281 free(cmd);
282
Jens Axboe794d69c2011-10-01 08:48:50 -0600283 return ret;
284}
285
Jens Axboe89c17072011-10-11 10:15:51 +0200286static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600287{
Jens Axboe178cde92011-10-05 22:14:31 +0200288 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600289
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200290 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600291 fio_net_cmd_crc(&cmd);
292
293 return fio_send_data(sk, &cmd, sizeof(cmd));
294}
295
Jens Axboe89c17072011-10-11 10:15:51 +0200296/*
297 * If 'list' is non-NULL, then allocate and store the sent command for
298 * later verification.
299 */
300int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
301 struct flist_head *list)
302{
303 struct fio_net_int_cmd *cmd;
304 int ret;
305
306 if (!list)
307 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
308
309 cmd = malloc(sizeof(*cmd));
310
Jens Axboedf380932011-10-11 14:25:08 +0200311 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
Jens Axboe89c17072011-10-11 10:15:51 +0200312 fio_net_cmd_crc(&cmd->cmd);
313
314 INIT_FLIST_HEAD(&cmd->list);
315 gettimeofday(&cmd->tv, NULL);
316 cmd->saved_tag = tag;
317
318 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
319 if (ret) {
320 free(cmd);
321 return ret;
322 }
323
324 flist_add_tail(&cmd->list, list);
325 return 0;
326}
327
Jens Axboe9abea482011-10-04 13:21:54 +0200328static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600329{
Jens Axboe46c48f12011-10-01 12:50:51 -0600330 dprint(FD_NET, "server: sending quit\n");
Jens Axboe89c17072011-10-11 10:15:51 +0200331 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600332}
333
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200334static int handle_job_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600335{
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200336 char *buf = (char *) cmd->payload;
Jens Axboe11e950b2011-10-16 21:34:14 +0200337 struct cmd_start_pdu spdu;
338 struct cmd_end_pdu epdu;
Jens Axboea64e88d2011-10-03 14:20:01 +0200339 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600340
Jens Axboee6d1c662011-10-05 20:41:06 +0200341 if (parse_jobs_ini(buf, 1, 0)) {
342 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200343 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200344 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200345
Jens Axboe11e950b2011-10-16 21:34:14 +0200346 spdu.jobs = cpu_to_le32(thread_number);
347 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200348
349 ret = exec_run();
Jens Axboe11e950b2011-10-16 21:34:14 +0200350
351 epdu.error = ret;
352 fio_net_send_cmd(server_fd, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0);
353
Jens Axboe9abea482011-10-04 13:21:54 +0200354 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200355 reset_fio_state();
356 return ret;
357}
358
359static int handle_jobline_cmd(struct fio_net_cmd *cmd)
360{
Jens Axboefa2ea802011-10-08 21:07:29 +0200361 void *pdu = cmd->payload;
362 struct cmd_single_line_pdu *cslp;
363 struct cmd_line_pdu *clp;
364 unsigned long offset;
365 char **argv;
Jens Axboe81179ee2011-10-04 12:42:06 +0200366 int ret, i;
367
Jens Axboefa2ea802011-10-08 21:07:29 +0200368 clp = pdu;
369 clp->lines = le16_to_cpu(clp->lines);
370 argv = malloc(clp->lines * sizeof(char *));
371 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200372
Jens Axboefa2ea802011-10-08 21:07:29 +0200373 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200374
Jens Axboefa2ea802011-10-08 21:07:29 +0200375 for (i = 0; i < clp->lines; i++) {
376 cslp = pdu + offset;
377 argv[i] = (char *) cslp->text;
378
379 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200380 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
381 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200382
Jens Axboefa2ea802011-10-08 21:07:29 +0200383 if (parse_cmd_line(clp->lines, argv)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200384 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200385 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200386 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200387 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200388
Jens Axboefa2ea802011-10-08 21:07:29 +0200389 free(argv);
390
Jens Axboe89c17072011-10-11 10:15:51 +0200391 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200392
Jens Axboe794d69c2011-10-01 08:48:50 -0600393 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200394 fio_server_send_quit_cmd();
Jens Axboe794d69c2011-10-01 08:48:50 -0600395 reset_fio_state();
Jens Axboe132159a2011-09-30 15:01:32 -0600396 return ret;
397}
398
Jens Axboec28e8e82011-10-04 08:57:39 +0200399static int handle_probe_cmd(struct fio_net_cmd *cmd)
400{
401 struct cmd_probe_pdu probe;
402
Jens Axboe89c17072011-10-11 10:15:51 +0200403 dprint(FD_NET, "server: sending probe reply\n");
404
Jens Axboec28e8e82011-10-04 08:57:39 +0200405 memset(&probe, 0, sizeof(probe));
406 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200407#ifdef FIO_BIG_ENDIAN
408 probe.bigendian = 1;
409#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200410 probe.fio_major = FIO_MAJOR;
411 probe.fio_minor = FIO_MINOR;
412 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200413
Jens Axboecca84642011-10-07 12:47:57 +0200414 probe.os = FIO_OS;
415 probe.arch = FIO_ARCH;
416
Jens Axboe38fdef22011-10-11 14:30:06 +0200417 probe.bpp = sizeof(void *);
418
Jens Axboe89c17072011-10-11 10:15:51 +0200419 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200420}
421
422static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
423{
424 struct jobs_eta *je;
425 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200426 int i;
427
Jens Axboeb814fb22011-10-12 09:20:34 +0200428 if (!thread_number)
429 return 0;
430
431 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200432 je = malloc(size);
433 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200434
435 if (!calc_thread_status(je, 1)) {
436 free(je);
437 return 0;
438 }
439
440 dprint(FD_NET, "server sending status\n");
441
442 je->nr_running = cpu_to_le32(je->nr_running);
443 je->nr_ramp = cpu_to_le32(je->nr_ramp);
444 je->nr_pending = cpu_to_le32(je->nr_pending);
445 je->files_open = cpu_to_le32(je->files_open);
446 je->m_rate = cpu_to_le32(je->m_rate);
447 je->t_rate = cpu_to_le32(je->t_rate);
448 je->m_iops = cpu_to_le32(je->m_iops);
449 je->t_iops = cpu_to_le32(je->t_iops);
450
451 for (i = 0; i < 2; i++) {
452 je->rate[i] = cpu_to_le32(je->rate[i]);
453 je->iops[i] = cpu_to_le32(je->iops[i]);
454 }
455
456 je->elapsed_sec = cpu_to_le32(je->nr_running);
457 je->eta_sec = cpu_to_le64(je->eta_sec);
458
Jens Axboe7f868312011-10-10 09:55:21 +0200459 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200460 free(je);
461 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200462}
463
Jens Axboe132159a2011-09-30 15:01:32 -0600464static int handle_command(struct fio_net_cmd *cmd)
465{
466 int ret;
467
Jens Axboe89c17072011-10-11 10:15:51 +0200468 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
469 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600470
Jens Axboe132159a2011-09-30 15:01:32 -0600471 switch (cmd->opcode) {
472 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200473 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200474 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200475 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600476 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200477 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600478 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200479 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600480 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200481 case FIO_NET_CMD_JOBLINE:
482 ret = handle_jobline_cmd(cmd);
483 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200484 case FIO_NET_CMD_PROBE:
485 ret = handle_probe_cmd(cmd);
486 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200487 case FIO_NET_CMD_SEND_ETA:
488 ret = handle_send_eta_cmd(cmd);
489 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600490 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200491 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600492 ret = 1;
493 }
494
495 return ret;
496}
497
Jens Axboe70e0c312011-10-04 09:18:30 +0200498static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600499{
500 struct fio_net_cmd *cmd = NULL;
501 int ret = 0;
502
503 /* read forever */
504 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200505 struct pollfd pfd = {
506 .fd = sk,
507 .events = POLLIN,
508 };
509
510 ret = 0;
511 do {
512 ret = poll(&pfd, 1, 100);
513 if (ret < 0) {
514 if (errno == EINTR)
515 break;
516 log_err("fio: poll: %s\n", strerror(errno));
517 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200518 } else if (!ret) {
519 if (!block)
520 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200521 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200522 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200523
524 if (pfd.revents & POLLIN)
525 break;
526 if (pfd.revents & (POLLERR|POLLHUP)) {
527 ret = 1;
528 break;
529 }
Jens Axboe19c65172011-10-05 22:05:37 +0200530 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200531
532 if (ret < 0)
533 break;
534
535 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600536 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200537 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600538 break;
539 }
540
Jens Axboe132159a2011-09-30 15:01:32 -0600541 ret = handle_command(cmd);
542 if (ret)
543 break;
544
545 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400546 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600547 }
548
549 if (cmd)
550 free(cmd);
551
552 return ret;
553}
554
Jens Axboecc0df002011-10-03 20:53:32 +0200555void fio_server_idle_loop(void)
556{
Jens Axboe01be0382011-10-15 14:43:41 +0200557 if (!first_cmd_check)
558 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_RUN, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200559 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200560 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200561}
562
Jens Axboe50d16972011-09-29 17:45:28 -0600563static int accept_loop(int listen_sk)
564{
Jens Axboebb447a22011-10-04 15:06:42 +0200565 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200566 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600567 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600568 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600569
Jens Axboe60efd142011-10-04 13:30:11 +0200570 dprint(FD_NET, "server enter accept loop\n");
571
Jens Axboe009b1be2011-09-29 18:27:02 -0600572 flags = fcntl(listen_sk, F_GETFL);
573 flags |= O_NONBLOCK;
574 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600575again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600576 pfd.fd = listen_sk;
577 pfd.events = POLLIN;
578 do {
579 ret = poll(&pfd, 1, 100);
580 if (ret < 0) {
581 if (errno == EINTR)
582 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600583 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600584 goto out;
585 } else if (!ret)
586 continue;
587
588 if (pfd.revents & POLLIN)
589 break;
590 } while (!exit_backend);
591
592 if (exit_backend)
593 goto out;
594
Jens Axboe6b976bf2011-10-04 15:07:43 +0200595 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600596 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600597 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600598 return -1;
599 }
600
Jens Axboebb447a22011-10-04 15:06:42 +0200601 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600602
Jens Axboe37db14f2011-09-30 17:00:42 -0600603 server_fd = sk;
604
Jens Axboe70e0c312011-10-04 09:18:30 +0200605 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600606
Jens Axboe37db14f2011-09-30 17:00:42 -0600607 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600608 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600609
Jens Axboe009b1be2011-09-29 18:27:02 -0600610 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600611 goto again;
612
Jens Axboe009b1be2011-09-29 18:27:02 -0600613out:
Jens Axboe132159a2011-09-30 15:01:32 -0600614 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600615}
616
Jens Axboe13755d92011-10-10 19:51:26 +0200617int fio_server_text_output(const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600618{
Jens Axboe337d75a2011-10-01 12:36:32 -0600619 if (server_fd != -1)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200620 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0);
Jens Axboe337d75a2011-10-01 12:36:32 -0600621
Jens Axboe13755d92011-10-10 19:51:26 +0200622 return log_local_buf(buf, len);
Jens Axboe142575e2011-09-30 17:35:45 -0600623}
624
Jens Axboea64e88d2011-10-03 14:20:01 +0200625static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
626{
627 dst->max_val = cpu_to_le64(src->max_val);
628 dst->min_val = cpu_to_le64(src->min_val);
629 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200630
631 /*
632 * Encode to IEEE 754 for network transfer
633 */
634 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
635 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200636}
637
638static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
639{
640 int i;
641
642 for (i = 0; i < 2; i++) {
643 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
644 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
645 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
646 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
647 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
648 dst->agg[i] = cpu_to_le64(src->agg[i]);
649 }
650
651 dst->kb_base = cpu_to_le32(src->kb_base);
652 dst->groupid = cpu_to_le32(src->groupid);
653}
654
655/*
656 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
657 * into a single payload.
658 */
659void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
660{
661 struct cmd_ts_pdu p;
662 int i, j;
663
Jens Axboe60efd142011-10-04 13:30:11 +0200664 dprint(FD_NET, "server sending end stats\n");
665
Jens Axboe317b3c82011-10-03 21:47:27 +0200666 memset(&p, 0, sizeof(p));
667
Jens Axboea64e88d2011-10-03 14:20:01 +0200668 strcpy(p.ts.name, ts->name);
669 strcpy(p.ts.verror, ts->verror);
670 strcpy(p.ts.description, ts->description);
671
Jens Axboeddcc0b62011-10-03 14:45:27 +0200672 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200673 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200674 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200675 p.ts.members = cpu_to_le32(ts->members);
676
677 for (i = 0; i < 2; i++) {
678 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
679 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
680 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
681 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
682 }
683
684 p.ts.usr_time = cpu_to_le64(ts->usr_time);
685 p.ts.sys_time = cpu_to_le64(ts->sys_time);
686 p.ts.ctx = cpu_to_le64(ts->ctx);
687 p.ts.minf = cpu_to_le64(ts->minf);
688 p.ts.majf = cpu_to_le64(ts->majf);
689 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200690
691 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200692 fio_fp64_t *src = &ts->percentile_list[i];
693 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200694
Jens Axboecfc03e42011-10-12 21:20:42 +0200695 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200696 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200697
698 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
699 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
700 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
701 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
702 }
703
704 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
705 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
706 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
707 }
708
709 for (i = 0; i < 2; i++)
710 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
711 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
712
713 for (i = 0; i < 3; i++) {
714 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200715 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200716 }
717
Jens Axboe93eee042011-10-03 21:08:48 +0200718 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200719 p.ts.total_complete = cpu_to_le64(ts->total_complete);
720
721 for (i = 0; i < 2; i++) {
722 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
723 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
724 }
725
726 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
727 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
728 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200729 p.ts.first_error = cpu_to_le32(ts->first_error);
730 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200731
732 convert_gs(&p.rs, rs);
733
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200734 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200735}
736
737void fio_server_send_gs(struct group_run_stats *rs)
738{
739 struct group_run_stats gs;
740
Jens Axboe60efd142011-10-04 13:30:11 +0200741 dprint(FD_NET, "server sending group run stats\n");
742
Jens Axboea64e88d2011-10-03 14:20:01 +0200743 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200744 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200745}
746
Jens Axboed09a64a2011-10-13 11:38:56 +0200747static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
748{
749 int i;
750
751 for (i = 0; i < 2; i++) {
752 dst->ios[i] = cpu_to_le32(src->ios[i]);
753 dst->merges[i] = cpu_to_le32(src->merges[i]);
754 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
755 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
756 }
757
758 dst->io_ticks = cpu_to_le32(src->io_ticks);
759 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
760 dst->slavecount = cpu_to_le32(src->slavecount);
761 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
762}
763
764static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
765{
766 int i;
767
768 strcpy((char *) dst->name, (char *) src->name);
769
770 for (i = 0; i < 2; i++) {
771 dst->ios[i] = cpu_to_le32(src->ios[i]);
772 dst->merges[i] = cpu_to_le32(src->merges[i]);
773 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
774 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
775 }
776
777 dst->io_ticks = cpu_to_le32(src->io_ticks);
778 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
779 dst->msec = cpu_to_le64(src->msec);
780}
781
782void fio_server_send_du(void)
783{
784 struct disk_util *du;
785 struct flist_head *entry;
786 struct cmd_du_pdu pdu;
787
788 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
789
Jens Axboe0766d922011-10-13 12:02:08 +0200790 memset(&pdu, 0, sizeof(pdu));
791
Jens Axboed09a64a2011-10-13 11:38:56 +0200792 flist_for_each(entry, &disk_list) {
793 du = flist_entry(entry, struct disk_util, list);
794
795 convert_dus(&pdu.dus, &du->dus);
796 convert_agg(&pdu.agg, &du->agg);
797
798 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
799 }
800}
801
Jens Axboe142575e2011-09-30 17:35:45 -0600802int fio_server_log(const char *format, ...)
803{
804 char buffer[1024];
805 va_list args;
Jens Axboe82fa6b22011-09-30 22:29:08 -0600806 size_t len;
Jens Axboe142575e2011-09-30 17:35:45 -0600807
Jens Axboe60efd142011-10-04 13:30:11 +0200808 dprint(FD_NET, "server log\n");
809
Jens Axboe142575e2011-09-30 17:35:45 -0600810 va_start(args, format);
Jens Axboe82fa6b22011-09-30 22:29:08 -0600811 len = vsnprintf(buffer, sizeof(buffer), format, args);
Jens Axboe142575e2011-09-30 17:35:45 -0600812 va_end(args);
813
Jens Axboe82fa6b22011-09-30 22:29:08 -0600814 return fio_server_text_output(buffer, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600815}
Jens Axboee46d8092011-10-03 09:11:02 +0200816
Jens Axboe87aa8f12011-10-06 21:24:13 +0200817static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200818{
Jens Axboe811826b2011-10-24 09:11:50 +0200819 struct sockaddr *addr;
820 fio_socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200821 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200822
Jens Axboe811826b2011-10-24 09:11:50 +0200823 if (use_ipv6)
824 sk = socket(AF_INET6, SOCK_STREAM, 0);
825 else
826 sk = socket(AF_INET, SOCK_STREAM, 0);
827
Jens Axboe81179ee2011-10-04 12:42:06 +0200828 if (sk < 0) {
829 log_err("fio: socket: %s\n", strerror(errno));
830 return -1;
831 }
832
833 opt = 1;
834 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
835 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200836 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200837 return -1;
838 }
839#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200840 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200841 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200842 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200843 return -1;
844 }
845#endif
846
Jens Axboe811826b2011-10-24 09:11:50 +0200847 if (use_ipv6) {
848 addr = (struct sockaddr *) &saddr_in6;
849 socklen = sizeof(saddr_in6);
850 saddr_in6.sin6_family = AF_INET6;
851 } else {
852 addr = (struct sockaddr *) &saddr_in;
853 socklen = sizeof(saddr_in);
854 saddr_in.sin_family = AF_INET;
855 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200856
Jens Axboe811826b2011-10-24 09:11:50 +0200857 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200858 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200859 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200860 return -1;
861 }
862
Jens Axboe87aa8f12011-10-06 21:24:13 +0200863 return sk;
864}
865
866static int fio_init_server_sock(void)
867{
868 struct sockaddr_un addr;
869 fio_socklen_t len;
870 mode_t mode;
871 int sk;
872
873 sk = socket(AF_UNIX, SOCK_STREAM, 0);
874 if (sk < 0) {
875 log_err("fio: socket: %s\n", strerror(errno));
876 return -1;
877 }
878
879 mode = umask(000);
880
881 memset(&addr, 0, sizeof(addr));
882 addr.sun_family = AF_UNIX;
883 strcpy(addr.sun_path, bind_sock);
884 unlink(bind_sock);
885
886 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
887
888 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
889 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200890 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200891 return -1;
892 }
893
894 umask(mode);
895 return sk;
896}
897
898static int fio_init_server_connection(void)
899{
Jens Axboebebe6392011-10-07 10:00:51 +0200900 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +0200901 int sk;
902
903 dprint(FD_NET, "starting server\n");
904
905 if (!bind_sock)
906 sk = fio_init_server_ip();
907 else
908 sk = fio_init_server_sock();
909
910 if (sk < 0)
911 return sk;
912
Jens Axboe811826b2011-10-24 09:11:50 +0200913 if (!bind_sock) {
914 char *p, port[16];
915 const void *src;
916 int af;
917
918 if (use_ipv6) {
919 af = AF_INET6;
920 src = &saddr_in6.sin6_addr;
921 } else {
922 af = AF_INET;
923 src = &saddr_in.sin_addr;
924 }
925
926 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
927
928 sprintf(port, ",%u", fio_net_port);
929 if (p)
930 strcat(p, port);
931 else
932 strcpy(bind_str, port);
933 } else
Jens Axboebebe6392011-10-07 10:00:51 +0200934 strcpy(bind_str, bind_sock);
935
936 log_info("fio: server listening on %s\n", bind_str);
937
Jens Axboe89c17072011-10-11 10:15:51 +0200938 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200939 log_err("fio: listen: %s\n", strerror(errno));
940 return -1;
941 }
942
Jens Axboe87aa8f12011-10-06 21:24:13 +0200943 return sk;
944}
945
Jens Axboe660a2bf2011-10-24 09:35:06 +0200946/*
947 * Parse a host/ip/port string. Reads from 'str'.
948 *
949 * Outputs:
950 *
951 * For IPv4:
952 * *ptr is the host, *port is the port, inp is the destination.
953 * For IPv6:
954 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
955 * For local domain sockets:
956 * *ptr is the filename, *is_sock is 1.
957 */
Jens Axboebebe6392011-10-07 10:00:51 +0200958int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +0200959 int *port, struct in_addr *inp,
960 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +0200961{
Jens Axboe76867622011-10-25 09:52:51 +0200962 const char *host = str;
963 char *portp;
964 int ret, lport = 0;
965
Jens Axboebebe6392011-10-07 10:00:51 +0200966 *ptr = NULL;
967 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +0200968 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +0200969 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +0200970
971 if (!strncmp(str, "sock:", 5)) {
972 *ptr = strdup(str + 5);
973 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +0200974
Jens Axboe76867622011-10-25 09:52:51 +0200975 return 0;
976 }
977
978 /*
979 * Is it ip:<ip or host>:port
980 */
981 if (!strncmp(host, "ip:", 3))
982 host += 3;
983 else if (!strncmp(host, "ip4:", 4))
984 host += 4;
985 else if (!strncmp(host, "ip6:", 4)) {
986 host += 4;
987 *ipv6 = 1;
988 } else if (host[0] == ':') {
989 /* String is :port */
990 host++;
991 lport = atoi(host);
992 if (!lport || lport > 65535) {
993 log_err("fio: bad server port %u\n", port);
994 return 1;
995 }
996 /* no hostname given, we are done */
997 *port = lport;
998 return 0;
999 }
1000
1001 /*
1002 * If no port seen yet, check if there's a last ':' at the end
1003 */
1004 if (!lport) {
1005 portp = strchr(host, ',');
1006 if (portp) {
1007 *portp = '\0';
1008 portp++;
1009 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001010 if (!lport || lport > 65535) {
1011 log_err("fio: bad server port %u\n", port);
1012 return 1;
1013 }
Jens Axboe76867622011-10-25 09:52:51 +02001014 }
1015 }
1016
1017 if (lport)
1018 *port = lport;
1019
1020 if (!strlen(host))
1021 return 0;
1022
1023 *ptr = strdup(host);
1024
1025 if (*ipv6)
1026 ret = inet_pton(AF_INET6, host, inp6);
1027 else
1028 ret = inet_pton(AF_INET, host, inp);
1029
1030 if (ret != 1) {
1031 struct hostent *hent;
1032
1033 hent = gethostbyname(host);
1034 if (!hent) {
1035 log_err("fio: failed to resolve <%s>\n", host);
1036 free(*ptr);
1037 *ptr = NULL;
1038 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001039 }
1040
Jens Axboe76867622011-10-25 09:52:51 +02001041 if (*ipv6) {
1042 if (hent->h_addrtype != AF_INET6) {
1043 log_info("fio: falling back to IPv4\n");
1044 *ipv6 = 0;
1045 } else
1046 memcpy(inp6, hent->h_addr_list[0], 16);
Jens Axboebebe6392011-10-07 10:00:51 +02001047 }
Jens Axboe76867622011-10-25 09:52:51 +02001048 if (!*ipv6) {
1049 if (hent->h_addrtype != AF_INET) {
1050 log_err("fio: lookup type mismatch\n");
Jens Axboebebe6392011-10-07 10:00:51 +02001051 free(*ptr);
1052 *ptr = NULL;
1053 return 1;
1054 }
Jens Axboe76867622011-10-25 09:52:51 +02001055 memcpy(inp, hent->h_addr_list[0], 4);
Jens Axboebebe6392011-10-07 10:00:51 +02001056 }
1057 }
1058
1059 if (*port == 0)
1060 *port = fio_net_port;
1061
1062 return 0;
1063}
1064
Jens Axboe87aa8f12011-10-06 21:24:13 +02001065/*
1066 * Server arg should be one of:
1067 *
1068 * sock:/path/to/socket
1069 * ip:1.2.3.4
1070 * 1.2.3.4
1071 *
1072 * Where sock uses unix domain sockets, and ip binds the server to
1073 * a specific interface. If no arguments are given to the server, it
1074 * uses IP and binds to 0.0.0.0.
1075 *
1076 */
1077static int fio_handle_server_arg(void)
1078{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001079 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001080 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001081
Jens Axboe87aa8f12011-10-06 21:24:13 +02001082 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1083
1084 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001085 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001086
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001087 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001088 &port, &saddr_in.sin_addr,
1089 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001090
1091 if (!is_sock && bind_sock) {
1092 free(bind_sock);
1093 bind_sock = NULL;
1094 }
1095
Jens Axboea7de0a12011-10-07 12:55:14 +02001096out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001097 fio_net_port = port;
1098 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001099 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001100 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001101}
1102
1103static int fio_server(void)
1104{
1105 int sk, ret;
1106
1107 dprint(FD_NET, "starting server\n");
1108
1109 if (fio_handle_server_arg())
1110 return -1;
1111
1112 sk = fio_init_server_connection();
1113 if (sk < 0)
1114 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001115
1116 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001117
Jens Axboe81179ee2011-10-04 12:42:06 +02001118 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001119
1120 if (fio_server_arg) {
1121 free(fio_server_arg);
1122 fio_server_arg = NULL;
1123 }
Jens Axboebebe6392011-10-07 10:00:51 +02001124 if (bind_sock)
1125 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001126
Jens Axboe81179ee2011-10-04 12:42:06 +02001127 return ret;
1128}
1129
Jens Axboe7b821682011-10-11 12:16:32 +02001130void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001131{
Jens Axboe7b821682011-10-11 12:16:32 +02001132 if (signal == SIGPIPE)
1133 server_fd = -1;
1134 else {
1135 log_info("\nfio: terminating on signal %d\n", signal);
1136 exit_backend = 1;
1137 }
Jens Axboe9abea482011-10-04 13:21:54 +02001138}
1139
Jens Axboe13755d92011-10-10 19:51:26 +02001140static int check_existing_pidfile(const char *pidfile)
1141{
1142 struct stat sb;
1143 char buf[16];
1144 pid_t pid;
1145 FILE *f;
1146
1147 if (stat(pidfile, &sb))
1148 return 0;
1149
1150 f = fopen(pidfile, "r");
1151 if (!f)
1152 return 0;
1153
Jens Axboebfc3b172011-10-10 21:16:55 +02001154 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001155 fclose(f);
1156 return 1;
1157 }
1158 fclose(f);
1159
1160 pid = atoi(buf);
1161 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001162 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001163
1164 return 1;
1165}
1166
1167static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001168{
1169 FILE *fpid;
1170
1171 fpid = fopen(pidfile, "w");
1172 if (!fpid) {
1173 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001174 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001175 }
1176
1177 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001178 fclose(fpid);
1179 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001180}
1181
1182/*
1183 * If pidfile is specified, background us.
1184 */
1185int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001186{
1187 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001188 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001189
Jens Axboe402668f2011-10-10 15:28:58 +02001190 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001191 return fio_server();
1192
Jens Axboe13755d92011-10-10 19:51:26 +02001193 if (check_existing_pidfile(pidfile)) {
1194 log_err("fio: pidfile %s exists and server appears alive\n",
1195 pidfile);
1196 return -1;
1197 }
1198
Jens Axboee46d8092011-10-03 09:11:02 +02001199 pid = fork();
1200 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001201 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001202 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001203 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001204 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001205 int ret = write_pid(pid, pidfile);
1206
1207 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001208 }
Jens Axboee46d8092011-10-03 09:11:02 +02001209
1210 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001211 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1212 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001213 close(STDIN_FILENO);
1214 close(STDOUT_FILENO);
1215 close(STDERR_FILENO);
1216 f_out = NULL;
1217 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001218
1219 ret = fio_server();
1220
1221 closelog();
1222 unlink(pidfile);
1223 free(pidfile);
1224 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001225}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001226
Jens Axboebebe6392011-10-07 10:00:51 +02001227void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001228{
1229 fio_server_arg = strdup(arg);
1230}