blob: e7a905722f30f4d3b3177aafdbd63c735e7a1376 [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 Axboe37db14f2011-09-30 17:00:42 -060035
Jens Axboe89c17072011-10-11 10:15:51 +020036static const char *fio_server_ops[FIO_NET_CMD_NR] = {
37 "",
38 "QUIT",
39 "EXIT",
40 "JOB",
41 "JOBLINE",
42 "TEXT",
43 "TS",
44 "GS",
45 "SEND_ETA",
46 "ETA",
47 "PROBE",
48 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020049 "STOP",
50 "DISK_UTIL",
Jens Axboe89c17072011-10-11 10:15:51 +020051};
52
53const char *fio_server_op(unsigned int op)
54{
55 static char buf[32];
56
57 if (op < FIO_NET_CMD_NR)
58 return fio_server_ops[op];
59
60 sprintf(buf, "UNKNOWN/%d", op);
61 return buf;
62}
63
Jens Axboe132159a2011-09-30 15:01:32 -060064int fio_send_data(int sk, const void *p, unsigned int len)
65{
Jens Axboe794d69c2011-10-01 08:48:50 -060066 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU);
67
Jens Axboe132159a2011-09-30 15:01:32 -060068 do {
69 int ret = send(sk, p, len, 0);
70
71 if (ret > 0) {
72 len -= ret;
73 if (!len)
74 break;
75 p += ret;
76 continue;
77 } else if (!ret)
78 break;
79 else if (errno == EAGAIN || errno == EINTR)
80 continue;
Jens Axboe7b821682011-10-11 12:16:32 +020081 else
82 break;
Jens Axboe132159a2011-09-30 15:01:32 -060083 } while (!exit_backend);
84
85 if (!len)
86 return 0;
87
88 return 1;
89}
90
91int fio_recv_data(int sk, void *p, unsigned int len)
92{
93 do {
94 int ret = recv(sk, p, len, MSG_WAITALL);
95
96 if (ret > 0) {
97 len -= ret;
98 if (!len)
99 break;
100 p += ret;
101 continue;
102 } else if (!ret)
103 break;
104 else if (errno == EAGAIN || errno == EINTR)
105 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200106 else
107 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600108 } while (!exit_backend);
109
110 if (!len)
111 return 0;
112
113 return -1;
114}
115
116static int verify_convert_cmd(struct fio_net_cmd *cmd)
117{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600118 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600119
Jens Axboefcee5ff2011-09-30 22:50:39 -0600120 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
121 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600122
Jens Axboefcee5ff2011-09-30 22:50:39 -0600123 crc = crc16(cmd, FIO_NET_CMD_CRC_SZ);
124 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600125 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600126 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600127 return 1;
128 }
129
130 cmd->version = le16_to_cpu(cmd->version);
131 cmd->opcode = le16_to_cpu(cmd->opcode);
132 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200133 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600134 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
135
136 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200137 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600138 break;
139 default:
140 log_err("fio: bad server cmd version %d\n", cmd->version);
141 return 1;
142 }
143
144 if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
145 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
146 return 1;
147 }
148
149 return 0;
150}
151
Jens Axboea64e88d2011-10-03 14:20:01 +0200152/*
153 * Read (and defragment, if necessary) incoming commands
154 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200155struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600156{
Jens Axboea64e88d2011-10-03 14:20:01 +0200157 struct fio_net_cmd cmd, *cmdret = NULL;
158 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600159 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200160 int ret, first = 1;
161 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600162
Jens Axboea64e88d2011-10-03 14:20:01 +0200163 do {
164 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
165 if (ret)
166 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600167
Jens Axboea64e88d2011-10-03 14:20:01 +0200168 /* We have a command, verify it and swap if need be */
169 ret = verify_convert_cmd(&cmd);
170 if (ret)
171 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600172
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200173 if (first) {
174 /* if this is text, add room for \0 at the end */
175 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
176 assert(!cmdret);
177 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200178 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600179
Jens Axboea64e88d2011-10-03 14:20:01 +0200180 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600181
Jens Axboea64e88d2011-10-03 14:20:01 +0200182 if (first)
183 memcpy(cmdret, &cmd, sizeof(cmd));
184 else
185 assert(cmdret->opcode == cmd.opcode);
186
187 if (!cmd.pdu_len)
188 break;
189
190 /* There's payload, get it */
191 pdu = (void *) cmdret->payload + pdu_offset;
192 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
193 if (ret)
194 break;
195
196 /* Verify payload crc */
197 crc = crc16(pdu, cmd.pdu_len);
198 if (crc != cmd.pdu_crc16) {
199 log_err("fio: server bad crc on payload ");
200 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
201 ret = 1;
202 break;
203 }
204
205 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200206 if (!first)
207 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200208 first = 0;
209 } while (cmd.flags & FIO_NET_CMD_F_MORE);
210
211 if (ret) {
212 free(cmdret);
213 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200214 } else if (cmdret) {
215 /* zero-terminate text input */
216 if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT ||
217 cmdret->opcode == FIO_NET_CMD_JOB)) {
218 char *buf = (char *) cmdret->payload;
219
220 buf[cmdret->pdu_len ] = '\0';
221 }
222 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200223 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200224 }
Jens Axboe132159a2011-09-30 15:01:32 -0600225
Jens Axboea64e88d2011-10-03 14:20:01 +0200226 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600227}
228
229void fio_net_cmd_crc(struct fio_net_cmd *cmd)
230{
231 uint32_t pdu_len;
232
Jens Axboeddcc0b62011-10-03 14:45:27 +0200233 cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600234
235 pdu_len = le32_to_cpu(cmd->pdu_len);
236 if (pdu_len)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200237 cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600238}
239
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200240int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
241 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600242{
Jens Axboe7f868312011-10-10 09:55:21 +0200243 struct fio_net_cmd *cmd = NULL;
244 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600245 int ret;
246
247 do {
248 this_len = size;
249 if (this_len > FIO_SERVER_MAX_PDU)
250 this_len = FIO_SERVER_MAX_PDU;
251
Jens Axboe7f868312011-10-10 09:55:21 +0200252 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
253 if (cmd)
254 free(cmd);
255
256 cur_len = sizeof(*cmd) + this_len;
257 cmd = malloc(cur_len);
258 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600259
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200260 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600261
262 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200263 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600264
265 fio_net_cmd_crc(cmd);
266
267 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600268 size -= this_len;
269 buf += this_len;
270 } while (!ret && size);
271
Jens Axboe7f868312011-10-10 09:55:21 +0200272 if (cmd)
273 free(cmd);
274
Jens Axboe794d69c2011-10-01 08:48:50 -0600275 return ret;
276}
277
Jens Axboe89c17072011-10-11 10:15:51 +0200278static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600279{
Jens Axboe178cde92011-10-05 22:14:31 +0200280 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600281
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200282 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600283 fio_net_cmd_crc(&cmd);
284
285 return fio_send_data(sk, &cmd, sizeof(cmd));
286}
287
Jens Axboe89c17072011-10-11 10:15:51 +0200288/*
289 * If 'list' is non-NULL, then allocate and store the sent command for
290 * later verification.
291 */
292int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
293 struct flist_head *list)
294{
295 struct fio_net_int_cmd *cmd;
296 int ret;
297
298 if (!list)
299 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
300
301 cmd = malloc(sizeof(*cmd));
302
Jens Axboedf380932011-10-11 14:25:08 +0200303 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
Jens Axboe89c17072011-10-11 10:15:51 +0200304 fio_net_cmd_crc(&cmd->cmd);
305
306 INIT_FLIST_HEAD(&cmd->list);
307 gettimeofday(&cmd->tv, NULL);
308 cmd->saved_tag = tag;
309
310 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
311 if (ret) {
312 free(cmd);
313 return ret;
314 }
315
316 flist_add_tail(&cmd->list, list);
317 return 0;
318}
319
Jens Axboe9abea482011-10-04 13:21:54 +0200320static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600321{
Jens Axboe46c48f12011-10-01 12:50:51 -0600322 dprint(FD_NET, "server: sending quit\n");
Jens Axboe89c17072011-10-11 10:15:51 +0200323 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600324}
325
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200326static int handle_job_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600327{
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200328 char *buf = (char *) cmd->payload;
Jens Axboea64e88d2011-10-03 14:20:01 +0200329 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600330
Jens Axboee6d1c662011-10-05 20:41:06 +0200331 if (parse_jobs_ini(buf, 1, 0)) {
332 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200333 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200334 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200335
Jens Axboe89c17072011-10-11 10:15:51 +0200336 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200337
338 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200339 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200340 reset_fio_state();
341 return ret;
342}
343
344static int handle_jobline_cmd(struct fio_net_cmd *cmd)
345{
Jens Axboefa2ea802011-10-08 21:07:29 +0200346 void *pdu = cmd->payload;
347 struct cmd_single_line_pdu *cslp;
348 struct cmd_line_pdu *clp;
349 unsigned long offset;
350 char **argv;
Jens Axboe81179ee2011-10-04 12:42:06 +0200351 int ret, i;
352
Jens Axboefa2ea802011-10-08 21:07:29 +0200353 clp = pdu;
354 clp->lines = le16_to_cpu(clp->lines);
355 argv = malloc(clp->lines * sizeof(char *));
356 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200357
Jens Axboefa2ea802011-10-08 21:07:29 +0200358 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200359
Jens Axboefa2ea802011-10-08 21:07:29 +0200360 for (i = 0; i < clp->lines; i++) {
361 cslp = pdu + offset;
362 argv[i] = (char *) cslp->text;
363
364 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200365 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
366 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200367
Jens Axboefa2ea802011-10-08 21:07:29 +0200368 if (parse_cmd_line(clp->lines, argv)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200369 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200370 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200371 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200372 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200373
Jens Axboefa2ea802011-10-08 21:07:29 +0200374 free(argv);
375
Jens Axboe89c17072011-10-11 10:15:51 +0200376 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200377
Jens Axboe794d69c2011-10-01 08:48:50 -0600378 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200379 fio_server_send_quit_cmd();
Jens Axboe794d69c2011-10-01 08:48:50 -0600380 reset_fio_state();
Jens Axboe132159a2011-09-30 15:01:32 -0600381 return ret;
382}
383
Jens Axboec28e8e82011-10-04 08:57:39 +0200384static int handle_probe_cmd(struct fio_net_cmd *cmd)
385{
386 struct cmd_probe_pdu probe;
387
Jens Axboe89c17072011-10-11 10:15:51 +0200388 dprint(FD_NET, "server: sending probe reply\n");
389
Jens Axboec28e8e82011-10-04 08:57:39 +0200390 memset(&probe, 0, sizeof(probe));
391 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200392#ifdef FIO_BIG_ENDIAN
393 probe.bigendian = 1;
394#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200395 probe.fio_major = FIO_MAJOR;
396 probe.fio_minor = FIO_MINOR;
397 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200398
Jens Axboecca84642011-10-07 12:47:57 +0200399 probe.os = FIO_OS;
400 probe.arch = FIO_ARCH;
401
Jens Axboe38fdef22011-10-11 14:30:06 +0200402 probe.bpp = sizeof(void *);
403
Jens Axboe89c17072011-10-11 10:15:51 +0200404 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200405}
406
407static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
408{
409 struct jobs_eta *je;
410 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200411 int i;
412
Jens Axboeb814fb22011-10-12 09:20:34 +0200413 if (!thread_number)
414 return 0;
415
416 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200417 je = malloc(size);
418 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200419
420 if (!calc_thread_status(je, 1)) {
421 free(je);
422 return 0;
423 }
424
425 dprint(FD_NET, "server sending status\n");
426
427 je->nr_running = cpu_to_le32(je->nr_running);
428 je->nr_ramp = cpu_to_le32(je->nr_ramp);
429 je->nr_pending = cpu_to_le32(je->nr_pending);
430 je->files_open = cpu_to_le32(je->files_open);
431 je->m_rate = cpu_to_le32(je->m_rate);
432 je->t_rate = cpu_to_le32(je->t_rate);
433 je->m_iops = cpu_to_le32(je->m_iops);
434 je->t_iops = cpu_to_le32(je->t_iops);
435
436 for (i = 0; i < 2; i++) {
437 je->rate[i] = cpu_to_le32(je->rate[i]);
438 je->iops[i] = cpu_to_le32(je->iops[i]);
439 }
440
441 je->elapsed_sec = cpu_to_le32(je->nr_running);
442 je->eta_sec = cpu_to_le64(je->eta_sec);
443
Jens Axboe7f868312011-10-10 09:55:21 +0200444 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200445 free(je);
446 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200447}
448
Jens Axboe132159a2011-09-30 15:01:32 -0600449static int handle_command(struct fio_net_cmd *cmd)
450{
451 int ret;
452
Jens Axboe89c17072011-10-11 10:15:51 +0200453 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
454 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600455
Jens Axboe132159a2011-09-30 15:01:32 -0600456 switch (cmd->opcode) {
457 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200458 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200459 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200460 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600461 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200462 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600463 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200464 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600465 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200466 case FIO_NET_CMD_JOBLINE:
467 ret = handle_jobline_cmd(cmd);
468 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200469 case FIO_NET_CMD_PROBE:
470 ret = handle_probe_cmd(cmd);
471 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200472 case FIO_NET_CMD_SEND_ETA:
473 ret = handle_send_eta_cmd(cmd);
474 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600475 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200476 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600477 ret = 1;
478 }
479
480 return ret;
481}
482
Jens Axboe70e0c312011-10-04 09:18:30 +0200483static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600484{
485 struct fio_net_cmd *cmd = NULL;
486 int ret = 0;
487
488 /* read forever */
489 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200490 struct pollfd pfd = {
491 .fd = sk,
492 .events = POLLIN,
493 };
494
495 ret = 0;
496 do {
497 ret = poll(&pfd, 1, 100);
498 if (ret < 0) {
499 if (errno == EINTR)
500 break;
501 log_err("fio: poll: %s\n", strerror(errno));
502 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200503 } else if (!ret) {
504 if (!block)
505 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200506 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200507 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200508
509 if (pfd.revents & POLLIN)
510 break;
511 if (pfd.revents & (POLLERR|POLLHUP)) {
512 ret = 1;
513 break;
514 }
Jens Axboe19c65172011-10-05 22:05:37 +0200515 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200516
517 if (ret < 0)
518 break;
519
520 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600521 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200522 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600523 break;
524 }
525
Jens Axboe132159a2011-09-30 15:01:32 -0600526 ret = handle_command(cmd);
527 if (ret)
528 break;
529
530 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400531 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600532 }
533
534 if (cmd)
535 free(cmd);
536
537 return ret;
538}
539
Jens Axboecc0df002011-10-03 20:53:32 +0200540void fio_server_idle_loop(void)
541{
542 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200543 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200544}
545
Jens Axboe50d16972011-09-29 17:45:28 -0600546static int accept_loop(int listen_sk)
547{
Jens Axboebb447a22011-10-04 15:06:42 +0200548 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200549 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600550 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600551 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600552
Jens Axboe60efd142011-10-04 13:30:11 +0200553 dprint(FD_NET, "server enter accept loop\n");
554
Jens Axboe009b1be2011-09-29 18:27:02 -0600555 flags = fcntl(listen_sk, F_GETFL);
556 flags |= O_NONBLOCK;
557 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600558again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600559 pfd.fd = listen_sk;
560 pfd.events = POLLIN;
561 do {
562 ret = poll(&pfd, 1, 100);
563 if (ret < 0) {
564 if (errno == EINTR)
565 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600566 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600567 goto out;
568 } else if (!ret)
569 continue;
570
571 if (pfd.revents & POLLIN)
572 break;
573 } while (!exit_backend);
574
575 if (exit_backend)
576 goto out;
577
Jens Axboe6b976bf2011-10-04 15:07:43 +0200578 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600579 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600580 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600581 return -1;
582 }
583
Jens Axboebb447a22011-10-04 15:06:42 +0200584 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600585
Jens Axboe37db14f2011-09-30 17:00:42 -0600586 server_fd = sk;
587
Jens Axboe70e0c312011-10-04 09:18:30 +0200588 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600589
Jens Axboe37db14f2011-09-30 17:00:42 -0600590 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600591 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600592
Jens Axboe009b1be2011-09-29 18:27:02 -0600593 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600594 goto again;
595
Jens Axboe009b1be2011-09-29 18:27:02 -0600596out:
Jens Axboe132159a2011-09-30 15:01:32 -0600597 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600598}
599
Jens Axboe13755d92011-10-10 19:51:26 +0200600int fio_server_text_output(const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600601{
Jens Axboe337d75a2011-10-01 12:36:32 -0600602 if (server_fd != -1)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200603 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0);
Jens Axboe337d75a2011-10-01 12:36:32 -0600604
Jens Axboe13755d92011-10-10 19:51:26 +0200605 return log_local_buf(buf, len);
Jens Axboe142575e2011-09-30 17:35:45 -0600606}
607
Jens Axboea64e88d2011-10-03 14:20:01 +0200608static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
609{
610 dst->max_val = cpu_to_le64(src->max_val);
611 dst->min_val = cpu_to_le64(src->min_val);
612 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200613
614 /*
615 * Encode to IEEE 754 for network transfer
616 */
617 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
618 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200619}
620
621static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
622{
623 int i;
624
625 for (i = 0; i < 2; i++) {
626 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
627 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
628 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
629 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
630 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
631 dst->agg[i] = cpu_to_le64(src->agg[i]);
632 }
633
634 dst->kb_base = cpu_to_le32(src->kb_base);
635 dst->groupid = cpu_to_le32(src->groupid);
636}
637
638/*
639 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
640 * into a single payload.
641 */
642void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
643{
644 struct cmd_ts_pdu p;
645 int i, j;
646
Jens Axboe60efd142011-10-04 13:30:11 +0200647 dprint(FD_NET, "server sending end stats\n");
648
Jens Axboe317b3c82011-10-03 21:47:27 +0200649 memset(&p, 0, sizeof(p));
650
Jens Axboea64e88d2011-10-03 14:20:01 +0200651 strcpy(p.ts.name, ts->name);
652 strcpy(p.ts.verror, ts->verror);
653 strcpy(p.ts.description, ts->description);
654
Jens Axboeddcc0b62011-10-03 14:45:27 +0200655 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200656 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200657 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200658 p.ts.members = cpu_to_le32(ts->members);
659
660 for (i = 0; i < 2; i++) {
661 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
662 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
663 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
664 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
665 }
666
667 p.ts.usr_time = cpu_to_le64(ts->usr_time);
668 p.ts.sys_time = cpu_to_le64(ts->sys_time);
669 p.ts.ctx = cpu_to_le64(ts->ctx);
670 p.ts.minf = cpu_to_le64(ts->minf);
671 p.ts.majf = cpu_to_le64(ts->majf);
672 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200673
674 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200675 fio_fp64_t *src = &ts->percentile_list[i];
676 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200677
Jens Axboecfc03e42011-10-12 21:20:42 +0200678 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200679 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200680
681 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
682 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
683 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
684 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
685 }
686
687 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
688 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
689 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
690 }
691
692 for (i = 0; i < 2; i++)
693 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
694 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
695
696 for (i = 0; i < 3; i++) {
697 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200698 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200699 }
700
Jens Axboe93eee042011-10-03 21:08:48 +0200701 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200702 p.ts.total_complete = cpu_to_le64(ts->total_complete);
703
704 for (i = 0; i < 2; i++) {
705 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
706 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
707 }
708
709 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
710 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
711 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200712 p.ts.first_error = cpu_to_le32(ts->first_error);
713 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200714
715 convert_gs(&p.rs, rs);
716
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200717 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200718}
719
720void fio_server_send_gs(struct group_run_stats *rs)
721{
722 struct group_run_stats gs;
723
Jens Axboe60efd142011-10-04 13:30:11 +0200724 dprint(FD_NET, "server sending group run stats\n");
725
Jens Axboea64e88d2011-10-03 14:20:01 +0200726 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200727 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200728}
729
Jens Axboed09a64a2011-10-13 11:38:56 +0200730static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
731{
732 int i;
733
734 for (i = 0; i < 2; i++) {
735 dst->ios[i] = cpu_to_le32(src->ios[i]);
736 dst->merges[i] = cpu_to_le32(src->merges[i]);
737 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
738 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
739 }
740
741 dst->io_ticks = cpu_to_le32(src->io_ticks);
742 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
743 dst->slavecount = cpu_to_le32(src->slavecount);
744 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
745}
746
747static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
748{
749 int i;
750
751 strcpy((char *) dst->name, (char *) src->name);
752
753 for (i = 0; i < 2; i++) {
754 dst->ios[i] = cpu_to_le32(src->ios[i]);
755 dst->merges[i] = cpu_to_le32(src->merges[i]);
756 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
757 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
758 }
759
760 dst->io_ticks = cpu_to_le32(src->io_ticks);
761 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
762 dst->msec = cpu_to_le64(src->msec);
763}
764
765void fio_server_send_du(void)
766{
767 struct disk_util *du;
768 struct flist_head *entry;
769 struct cmd_du_pdu pdu;
770
771 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
772
Jens Axboe0766d922011-10-13 12:02:08 +0200773 memset(&pdu, 0, sizeof(pdu));
774
Jens Axboed09a64a2011-10-13 11:38:56 +0200775 flist_for_each(entry, &disk_list) {
776 du = flist_entry(entry, struct disk_util, list);
777
778 convert_dus(&pdu.dus, &du->dus);
779 convert_agg(&pdu.agg, &du->agg);
780
781 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
782 }
783}
784
Jens Axboe142575e2011-09-30 17:35:45 -0600785int fio_server_log(const char *format, ...)
786{
787 char buffer[1024];
788 va_list args;
Jens Axboe82fa6b22011-09-30 22:29:08 -0600789 size_t len;
Jens Axboe142575e2011-09-30 17:35:45 -0600790
Jens Axboe60efd142011-10-04 13:30:11 +0200791 dprint(FD_NET, "server log\n");
792
Jens Axboe142575e2011-09-30 17:35:45 -0600793 va_start(args, format);
Jens Axboe82fa6b22011-09-30 22:29:08 -0600794 len = vsnprintf(buffer, sizeof(buffer), format, args);
Jens Axboe142575e2011-09-30 17:35:45 -0600795 va_end(args);
796
Jens Axboe82fa6b22011-09-30 22:29:08 -0600797 return fio_server_text_output(buffer, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600798}
Jens Axboee46d8092011-10-03 09:11:02 +0200799
Jens Axboe87aa8f12011-10-06 21:24:13 +0200800static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200801{
Jens Axboe87aa8f12011-10-06 21:24:13 +0200802 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200803
804 sk = socket(AF_INET, SOCK_STREAM, 0);
805 if (sk < 0) {
806 log_err("fio: socket: %s\n", strerror(errno));
807 return -1;
808 }
809
810 opt = 1;
811 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
812 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200813 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200814 return -1;
815 }
816#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200817 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200818 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200819 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200820 return -1;
821 }
822#endif
823
824 saddr_in.sin_family = AF_INET;
Jens Axboe81179ee2011-10-04 12:42:06 +0200825
826 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
827 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200828 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200829 return -1;
830 }
831
Jens Axboe87aa8f12011-10-06 21:24:13 +0200832 return sk;
833}
834
835static int fio_init_server_sock(void)
836{
837 struct sockaddr_un addr;
838 fio_socklen_t len;
839 mode_t mode;
840 int sk;
841
842 sk = socket(AF_UNIX, SOCK_STREAM, 0);
843 if (sk < 0) {
844 log_err("fio: socket: %s\n", strerror(errno));
845 return -1;
846 }
847
848 mode = umask(000);
849
850 memset(&addr, 0, sizeof(addr));
851 addr.sun_family = AF_UNIX;
852 strcpy(addr.sun_path, bind_sock);
853 unlink(bind_sock);
854
855 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
856
857 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
858 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200859 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200860 return -1;
861 }
862
863 umask(mode);
864 return sk;
865}
866
867static int fio_init_server_connection(void)
868{
Jens Axboebebe6392011-10-07 10:00:51 +0200869 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +0200870 int sk;
871
872 dprint(FD_NET, "starting server\n");
873
874 if (!bind_sock)
875 sk = fio_init_server_ip();
876 else
877 sk = fio_init_server_sock();
878
879 if (sk < 0)
880 return sk;
881
Jens Axboebebe6392011-10-07 10:00:51 +0200882 if (!bind_sock)
883 sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port);
884 else
885 strcpy(bind_str, bind_sock);
886
887 log_info("fio: server listening on %s\n", bind_str);
888
Jens Axboe89c17072011-10-11 10:15:51 +0200889 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200890 log_err("fio: listen: %s\n", strerror(errno));
891 return -1;
892 }
893
Jens Axboe87aa8f12011-10-06 21:24:13 +0200894 return sk;
895}
896
Jens Axboebebe6392011-10-07 10:00:51 +0200897int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
898 int *port, struct in_addr *inp)
899{
900 *ptr = NULL;
901 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +0200902 *port = fio_net_port;
Jens Axboebebe6392011-10-07 10:00:51 +0200903
904 if (!strncmp(str, "sock:", 5)) {
905 *ptr = strdup(str + 5);
906 *is_sock = 1;
907 } else {
908 const char *host = str;
909 char *portp;
910 int lport = 0;
911
912 /*
913 * Is it ip:<ip or host>:port
914 */
915 if (!strncmp(host, "ip:", 3))
916 host += 3;
917 else if (host[0] == ':') {
918 /* String is :port */
919 host++;
920 lport = atoi(host);
921 if (!lport || lport > 65535) {
922 log_err("fio: bad server port %u\n", port);
923 return 1;
924 }
925 /* no hostname given, we are done */
926 *port = lport;
927 return 0;
928 }
929
930 /*
931 * If no port seen yet, check if there's a last ':' at the end
932 */
933 if (!lport) {
934 portp = strchr(host, ':');
935 if (portp) {
936 *portp = '\0';
937 portp++;
938 lport = atoi(portp);
939 if (!lport || lport > 65535) {
940 log_err("fio: bad server port %u\n", port);
941 return 1;
942 }
943 }
944 }
945
946 if (lport)
947 *port = lport;
948
949 *ptr = strdup(host);
950
951 if (inet_aton(host, inp) != 1) {
952 struct hostent *hent;
953
954 hent = gethostbyname(host);
955 if (!hent) {
Jens Axboebebe6392011-10-07 10:00:51 +0200956 free(*ptr);
957 *ptr = NULL;
958 return 1;
959 }
960
961 memcpy(inp, hent->h_addr, 4);
962 }
963 }
964
965 if (*port == 0)
966 *port = fio_net_port;
967
968 return 0;
969}
970
Jens Axboe87aa8f12011-10-06 21:24:13 +0200971/*
972 * Server arg should be one of:
973 *
974 * sock:/path/to/socket
975 * ip:1.2.3.4
976 * 1.2.3.4
977 *
978 * Where sock uses unix domain sockets, and ip binds the server to
979 * a specific interface. If no arguments are given to the server, it
980 * uses IP and binds to 0.0.0.0.
981 *
982 */
983static int fio_handle_server_arg(void)
984{
Jens Axboe6d2cf392011-10-07 13:19:28 +0200985 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +0200986 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +0200987
Jens Axboe87aa8f12011-10-06 21:24:13 +0200988 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
989
990 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +0200991 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200992
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200993 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe6d2cf392011-10-07 13:19:28 +0200994 &port, &saddr_in.sin_addr);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200995
996 if (!is_sock && bind_sock) {
997 free(bind_sock);
998 bind_sock = NULL;
999 }
1000
Jens Axboea7de0a12011-10-07 12:55:14 +02001001out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001002 fio_net_port = port;
1003 saddr_in.sin_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001004 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001005}
1006
1007static int fio_server(void)
1008{
1009 int sk, ret;
1010
1011 dprint(FD_NET, "starting server\n");
1012
1013 if (fio_handle_server_arg())
1014 return -1;
1015
1016 sk = fio_init_server_connection();
1017 if (sk < 0)
1018 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001019
1020 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001021
Jens Axboe81179ee2011-10-04 12:42:06 +02001022 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001023
1024 if (fio_server_arg) {
1025 free(fio_server_arg);
1026 fio_server_arg = NULL;
1027 }
Jens Axboebebe6392011-10-07 10:00:51 +02001028 if (bind_sock)
1029 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001030
Jens Axboe81179ee2011-10-04 12:42:06 +02001031 return ret;
1032}
1033
Jens Axboe7b821682011-10-11 12:16:32 +02001034void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001035{
Jens Axboe7b821682011-10-11 12:16:32 +02001036 if (signal == SIGPIPE)
1037 server_fd = -1;
1038 else {
1039 log_info("\nfio: terminating on signal %d\n", signal);
1040 exit_backend = 1;
1041 }
Jens Axboe9abea482011-10-04 13:21:54 +02001042}
1043
Jens Axboe13755d92011-10-10 19:51:26 +02001044static int check_existing_pidfile(const char *pidfile)
1045{
1046 struct stat sb;
1047 char buf[16];
1048 pid_t pid;
1049 FILE *f;
1050
1051 if (stat(pidfile, &sb))
1052 return 0;
1053
1054 f = fopen(pidfile, "r");
1055 if (!f)
1056 return 0;
1057
Jens Axboebfc3b172011-10-10 21:16:55 +02001058 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001059 fclose(f);
1060 return 1;
1061 }
1062 fclose(f);
1063
1064 pid = atoi(buf);
1065 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001066 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001067
1068 return 1;
1069}
1070
1071static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001072{
1073 FILE *fpid;
1074
1075 fpid = fopen(pidfile, "w");
1076 if (!fpid) {
1077 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001078 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001079 }
1080
1081 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001082 fclose(fpid);
1083 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001084}
1085
1086/*
1087 * If pidfile is specified, background us.
1088 */
1089int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001090{
1091 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001092 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001093
Jens Axboe402668f2011-10-10 15:28:58 +02001094 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001095 return fio_server();
1096
Jens Axboe13755d92011-10-10 19:51:26 +02001097 if (check_existing_pidfile(pidfile)) {
1098 log_err("fio: pidfile %s exists and server appears alive\n",
1099 pidfile);
1100 return -1;
1101 }
1102
Jens Axboee46d8092011-10-03 09:11:02 +02001103 pid = fork();
1104 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001105 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001106 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001107 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001108 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001109 int ret = write_pid(pid, pidfile);
1110
1111 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001112 }
Jens Axboee46d8092011-10-03 09:11:02 +02001113
1114 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001115 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1116 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001117 close(STDIN_FILENO);
1118 close(STDOUT_FILENO);
1119 close(STDERR_FILENO);
1120 f_out = NULL;
1121 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001122
1123 ret = fio_server();
1124
1125 closelog();
1126 unlink(pidfile);
1127 free(pidfile);
1128 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001129}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001130
Jens Axboebebe6392011-10-07 10:00:51 +02001131void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001132{
1133 fio_server_arg = strdup(arg);
1134}