blob: 4da8bf0040397d5173987b3b993e5a794f1959fc [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 Axboe01be0382011-10-15 14:43:41 +020035static int first_cmd_check;
Jens Axboe37db14f2011-09-30 17:00:42 -060036
Jens Axboe89c17072011-10-11 10:15:51 +020037static const char *fio_server_ops[FIO_NET_CMD_NR] = {
38 "",
39 "QUIT",
40 "EXIT",
41 "JOB",
42 "JOBLINE",
43 "TEXT",
44 "TS",
45 "GS",
46 "SEND_ETA",
47 "ETA",
48 "PROBE",
49 "START",
Jens Axboed09a64a2011-10-13 11:38:56 +020050 "STOP",
51 "DISK_UTIL",
Jens Axboe01be0382011-10-15 14:43:41 +020052 "RUN",
Jens Axboe89c17072011-10-11 10:15:51 +020053};
54
55const char *fio_server_op(unsigned int op)
56{
57 static char buf[32];
58
59 if (op < FIO_NET_CMD_NR)
60 return fio_server_ops[op];
61
62 sprintf(buf, "UNKNOWN/%d", op);
63 return buf;
64}
65
Jens Axboe132159a2011-09-30 15:01:32 -060066int fio_send_data(int sk, const void *p, unsigned int len)
67{
Jens Axboe794d69c2011-10-01 08:48:50 -060068 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU);
69
Jens Axboe132159a2011-09-30 15:01:32 -060070 do {
71 int ret = send(sk, p, len, 0);
72
73 if (ret > 0) {
74 len -= ret;
75 if (!len)
76 break;
77 p += ret;
78 continue;
79 } else if (!ret)
80 break;
81 else if (errno == EAGAIN || errno == EINTR)
82 continue;
Jens Axboe7b821682011-10-11 12:16:32 +020083 else
84 break;
Jens Axboe132159a2011-09-30 15:01:32 -060085 } while (!exit_backend);
86
87 if (!len)
88 return 0;
89
90 return 1;
91}
92
93int fio_recv_data(int sk, void *p, unsigned int len)
94{
95 do {
96 int ret = recv(sk, p, len, MSG_WAITALL);
97
98 if (ret > 0) {
99 len -= ret;
100 if (!len)
101 break;
102 p += ret;
103 continue;
104 } else if (!ret)
105 break;
106 else if (errno == EAGAIN || errno == EINTR)
107 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200108 else
109 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600110 } while (!exit_backend);
111
112 if (!len)
113 return 0;
114
115 return -1;
116}
117
118static int verify_convert_cmd(struct fio_net_cmd *cmd)
119{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600120 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600121
Jens Axboefcee5ff2011-09-30 22:50:39 -0600122 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
123 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600124
Jens Axboefcee5ff2011-09-30 22:50:39 -0600125 crc = crc16(cmd, FIO_NET_CMD_CRC_SZ);
126 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600127 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600128 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600129 return 1;
130 }
131
132 cmd->version = le16_to_cpu(cmd->version);
133 cmd->opcode = le16_to_cpu(cmd->opcode);
134 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200135 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600136 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
137
138 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200139 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600140 break;
141 default:
142 log_err("fio: bad server cmd version %d\n", cmd->version);
143 return 1;
144 }
145
146 if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
147 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
148 return 1;
149 }
150
151 return 0;
152}
153
Jens Axboea64e88d2011-10-03 14:20:01 +0200154/*
155 * Read (and defragment, if necessary) incoming commands
156 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200157struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600158{
Jens Axboea64e88d2011-10-03 14:20:01 +0200159 struct fio_net_cmd cmd, *cmdret = NULL;
160 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600161 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200162 int ret, first = 1;
163 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600164
Jens Axboea64e88d2011-10-03 14:20:01 +0200165 do {
166 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
167 if (ret)
168 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600169
Jens Axboea64e88d2011-10-03 14:20:01 +0200170 /* We have a command, verify it and swap if need be */
171 ret = verify_convert_cmd(&cmd);
172 if (ret)
173 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600174
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200175 if (first) {
176 /* if this is text, add room for \0 at the end */
177 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
178 assert(!cmdret);
179 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200180 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600181
Jens Axboea64e88d2011-10-03 14:20:01 +0200182 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600183
Jens Axboea64e88d2011-10-03 14:20:01 +0200184 if (first)
185 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200186 else if (cmdret->opcode != cmd.opcode) {
187 log_err("fio: fragment opcode mismatch (%d != %d)\n",
188 cmdret->opcode, cmd.opcode);
189 ret = 1;
190 break;
191 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200192
193 if (!cmd.pdu_len)
194 break;
195
196 /* There's payload, get it */
197 pdu = (void *) cmdret->payload + pdu_offset;
198 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
199 if (ret)
200 break;
201
202 /* Verify payload crc */
203 crc = crc16(pdu, cmd.pdu_len);
204 if (crc != cmd.pdu_crc16) {
205 log_err("fio: server bad crc on payload ");
206 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
207 ret = 1;
208 break;
209 }
210
211 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200212 if (!first)
213 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200214 first = 0;
215 } while (cmd.flags & FIO_NET_CMD_F_MORE);
216
217 if (ret) {
218 free(cmdret);
219 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200220 } else if (cmdret) {
221 /* zero-terminate text input */
222 if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT ||
223 cmdret->opcode == FIO_NET_CMD_JOB)) {
224 char *buf = (char *) cmdret->payload;
225
226 buf[cmdret->pdu_len ] = '\0';
227 }
228 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200229 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200230 }
Jens Axboe132159a2011-09-30 15:01:32 -0600231
Jens Axboea64e88d2011-10-03 14:20:01 +0200232 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600233}
234
235void fio_net_cmd_crc(struct fio_net_cmd *cmd)
236{
237 uint32_t pdu_len;
238
Jens Axboeddcc0b62011-10-03 14:45:27 +0200239 cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600240
241 pdu_len = le32_to_cpu(cmd->pdu_len);
242 if (pdu_len)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200243 cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600244}
245
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200246int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
247 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600248{
Jens Axboe7f868312011-10-10 09:55:21 +0200249 struct fio_net_cmd *cmd = NULL;
250 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600251 int ret;
252
253 do {
254 this_len = size;
255 if (this_len > FIO_SERVER_MAX_PDU)
256 this_len = FIO_SERVER_MAX_PDU;
257
Jens Axboe7f868312011-10-10 09:55:21 +0200258 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
259 if (cmd)
260 free(cmd);
261
262 cur_len = sizeof(*cmd) + this_len;
263 cmd = malloc(cur_len);
264 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600265
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200266 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600267
268 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200269 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600270
271 fio_net_cmd_crc(cmd);
272
273 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600274 size -= this_len;
275 buf += this_len;
276 } while (!ret && size);
277
Jens Axboe7f868312011-10-10 09:55:21 +0200278 if (cmd)
279 free(cmd);
280
Jens Axboe794d69c2011-10-01 08:48:50 -0600281 return ret;
282}
283
Jens Axboe89c17072011-10-11 10:15:51 +0200284static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600285{
Jens Axboe178cde92011-10-05 22:14:31 +0200286 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600287
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200288 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600289 fio_net_cmd_crc(&cmd);
290
291 return fio_send_data(sk, &cmd, sizeof(cmd));
292}
293
Jens Axboe89c17072011-10-11 10:15:51 +0200294/*
295 * If 'list' is non-NULL, then allocate and store the sent command for
296 * later verification.
297 */
298int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
299 struct flist_head *list)
300{
301 struct fio_net_int_cmd *cmd;
302 int ret;
303
304 if (!list)
305 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
306
307 cmd = malloc(sizeof(*cmd));
308
Jens Axboedf380932011-10-11 14:25:08 +0200309 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
Jens Axboe89c17072011-10-11 10:15:51 +0200310 fio_net_cmd_crc(&cmd->cmd);
311
312 INIT_FLIST_HEAD(&cmd->list);
313 gettimeofday(&cmd->tv, NULL);
314 cmd->saved_tag = tag;
315
316 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
317 if (ret) {
318 free(cmd);
319 return ret;
320 }
321
322 flist_add_tail(&cmd->list, list);
323 return 0;
324}
325
Jens Axboe9abea482011-10-04 13:21:54 +0200326static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600327{
Jens Axboe46c48f12011-10-01 12:50:51 -0600328 dprint(FD_NET, "server: sending quit\n");
Jens Axboe89c17072011-10-11 10:15:51 +0200329 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600330}
331
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200332static int handle_job_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600333{
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200334 char *buf = (char *) cmd->payload;
Jens Axboe11e950b2011-10-16 21:34:14 +0200335 struct cmd_start_pdu spdu;
336 struct cmd_end_pdu epdu;
Jens Axboea64e88d2011-10-03 14:20:01 +0200337 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600338
Jens Axboee6d1c662011-10-05 20:41:06 +0200339 if (parse_jobs_ini(buf, 1, 0)) {
340 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200341 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200342 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200343
Jens Axboe11e950b2011-10-16 21:34:14 +0200344 spdu.jobs = cpu_to_le32(thread_number);
345 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200346
347 ret = exec_run();
Jens Axboe11e950b2011-10-16 21:34:14 +0200348
349 epdu.error = ret;
350 fio_net_send_cmd(server_fd, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0);
351
Jens Axboe9abea482011-10-04 13:21:54 +0200352 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200353 reset_fio_state();
354 return ret;
355}
356
357static int handle_jobline_cmd(struct fio_net_cmd *cmd)
358{
Jens Axboefa2ea802011-10-08 21:07:29 +0200359 void *pdu = cmd->payload;
360 struct cmd_single_line_pdu *cslp;
361 struct cmd_line_pdu *clp;
362 unsigned long offset;
363 char **argv;
Jens Axboe81179ee2011-10-04 12:42:06 +0200364 int ret, i;
365
Jens Axboefa2ea802011-10-08 21:07:29 +0200366 clp = pdu;
367 clp->lines = le16_to_cpu(clp->lines);
368 argv = malloc(clp->lines * sizeof(char *));
369 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200370
Jens Axboefa2ea802011-10-08 21:07:29 +0200371 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200372
Jens Axboefa2ea802011-10-08 21:07:29 +0200373 for (i = 0; i < clp->lines; i++) {
374 cslp = pdu + offset;
375 argv[i] = (char *) cslp->text;
376
377 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200378 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
379 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200380
Jens Axboefa2ea802011-10-08 21:07:29 +0200381 if (parse_cmd_line(clp->lines, argv)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200382 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200383 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200384 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200385 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200386
Jens Axboefa2ea802011-10-08 21:07:29 +0200387 free(argv);
388
Jens Axboe89c17072011-10-11 10:15:51 +0200389 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200390
Jens Axboe794d69c2011-10-01 08:48:50 -0600391 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200392 fio_server_send_quit_cmd();
Jens Axboe794d69c2011-10-01 08:48:50 -0600393 reset_fio_state();
Jens Axboe132159a2011-09-30 15:01:32 -0600394 return ret;
395}
396
Jens Axboec28e8e82011-10-04 08:57:39 +0200397static int handle_probe_cmd(struct fio_net_cmd *cmd)
398{
399 struct cmd_probe_pdu probe;
400
Jens Axboe89c17072011-10-11 10:15:51 +0200401 dprint(FD_NET, "server: sending probe reply\n");
402
Jens Axboec28e8e82011-10-04 08:57:39 +0200403 memset(&probe, 0, sizeof(probe));
404 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200405#ifdef FIO_BIG_ENDIAN
406 probe.bigendian = 1;
407#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200408 probe.fio_major = FIO_MAJOR;
409 probe.fio_minor = FIO_MINOR;
410 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200411
Jens Axboecca84642011-10-07 12:47:57 +0200412 probe.os = FIO_OS;
413 probe.arch = FIO_ARCH;
414
Jens Axboe38fdef22011-10-11 14:30:06 +0200415 probe.bpp = sizeof(void *);
416
Jens Axboe89c17072011-10-11 10:15:51 +0200417 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200418}
419
420static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
421{
422 struct jobs_eta *je;
423 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200424 int i;
425
Jens Axboeb814fb22011-10-12 09:20:34 +0200426 if (!thread_number)
427 return 0;
428
429 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200430 je = malloc(size);
431 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200432
433 if (!calc_thread_status(je, 1)) {
434 free(je);
435 return 0;
436 }
437
438 dprint(FD_NET, "server sending status\n");
439
440 je->nr_running = cpu_to_le32(je->nr_running);
441 je->nr_ramp = cpu_to_le32(je->nr_ramp);
442 je->nr_pending = cpu_to_le32(je->nr_pending);
443 je->files_open = cpu_to_le32(je->files_open);
444 je->m_rate = cpu_to_le32(je->m_rate);
445 je->t_rate = cpu_to_le32(je->t_rate);
446 je->m_iops = cpu_to_le32(je->m_iops);
447 je->t_iops = cpu_to_le32(je->t_iops);
448
449 for (i = 0; i < 2; i++) {
450 je->rate[i] = cpu_to_le32(je->rate[i]);
451 je->iops[i] = cpu_to_le32(je->iops[i]);
452 }
453
454 je->elapsed_sec = cpu_to_le32(je->nr_running);
455 je->eta_sec = cpu_to_le64(je->eta_sec);
456
Jens Axboe7f868312011-10-10 09:55:21 +0200457 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200458 free(je);
459 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200460}
461
Jens Axboe132159a2011-09-30 15:01:32 -0600462static int handle_command(struct fio_net_cmd *cmd)
463{
464 int ret;
465
Jens Axboe89c17072011-10-11 10:15:51 +0200466 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
467 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600468
Jens Axboe132159a2011-09-30 15:01:32 -0600469 switch (cmd->opcode) {
470 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200471 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200472 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200473 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600474 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200475 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600476 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200477 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600478 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200479 case FIO_NET_CMD_JOBLINE:
480 ret = handle_jobline_cmd(cmd);
481 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200482 case FIO_NET_CMD_PROBE:
483 ret = handle_probe_cmd(cmd);
484 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200485 case FIO_NET_CMD_SEND_ETA:
486 ret = handle_send_eta_cmd(cmd);
487 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600488 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200489 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600490 ret = 1;
491 }
492
493 return ret;
494}
495
Jens Axboe70e0c312011-10-04 09:18:30 +0200496static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600497{
498 struct fio_net_cmd *cmd = NULL;
499 int ret = 0;
500
501 /* read forever */
502 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200503 struct pollfd pfd = {
504 .fd = sk,
505 .events = POLLIN,
506 };
507
508 ret = 0;
509 do {
510 ret = poll(&pfd, 1, 100);
511 if (ret < 0) {
512 if (errno == EINTR)
513 break;
514 log_err("fio: poll: %s\n", strerror(errno));
515 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200516 } else if (!ret) {
517 if (!block)
518 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200519 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200520 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200521
522 if (pfd.revents & POLLIN)
523 break;
524 if (pfd.revents & (POLLERR|POLLHUP)) {
525 ret = 1;
526 break;
527 }
Jens Axboe19c65172011-10-05 22:05:37 +0200528 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200529
530 if (ret < 0)
531 break;
532
533 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600534 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200535 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600536 break;
537 }
538
Jens Axboe132159a2011-09-30 15:01:32 -0600539 ret = handle_command(cmd);
540 if (ret)
541 break;
542
543 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400544 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600545 }
546
547 if (cmd)
548 free(cmd);
549
550 return ret;
551}
552
Jens Axboecc0df002011-10-03 20:53:32 +0200553void fio_server_idle_loop(void)
554{
Jens Axboe01be0382011-10-15 14:43:41 +0200555 if (!first_cmd_check)
556 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_RUN, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200557 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200558 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200559}
560
Jens Axboe50d16972011-09-29 17:45:28 -0600561static int accept_loop(int listen_sk)
562{
Jens Axboebb447a22011-10-04 15:06:42 +0200563 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200564 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600565 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600566 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600567
Jens Axboe60efd142011-10-04 13:30:11 +0200568 dprint(FD_NET, "server enter accept loop\n");
569
Jens Axboe009b1be2011-09-29 18:27:02 -0600570 flags = fcntl(listen_sk, F_GETFL);
571 flags |= O_NONBLOCK;
572 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600573again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600574 pfd.fd = listen_sk;
575 pfd.events = POLLIN;
576 do {
577 ret = poll(&pfd, 1, 100);
578 if (ret < 0) {
579 if (errno == EINTR)
580 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600581 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600582 goto out;
583 } else if (!ret)
584 continue;
585
586 if (pfd.revents & POLLIN)
587 break;
588 } while (!exit_backend);
589
590 if (exit_backend)
591 goto out;
592
Jens Axboe6b976bf2011-10-04 15:07:43 +0200593 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600594 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600595 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600596 return -1;
597 }
598
Jens Axboebb447a22011-10-04 15:06:42 +0200599 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600600
Jens Axboe37db14f2011-09-30 17:00:42 -0600601 server_fd = sk;
602
Jens Axboe70e0c312011-10-04 09:18:30 +0200603 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600604
Jens Axboe37db14f2011-09-30 17:00:42 -0600605 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600606 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600607
Jens Axboe009b1be2011-09-29 18:27:02 -0600608 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600609 goto again;
610
Jens Axboe009b1be2011-09-29 18:27:02 -0600611out:
Jens Axboe132159a2011-09-30 15:01:32 -0600612 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600613}
614
Jens Axboe13755d92011-10-10 19:51:26 +0200615int fio_server_text_output(const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600616{
Jens Axboe337d75a2011-10-01 12:36:32 -0600617 if (server_fd != -1)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200618 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0);
Jens Axboe337d75a2011-10-01 12:36:32 -0600619
Jens Axboe13755d92011-10-10 19:51:26 +0200620 return log_local_buf(buf, len);
Jens Axboe142575e2011-09-30 17:35:45 -0600621}
622
Jens Axboea64e88d2011-10-03 14:20:01 +0200623static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
624{
625 dst->max_val = cpu_to_le64(src->max_val);
626 dst->min_val = cpu_to_le64(src->min_val);
627 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200628
629 /*
630 * Encode to IEEE 754 for network transfer
631 */
632 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
633 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200634}
635
636static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
637{
638 int i;
639
640 for (i = 0; i < 2; i++) {
641 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
642 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
643 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
644 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
645 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
646 dst->agg[i] = cpu_to_le64(src->agg[i]);
647 }
648
649 dst->kb_base = cpu_to_le32(src->kb_base);
650 dst->groupid = cpu_to_le32(src->groupid);
651}
652
653/*
654 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
655 * into a single payload.
656 */
657void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
658{
659 struct cmd_ts_pdu p;
660 int i, j;
661
Jens Axboe60efd142011-10-04 13:30:11 +0200662 dprint(FD_NET, "server sending end stats\n");
663
Jens Axboe317b3c82011-10-03 21:47:27 +0200664 memset(&p, 0, sizeof(p));
665
Jens Axboea64e88d2011-10-03 14:20:01 +0200666 strcpy(p.ts.name, ts->name);
667 strcpy(p.ts.verror, ts->verror);
668 strcpy(p.ts.description, ts->description);
669
Jens Axboeddcc0b62011-10-03 14:45:27 +0200670 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200671 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200672 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200673 p.ts.members = cpu_to_le32(ts->members);
674
675 for (i = 0; i < 2; i++) {
676 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
677 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
678 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
679 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
680 }
681
682 p.ts.usr_time = cpu_to_le64(ts->usr_time);
683 p.ts.sys_time = cpu_to_le64(ts->sys_time);
684 p.ts.ctx = cpu_to_le64(ts->ctx);
685 p.ts.minf = cpu_to_le64(ts->minf);
686 p.ts.majf = cpu_to_le64(ts->majf);
687 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200688
689 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200690 fio_fp64_t *src = &ts->percentile_list[i];
691 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200692
Jens Axboecfc03e42011-10-12 21:20:42 +0200693 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200694 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200695
696 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
697 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
698 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
699 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
700 }
701
702 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
703 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
704 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
705 }
706
707 for (i = 0; i < 2; i++)
708 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
709 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
710
711 for (i = 0; i < 3; i++) {
712 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200713 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200714 }
715
Jens Axboe93eee042011-10-03 21:08:48 +0200716 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200717 p.ts.total_complete = cpu_to_le64(ts->total_complete);
718
719 for (i = 0; i < 2; i++) {
720 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
721 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
722 }
723
724 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
725 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
726 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200727 p.ts.first_error = cpu_to_le32(ts->first_error);
728 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200729
730 convert_gs(&p.rs, rs);
731
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200732 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200733}
734
735void fio_server_send_gs(struct group_run_stats *rs)
736{
737 struct group_run_stats gs;
738
Jens Axboe60efd142011-10-04 13:30:11 +0200739 dprint(FD_NET, "server sending group run stats\n");
740
Jens Axboea64e88d2011-10-03 14:20:01 +0200741 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200742 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200743}
744
Jens Axboed09a64a2011-10-13 11:38:56 +0200745static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
746{
747 int i;
748
749 for (i = 0; i < 2; i++) {
750 dst->ios[i] = cpu_to_le32(src->ios[i]);
751 dst->merges[i] = cpu_to_le32(src->merges[i]);
752 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
753 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
754 }
755
756 dst->io_ticks = cpu_to_le32(src->io_ticks);
757 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
758 dst->slavecount = cpu_to_le32(src->slavecount);
759 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
760}
761
762static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
763{
764 int i;
765
766 strcpy((char *) dst->name, (char *) src->name);
767
768 for (i = 0; i < 2; i++) {
769 dst->ios[i] = cpu_to_le32(src->ios[i]);
770 dst->merges[i] = cpu_to_le32(src->merges[i]);
771 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
772 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
773 }
774
775 dst->io_ticks = cpu_to_le32(src->io_ticks);
776 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
777 dst->msec = cpu_to_le64(src->msec);
778}
779
780void fio_server_send_du(void)
781{
782 struct disk_util *du;
783 struct flist_head *entry;
784 struct cmd_du_pdu pdu;
785
786 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
787
Jens Axboe0766d922011-10-13 12:02:08 +0200788 memset(&pdu, 0, sizeof(pdu));
789
Jens Axboed09a64a2011-10-13 11:38:56 +0200790 flist_for_each(entry, &disk_list) {
791 du = flist_entry(entry, struct disk_util, list);
792
793 convert_dus(&pdu.dus, &du->dus);
794 convert_agg(&pdu.agg, &du->agg);
795
796 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
797 }
798}
799
Jens Axboe142575e2011-09-30 17:35:45 -0600800int fio_server_log(const char *format, ...)
801{
802 char buffer[1024];
803 va_list args;
Jens Axboe82fa6b22011-09-30 22:29:08 -0600804 size_t len;
Jens Axboe142575e2011-09-30 17:35:45 -0600805
Jens Axboe60efd142011-10-04 13:30:11 +0200806 dprint(FD_NET, "server log\n");
807
Jens Axboe142575e2011-09-30 17:35:45 -0600808 va_start(args, format);
Jens Axboe82fa6b22011-09-30 22:29:08 -0600809 len = vsnprintf(buffer, sizeof(buffer), format, args);
Jens Axboe142575e2011-09-30 17:35:45 -0600810 va_end(args);
811
Jens Axboe82fa6b22011-09-30 22:29:08 -0600812 return fio_server_text_output(buffer, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600813}
Jens Axboee46d8092011-10-03 09:11:02 +0200814
Jens Axboe87aa8f12011-10-06 21:24:13 +0200815static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200816{
Jens Axboe87aa8f12011-10-06 21:24:13 +0200817 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200818
819 sk = socket(AF_INET, SOCK_STREAM, 0);
820 if (sk < 0) {
821 log_err("fio: socket: %s\n", strerror(errno));
822 return -1;
823 }
824
825 opt = 1;
826 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
827 log_err("fio: setsockopt: %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#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200832 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200833 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200834 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200835 return -1;
836 }
837#endif
838
839 saddr_in.sin_family = AF_INET;
Jens Axboe81179ee2011-10-04 12:42:06 +0200840
841 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
842 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200843 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200844 return -1;
845 }
846
Jens Axboe87aa8f12011-10-06 21:24:13 +0200847 return sk;
848}
849
850static int fio_init_server_sock(void)
851{
852 struct sockaddr_un addr;
853 fio_socklen_t len;
854 mode_t mode;
855 int sk;
856
857 sk = socket(AF_UNIX, SOCK_STREAM, 0);
858 if (sk < 0) {
859 log_err("fio: socket: %s\n", strerror(errno));
860 return -1;
861 }
862
863 mode = umask(000);
864
865 memset(&addr, 0, sizeof(addr));
866 addr.sun_family = AF_UNIX;
867 strcpy(addr.sun_path, bind_sock);
868 unlink(bind_sock);
869
870 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
871
872 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
873 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200874 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200875 return -1;
876 }
877
878 umask(mode);
879 return sk;
880}
881
882static int fio_init_server_connection(void)
883{
Jens Axboebebe6392011-10-07 10:00:51 +0200884 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +0200885 int sk;
886
887 dprint(FD_NET, "starting server\n");
888
889 if (!bind_sock)
890 sk = fio_init_server_ip();
891 else
892 sk = fio_init_server_sock();
893
894 if (sk < 0)
895 return sk;
896
Jens Axboebebe6392011-10-07 10:00:51 +0200897 if (!bind_sock)
898 sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port);
899 else
900 strcpy(bind_str, bind_sock);
901
902 log_info("fio: server listening on %s\n", bind_str);
903
Jens Axboe89c17072011-10-11 10:15:51 +0200904 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200905 log_err("fio: listen: %s\n", strerror(errno));
906 return -1;
907 }
908
Jens Axboe87aa8f12011-10-06 21:24:13 +0200909 return sk;
910}
911
Jens Axboebebe6392011-10-07 10:00:51 +0200912int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
913 int *port, struct in_addr *inp)
914{
915 *ptr = NULL;
916 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +0200917 *port = fio_net_port;
Jens Axboebebe6392011-10-07 10:00:51 +0200918
919 if (!strncmp(str, "sock:", 5)) {
920 *ptr = strdup(str + 5);
921 *is_sock = 1;
922 } else {
923 const char *host = str;
924 char *portp;
925 int lport = 0;
926
927 /*
928 * Is it ip:<ip or host>:port
929 */
930 if (!strncmp(host, "ip:", 3))
931 host += 3;
932 else if (host[0] == ':') {
933 /* String is :port */
934 host++;
935 lport = atoi(host);
936 if (!lport || lport > 65535) {
937 log_err("fio: bad server port %u\n", port);
938 return 1;
939 }
940 /* no hostname given, we are done */
941 *port = lport;
942 return 0;
943 }
944
945 /*
946 * If no port seen yet, check if there's a last ':' at the end
947 */
948 if (!lport) {
949 portp = strchr(host, ':');
950 if (portp) {
951 *portp = '\0';
952 portp++;
953 lport = atoi(portp);
954 if (!lport || lport > 65535) {
955 log_err("fio: bad server port %u\n", port);
956 return 1;
957 }
958 }
959 }
960
961 if (lport)
962 *port = lport;
963
964 *ptr = strdup(host);
965
966 if (inet_aton(host, inp) != 1) {
967 struct hostent *hent;
968
969 hent = gethostbyname(host);
970 if (!hent) {
Jens Axboebebe6392011-10-07 10:00:51 +0200971 free(*ptr);
972 *ptr = NULL;
973 return 1;
974 }
975
976 memcpy(inp, hent->h_addr, 4);
977 }
978 }
979
980 if (*port == 0)
981 *port = fio_net_port;
982
983 return 0;
984}
985
Jens Axboe87aa8f12011-10-06 21:24:13 +0200986/*
987 * Server arg should be one of:
988 *
989 * sock:/path/to/socket
990 * ip:1.2.3.4
991 * 1.2.3.4
992 *
993 * Where sock uses unix domain sockets, and ip binds the server to
994 * a specific interface. If no arguments are given to the server, it
995 * uses IP and binds to 0.0.0.0.
996 *
997 */
998static int fio_handle_server_arg(void)
999{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001000 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001001 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001002
Jens Axboe87aa8f12011-10-06 21:24:13 +02001003 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1004
1005 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001006 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001007
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001008 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe6d2cf392011-10-07 13:19:28 +02001009 &port, &saddr_in.sin_addr);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001010
1011 if (!is_sock && bind_sock) {
1012 free(bind_sock);
1013 bind_sock = NULL;
1014 }
1015
Jens Axboea7de0a12011-10-07 12:55:14 +02001016out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001017 fio_net_port = port;
1018 saddr_in.sin_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001019 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001020}
1021
1022static int fio_server(void)
1023{
1024 int sk, ret;
1025
1026 dprint(FD_NET, "starting server\n");
1027
1028 if (fio_handle_server_arg())
1029 return -1;
1030
1031 sk = fio_init_server_connection();
1032 if (sk < 0)
1033 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001034
1035 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001036
Jens Axboe81179ee2011-10-04 12:42:06 +02001037 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001038
1039 if (fio_server_arg) {
1040 free(fio_server_arg);
1041 fio_server_arg = NULL;
1042 }
Jens Axboebebe6392011-10-07 10:00:51 +02001043 if (bind_sock)
1044 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001045
Jens Axboe81179ee2011-10-04 12:42:06 +02001046 return ret;
1047}
1048
Jens Axboe7b821682011-10-11 12:16:32 +02001049void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001050{
Jens Axboe7b821682011-10-11 12:16:32 +02001051 if (signal == SIGPIPE)
1052 server_fd = -1;
1053 else {
1054 log_info("\nfio: terminating on signal %d\n", signal);
1055 exit_backend = 1;
1056 }
Jens Axboe9abea482011-10-04 13:21:54 +02001057}
1058
Jens Axboe13755d92011-10-10 19:51:26 +02001059static int check_existing_pidfile(const char *pidfile)
1060{
1061 struct stat sb;
1062 char buf[16];
1063 pid_t pid;
1064 FILE *f;
1065
1066 if (stat(pidfile, &sb))
1067 return 0;
1068
1069 f = fopen(pidfile, "r");
1070 if (!f)
1071 return 0;
1072
Jens Axboebfc3b172011-10-10 21:16:55 +02001073 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001074 fclose(f);
1075 return 1;
1076 }
1077 fclose(f);
1078
1079 pid = atoi(buf);
1080 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001081 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001082
1083 return 1;
1084}
1085
1086static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001087{
1088 FILE *fpid;
1089
1090 fpid = fopen(pidfile, "w");
1091 if (!fpid) {
1092 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001093 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001094 }
1095
1096 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001097 fclose(fpid);
1098 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001099}
1100
1101/*
1102 * If pidfile is specified, background us.
1103 */
1104int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001105{
1106 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001107 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001108
Jens Axboe402668f2011-10-10 15:28:58 +02001109 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001110 return fio_server();
1111
Jens Axboe13755d92011-10-10 19:51:26 +02001112 if (check_existing_pidfile(pidfile)) {
1113 log_err("fio: pidfile %s exists and server appears alive\n",
1114 pidfile);
1115 return -1;
1116 }
1117
Jens Axboee46d8092011-10-03 09:11:02 +02001118 pid = fork();
1119 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001120 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001121 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001122 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001123 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001124 int ret = write_pid(pid, pidfile);
1125
1126 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001127 }
Jens Axboee46d8092011-10-03 09:11:02 +02001128
1129 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001130 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1131 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001132 close(STDIN_FILENO);
1133 close(STDOUT_FILENO);
1134 close(STDERR_FILENO);
1135 f_out = NULL;
1136 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001137
1138 ret = fio_server();
1139
1140 closelog();
1141 unlink(pidfile);
1142 free(pidfile);
1143 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001144}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001145
Jens Axboebebe6392011-10-07 10:00:51 +02001146void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001147{
1148 fio_server_arg = strdup(arg);
1149}