blob: 10851805a42bb87b95f35aa598fe398d76075ac1 [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
Stephen M. Cameron5adc2442012-02-24 08:17:31 +010027int fio_net_port = FIO_NET_PORT;
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 Axboeb9d2f302012-03-08 20:36:28 +010054 "SERVER_START",
Jens Axboe807f9972012-03-02 10:25:24 +010055 "ADD_JOB",
Jens Axboeb9d2f302012-03-08 20:36:28 +010056 "CMD_RUN"
Jens Axboe89c17072011-10-11 10:15:51 +020057};
58
59const char *fio_server_op(unsigned int op)
60{
61 static char buf[32];
62
63 if (op < FIO_NET_CMD_NR)
64 return fio_server_ops[op];
65
66 sprintf(buf, "UNKNOWN/%d", op);
67 return buf;
68}
69
Jens Axboe132159a2011-09-30 15:01:32 -060070int fio_send_data(int sk, const void *p, unsigned int len)
71{
Jens Axboeb9d2f302012-03-08 20:36:28 +010072 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
Jens Axboe794d69c2011-10-01 08:48:50 -060073
Jens Axboe132159a2011-09-30 15:01:32 -060074 do {
75 int ret = send(sk, p, len, 0);
76
77 if (ret > 0) {
78 len -= ret;
79 if (!len)
80 break;
81 p += ret;
82 continue;
83 } else if (!ret)
84 break;
85 else if (errno == EAGAIN || errno == EINTR)
86 continue;
Jens Axboe7b821682011-10-11 12:16:32 +020087 else
88 break;
Jens Axboe132159a2011-09-30 15:01:32 -060089 } while (!exit_backend);
90
91 if (!len)
92 return 0;
93
94 return 1;
95}
96
97int fio_recv_data(int sk, void *p, unsigned int len)
98{
99 do {
100 int ret = recv(sk, p, len, MSG_WAITALL);
101
102 if (ret > 0) {
103 len -= ret;
104 if (!len)
105 break;
106 p += ret;
107 continue;
108 } else if (!ret)
109 break;
110 else if (errno == EAGAIN || errno == EINTR)
111 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200112 else
113 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600114 } while (!exit_backend);
115
116 if (!len)
117 return 0;
118
119 return -1;
120}
121
122static int verify_convert_cmd(struct fio_net_cmd *cmd)
123{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600124 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600125
Jens Axboefcee5ff2011-09-30 22:50:39 -0600126 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
127 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600128
Jens Axboe25dfa842012-02-29 10:01:34 +0100129 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600130 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600131 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600132 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600133 return 1;
134 }
135
136 cmd->version = le16_to_cpu(cmd->version);
137 cmd->opcode = le16_to_cpu(cmd->opcode);
138 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200139 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600140 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
141
142 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200143 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600144 break;
145 default:
146 log_err("fio: bad server cmd version %d\n", cmd->version);
147 return 1;
148 }
149
Jens Axboeb9d2f302012-03-08 20:36:28 +0100150 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600151 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
152 return 1;
153 }
154
155 return 0;
156}
157
Jens Axboea64e88d2011-10-03 14:20:01 +0200158/*
159 * Read (and defragment, if necessary) incoming commands
160 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200161struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600162{
Jens Axboea64e88d2011-10-03 14:20:01 +0200163 struct fio_net_cmd cmd, *cmdret = NULL;
164 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600165 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200166 int ret, first = 1;
167 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600168
Jens Axboea64e88d2011-10-03 14:20:01 +0200169 do {
170 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
171 if (ret)
172 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600173
Jens Axboea64e88d2011-10-03 14:20:01 +0200174 /* We have a command, verify it and swap if need be */
175 ret = verify_convert_cmd(&cmd);
176 if (ret)
177 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600178
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200179 if (first) {
180 /* if this is text, add room for \0 at the end */
181 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
182 assert(!cmdret);
183 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200184 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600185
Jens Axboea64e88d2011-10-03 14:20:01 +0200186 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600187
Jens Axboea64e88d2011-10-03 14:20:01 +0200188 if (first)
189 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200190 else if (cmdret->opcode != cmd.opcode) {
191 log_err("fio: fragment opcode mismatch (%d != %d)\n",
192 cmdret->opcode, cmd.opcode);
193 ret = 1;
194 break;
195 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200196
197 if (!cmd.pdu_len)
198 break;
199
200 /* There's payload, get it */
201 pdu = (void *) cmdret->payload + pdu_offset;
202 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
203 if (ret)
204 break;
205
206 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100207 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200208 if (crc != cmd.pdu_crc16) {
209 log_err("fio: server bad crc on payload ");
210 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
211 ret = 1;
212 break;
213 }
214
215 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200216 if (!first)
217 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200218 first = 0;
219 } while (cmd.flags & FIO_NET_CMD_F_MORE);
220
221 if (ret) {
222 free(cmdret);
223 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200224 } else if (cmdret) {
225 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100226 if (cmdret->pdu_len) {
227 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
228 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
229 char *buf = (char *) pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200230
Jens Axboe084d1c62012-03-03 20:28:07 +0100231 buf[pdu->buf_len ] = '\0';
232 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
233 char *buf = (char *) cmdret->payload;
234
235 buf[cmdret->pdu_len ] = '\0';
236 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200237 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100238
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200239 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200240 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200241 }
Jens Axboe132159a2011-09-30 15:01:32 -0600242
Jens Axboea64e88d2011-10-03 14:20:01 +0200243 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600244}
245
246void fio_net_cmd_crc(struct fio_net_cmd *cmd)
247{
248 uint32_t pdu_len;
249
Jens Axboe25dfa842012-02-29 10:01:34 +0100250 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600251
252 pdu_len = le32_to_cpu(cmd->pdu_len);
253 if (pdu_len)
Jens Axboe25dfa842012-02-29 10:01:34 +0100254 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600255}
256
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200257int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
258 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600259{
Jens Axboe7f868312011-10-10 09:55:21 +0200260 struct fio_net_cmd *cmd = NULL;
261 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600262 int ret;
263
264 do {
265 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100266 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
267 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600268
Jens Axboe7f868312011-10-10 09:55:21 +0200269 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
270 if (cmd)
271 free(cmd);
272
273 cur_len = sizeof(*cmd) + this_len;
274 cmd = malloc(cur_len);
275 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600276
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200277 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600278
279 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200280 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600281
282 fio_net_cmd_crc(cmd);
283
284 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600285 size -= this_len;
286 buf += this_len;
287 } while (!ret && size);
288
Jens Axboe7f868312011-10-10 09:55:21 +0200289 if (cmd)
290 free(cmd);
291
Jens Axboe794d69c2011-10-01 08:48:50 -0600292 return ret;
293}
294
Jens Axboe89c17072011-10-11 10:15:51 +0200295static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600296{
Jens Axboe178cde92011-10-05 22:14:31 +0200297 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600298
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200299 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600300 fio_net_cmd_crc(&cmd);
301
302 return fio_send_data(sk, &cmd, sizeof(cmd));
303}
304
Jens Axboe89c17072011-10-11 10:15:51 +0200305/*
306 * If 'list' is non-NULL, then allocate and store the sent command for
307 * later verification.
308 */
309int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
310 struct flist_head *list)
311{
312 struct fio_net_int_cmd *cmd;
313 int ret;
314
315 if (!list)
316 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
317
318 cmd = malloc(sizeof(*cmd));
319
Jens Axboedf380932011-10-11 14:25:08 +0200320 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
Jens Axboe89c17072011-10-11 10:15:51 +0200321 fio_net_cmd_crc(&cmd->cmd);
322
323 INIT_FLIST_HEAD(&cmd->list);
324 gettimeofday(&cmd->tv, NULL);
325 cmd->saved_tag = tag;
326
327 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
328 if (ret) {
329 free(cmd);
330 return ret;
331 }
332
333 flist_add_tail(&cmd->list, list);
334 return 0;
335}
336
Jens Axboe9abea482011-10-04 13:21:54 +0200337static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600338{
Jens Axboe46c48f12011-10-01 12:50:51 -0600339 dprint(FD_NET, "server: sending quit\n");
Jens Axboe89c17072011-10-11 10:15:51 +0200340 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600341}
342
Jens Axboeb9d2f302012-03-08 20:36:28 +0100343static int handle_run_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600344{
Jens Axboe11e950b2011-10-16 21:34:14 +0200345 struct cmd_end_pdu epdu;
Jens Axboea64e88d2011-10-03 14:20:01 +0200346 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600347
Jens Axboe2e1df072012-02-09 11:15:02 +0100348 ret = fio_backend();
Jens Axboe11e950b2011-10-16 21:34:14 +0200349
350 epdu.error = ret;
351 fio_net_send_cmd(server_fd, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0);
352
Jens Axboe9abea482011-10-04 13:21:54 +0200353 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200354 reset_fio_state();
Jens Axboe8663ea62012-03-02 14:04:30 +0100355 first_cmd_check = 0;
Jens Axboe81179ee2011-10-04 12:42:06 +0200356 return ret;
357}
358
Jens Axboeb9d2f302012-03-08 20:36:28 +0100359static int handle_job_cmd(struct fio_net_cmd *cmd)
360{
361 char *buf = (char *) cmd->payload;
362 struct cmd_start_pdu spdu;
363
364 if (parse_jobs_ini(buf, 1, 0)) {
365 fio_server_send_quit_cmd();
366 return -1;
367 }
368
369 spdu.jobs = cpu_to_le32(thread_number);
370 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
371 return 0;
372}
373
Jens Axboe81179ee2011-10-04 12:42:06 +0200374static int handle_jobline_cmd(struct fio_net_cmd *cmd)
375{
Jens Axboefa2ea802011-10-08 21:07:29 +0200376 void *pdu = cmd->payload;
377 struct cmd_single_line_pdu *cslp;
378 struct cmd_line_pdu *clp;
379 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100380 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200381 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100382 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200383
Jens Axboefa2ea802011-10-08 21:07:29 +0200384 clp = pdu;
385 clp->lines = le16_to_cpu(clp->lines);
386 argv = malloc(clp->lines * sizeof(char *));
387 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200388
Jens Axboefa2ea802011-10-08 21:07:29 +0200389 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200390
Jens Axboefa2ea802011-10-08 21:07:29 +0200391 for (i = 0; i < clp->lines; i++) {
392 cslp = pdu + offset;
393 argv[i] = (char *) cslp->text;
394
395 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200396 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
397 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200398
Jens Axboefa2ea802011-10-08 21:07:29 +0200399 if (parse_cmd_line(clp->lines, argv)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200400 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200401 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200402 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200403 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200404
Jens Axboefa2ea802011-10-08 21:07:29 +0200405 free(argv);
406
Jens Axboeb9d2f302012-03-08 20:36:28 +0100407 spdu.jobs = cpu_to_le32(thread_number);
408 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
409 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600410}
411
Jens Axboec28e8e82011-10-04 08:57:39 +0200412static int handle_probe_cmd(struct fio_net_cmd *cmd)
413{
414 struct cmd_probe_pdu probe;
415
Jens Axboe89c17072011-10-11 10:15:51 +0200416 dprint(FD_NET, "server: sending probe reply\n");
417
Jens Axboec28e8e82011-10-04 08:57:39 +0200418 memset(&probe, 0, sizeof(probe));
419 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200420#ifdef FIO_BIG_ENDIAN
421 probe.bigendian = 1;
422#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200423 probe.fio_major = FIO_MAJOR;
424 probe.fio_minor = FIO_MINOR;
425 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200426
Jens Axboecca84642011-10-07 12:47:57 +0200427 probe.os = FIO_OS;
428 probe.arch = FIO_ARCH;
429
Jens Axboe38fdef22011-10-11 14:30:06 +0200430 probe.bpp = sizeof(void *);
431
Jens Axboe89c17072011-10-11 10:15:51 +0200432 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200433}
434
435static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
436{
437 struct jobs_eta *je;
438 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200439 int i;
440
Jens Axboeb814fb22011-10-12 09:20:34 +0200441 if (!thread_number)
442 return 0;
443
444 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200445 je = malloc(size);
446 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200447
448 if (!calc_thread_status(je, 1)) {
449 free(je);
450 return 0;
451 }
452
453 dprint(FD_NET, "server sending status\n");
454
455 je->nr_running = cpu_to_le32(je->nr_running);
456 je->nr_ramp = cpu_to_le32(je->nr_ramp);
457 je->nr_pending = cpu_to_le32(je->nr_pending);
458 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200459
460 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100461 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
462 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
463 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
464 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200465 je->rate[i] = cpu_to_le32(je->rate[i]);
466 je->iops[i] = cpu_to_le32(je->iops[i]);
467 }
468
Jens Axboec1970b92012-03-06 15:37:40 +0100469 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200470 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100471 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200472
Jens Axboe7f868312011-10-10 09:55:21 +0200473 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200474 free(je);
475 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200476}
477
Jens Axboe132159a2011-09-30 15:01:32 -0600478static int handle_command(struct fio_net_cmd *cmd)
479{
480 int ret;
481
Jens Axboe89c17072011-10-11 10:15:51 +0200482 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
483 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600484
Jens Axboe132159a2011-09-30 15:01:32 -0600485 switch (cmd->opcode) {
486 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200487 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200488 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200489 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600490 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200491 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600492 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200493 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600494 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200495 case FIO_NET_CMD_JOBLINE:
496 ret = handle_jobline_cmd(cmd);
497 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200498 case FIO_NET_CMD_PROBE:
499 ret = handle_probe_cmd(cmd);
500 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200501 case FIO_NET_CMD_SEND_ETA:
502 ret = handle_send_eta_cmd(cmd);
503 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100504 case FIO_NET_CMD_RUN:
505 ret = handle_run_cmd(cmd);
506 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600507 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200508 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600509 ret = 1;
510 }
511
512 return ret;
513}
514
Jens Axboe70e0c312011-10-04 09:18:30 +0200515static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600516{
517 struct fio_net_cmd *cmd = NULL;
518 int ret = 0;
519
520 /* read forever */
521 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200522 struct pollfd pfd = {
523 .fd = sk,
524 .events = POLLIN,
525 };
526
527 ret = 0;
528 do {
529 ret = poll(&pfd, 1, 100);
530 if (ret < 0) {
531 if (errno == EINTR)
532 break;
533 log_err("fio: poll: %s\n", strerror(errno));
534 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200535 } else if (!ret) {
536 if (!block)
537 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200538 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200539 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200540
541 if (pfd.revents & POLLIN)
542 break;
543 if (pfd.revents & (POLLERR|POLLHUP)) {
544 ret = 1;
545 break;
546 }
Jens Axboe19c65172011-10-05 22:05:37 +0200547 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200548
549 if (ret < 0)
550 break;
551
552 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600553 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200554 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600555 break;
556 }
557
Jens Axboe132159a2011-09-30 15:01:32 -0600558 ret = handle_command(cmd);
559 if (ret)
560 break;
561
562 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400563 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600564 }
565
566 if (cmd)
567 free(cmd);
568
569 return ret;
570}
571
Jens Axboecc0df002011-10-03 20:53:32 +0200572void fio_server_idle_loop(void)
573{
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100574 if (!first_cmd_check) {
Jens Axboe5d7793a2012-03-08 19:59:16 +0100575 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100576 first_cmd_check = 1;
577 }
Jens Axboecc0df002011-10-03 20:53:32 +0200578 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200579 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200580}
581
Jens Axboe50d16972011-09-29 17:45:28 -0600582static int accept_loop(int listen_sk)
583{
Jens Axboebb447a22011-10-04 15:06:42 +0200584 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200585 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600586 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600587 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600588
Jens Axboe60efd142011-10-04 13:30:11 +0200589 dprint(FD_NET, "server enter accept loop\n");
590
Jens Axboe009b1be2011-09-29 18:27:02 -0600591 flags = fcntl(listen_sk, F_GETFL);
592 flags |= O_NONBLOCK;
593 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600594again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600595 pfd.fd = listen_sk;
596 pfd.events = POLLIN;
597 do {
598 ret = poll(&pfd, 1, 100);
599 if (ret < 0) {
600 if (errno == EINTR)
601 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600602 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600603 goto out;
604 } else if (!ret)
605 continue;
606
607 if (pfd.revents & POLLIN)
608 break;
609 } while (!exit_backend);
610
611 if (exit_backend)
612 goto out;
613
Jens Axboe6b976bf2011-10-04 15:07:43 +0200614 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600615 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600616 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600617 return -1;
618 }
619
Jens Axboebb447a22011-10-04 15:06:42 +0200620 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600621
Jens Axboe37db14f2011-09-30 17:00:42 -0600622 server_fd = sk;
623
Jens Axboe70e0c312011-10-04 09:18:30 +0200624 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600625
Jens Axboe37db14f2011-09-30 17:00:42 -0600626 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600627 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600628
Jens Axboe009b1be2011-09-29 18:27:02 -0600629 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600630 goto again;
631
Jens Axboe009b1be2011-09-29 18:27:02 -0600632out:
Jens Axboe132159a2011-09-30 15:01:32 -0600633 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600634}
635
Jens Axboe084d1c62012-03-03 20:28:07 +0100636int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600637{
Jens Axboe084d1c62012-03-03 20:28:07 +0100638 struct cmd_text_pdu *pdu;
639 unsigned int tlen;
640 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600641
Jens Axboe084d1c62012-03-03 20:28:07 +0100642 if (server_fd == -1)
643 return log_local_buf(buf, len);
644
645 tlen = sizeof(*pdu) + len;
646 pdu = malloc(tlen);
647
648 pdu->level = __cpu_to_le32(level);
649 pdu->buf_len = __cpu_to_le32(len);
650
651 gettimeofday(&tv, NULL);
652 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
653 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
654
655 memcpy(pdu->buf, buf, len);
656
657 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, 0);
658 free(pdu);
659 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600660}
661
Jens Axboea64e88d2011-10-03 14:20:01 +0200662static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
663{
664 dst->max_val = cpu_to_le64(src->max_val);
665 dst->min_val = cpu_to_le64(src->min_val);
666 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200667
668 /*
669 * Encode to IEEE 754 for network transfer
670 */
671 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
672 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200673}
674
675static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
676{
677 int i;
678
679 for (i = 0; i < 2; i++) {
680 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
681 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
682 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
683 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
684 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
685 dst->agg[i] = cpu_to_le64(src->agg[i]);
686 }
687
688 dst->kb_base = cpu_to_le32(src->kb_base);
689 dst->groupid = cpu_to_le32(src->groupid);
690}
691
692/*
693 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
694 * into a single payload.
695 */
696void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
697{
698 struct cmd_ts_pdu p;
699 int i, j;
700
Jens Axboe60efd142011-10-04 13:30:11 +0200701 dprint(FD_NET, "server sending end stats\n");
702
Jens Axboe317b3c82011-10-03 21:47:27 +0200703 memset(&p, 0, sizeof(p));
704
Jens Axboea64e88d2011-10-03 14:20:01 +0200705 strcpy(p.ts.name, ts->name);
706 strcpy(p.ts.verror, ts->verror);
707 strcpy(p.ts.description, ts->description);
708
Jens Axboeddcc0b62011-10-03 14:45:27 +0200709 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200710 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200711 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200712 p.ts.members = cpu_to_le32(ts->members);
713
714 for (i = 0; i < 2; i++) {
715 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
716 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
717 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
718 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
719 }
720
721 p.ts.usr_time = cpu_to_le64(ts->usr_time);
722 p.ts.sys_time = cpu_to_le64(ts->sys_time);
723 p.ts.ctx = cpu_to_le64(ts->ctx);
724 p.ts.minf = cpu_to_le64(ts->minf);
725 p.ts.majf = cpu_to_le64(ts->majf);
726 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200727
728 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200729 fio_fp64_t *src = &ts->percentile_list[i];
730 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200731
Jens Axboecfc03e42011-10-12 21:20:42 +0200732 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200733 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200734
735 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
736 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
737 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
738 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
739 }
740
741 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
742 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
743 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
744 }
745
746 for (i = 0; i < 2; i++)
747 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
748 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
749
750 for (i = 0; i < 3; i++) {
751 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200752 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200753 }
754
Jens Axboe93eee042011-10-03 21:08:48 +0200755 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200756 p.ts.total_complete = cpu_to_le64(ts->total_complete);
757
758 for (i = 0; i < 2; i++) {
759 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
760 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
761 }
762
763 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
764 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
765 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200766 p.ts.first_error = cpu_to_le32(ts->first_error);
767 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200768
769 convert_gs(&p.rs, rs);
770
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200771 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200772}
773
774void fio_server_send_gs(struct group_run_stats *rs)
775{
776 struct group_run_stats gs;
777
Jens Axboe60efd142011-10-04 13:30:11 +0200778 dprint(FD_NET, "server sending group run stats\n");
779
Jens Axboea64e88d2011-10-03 14:20:01 +0200780 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200781 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200782}
783
Jens Axboed09a64a2011-10-13 11:38:56 +0200784static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
785{
786 int i;
787
788 for (i = 0; i < 2; i++) {
789 dst->ios[i] = cpu_to_le32(src->ios[i]);
790 dst->merges[i] = cpu_to_le32(src->merges[i]);
791 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
792 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
793 }
794
795 dst->io_ticks = cpu_to_le32(src->io_ticks);
796 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
797 dst->slavecount = cpu_to_le32(src->slavecount);
798 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
799}
800
801static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
802{
803 int i;
804
805 strcpy((char *) dst->name, (char *) src->name);
806
807 for (i = 0; i < 2; i++) {
808 dst->ios[i] = cpu_to_le32(src->ios[i]);
809 dst->merges[i] = cpu_to_le32(src->merges[i]);
810 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
811 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
812 }
813
814 dst->io_ticks = cpu_to_le32(src->io_ticks);
815 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
816 dst->msec = cpu_to_le64(src->msec);
817}
818
819void fio_server_send_du(void)
820{
821 struct disk_util *du;
822 struct flist_head *entry;
823 struct cmd_du_pdu pdu;
824
825 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
826
Jens Axboe0766d922011-10-13 12:02:08 +0200827 memset(&pdu, 0, sizeof(pdu));
828
Jens Axboed09a64a2011-10-13 11:38:56 +0200829 flist_for_each(entry, &disk_list) {
830 du = flist_entry(entry, struct disk_util, list);
831
832 convert_dus(&pdu.dus, &du->dus);
833 convert_agg(&pdu.agg, &du->agg);
834
835 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
836 }
837}
838
Jens Axboe807f9972012-03-02 10:25:24 +0100839void fio_server_send_add_job(struct thread_options *o, const char *ioengine)
840{
841 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +0100842
Jens Axboedcaeb602012-03-08 19:45:37 +0100843 convert_thread_options_to_net(&pdu.top, o);
Jens Axboe807f9972012-03-02 10:25:24 +0100844
845 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), 0);
846}
847
Jens Axboe87aa8f12011-10-06 21:24:13 +0200848static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200849{
Jens Axboe811826b2011-10-24 09:11:50 +0200850 struct sockaddr *addr;
851 fio_socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200852 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200853
Jens Axboe811826b2011-10-24 09:11:50 +0200854 if (use_ipv6)
855 sk = socket(AF_INET6, SOCK_STREAM, 0);
856 else
857 sk = socket(AF_INET, SOCK_STREAM, 0);
858
Jens Axboe81179ee2011-10-04 12:42:06 +0200859 if (sk < 0) {
860 log_err("fio: socket: %s\n", strerror(errno));
861 return -1;
862 }
863
864 opt = 1;
865 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
866 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200867 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200868 return -1;
869 }
870#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200871 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200872 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200873 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200874 return -1;
875 }
876#endif
877
Jens Axboe811826b2011-10-24 09:11:50 +0200878 if (use_ipv6) {
879 addr = (struct sockaddr *) &saddr_in6;
880 socklen = sizeof(saddr_in6);
881 saddr_in6.sin6_family = AF_INET6;
882 } else {
883 addr = (struct sockaddr *) &saddr_in;
884 socklen = sizeof(saddr_in);
885 saddr_in.sin_family = AF_INET;
886 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200887
Jens Axboe811826b2011-10-24 09:11:50 +0200888 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200889 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200890 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200891 return -1;
892 }
893
Jens Axboe87aa8f12011-10-06 21:24:13 +0200894 return sk;
895}
896
897static int fio_init_server_sock(void)
898{
899 struct sockaddr_un addr;
900 fio_socklen_t len;
901 mode_t mode;
902 int sk;
903
904 sk = socket(AF_UNIX, SOCK_STREAM, 0);
905 if (sk < 0) {
906 log_err("fio: socket: %s\n", strerror(errno));
907 return -1;
908 }
909
910 mode = umask(000);
911
912 memset(&addr, 0, sizeof(addr));
913 addr.sun_family = AF_UNIX;
914 strcpy(addr.sun_path, bind_sock);
915 unlink(bind_sock);
916
917 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
918
919 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
920 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200921 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200922 return -1;
923 }
924
925 umask(mode);
926 return sk;
927}
928
929static int fio_init_server_connection(void)
930{
Jens Axboebebe6392011-10-07 10:00:51 +0200931 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +0200932 int sk;
933
934 dprint(FD_NET, "starting server\n");
935
936 if (!bind_sock)
937 sk = fio_init_server_ip();
938 else
939 sk = fio_init_server_sock();
940
941 if (sk < 0)
942 return sk;
943
Jens Axboe811826b2011-10-24 09:11:50 +0200944 if (!bind_sock) {
945 char *p, port[16];
946 const void *src;
947 int af;
948
949 if (use_ipv6) {
950 af = AF_INET6;
951 src = &saddr_in6.sin6_addr;
952 } else {
953 af = AF_INET;
954 src = &saddr_in.sin_addr;
955 }
956
957 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
958
959 sprintf(port, ",%u", fio_net_port);
960 if (p)
961 strcat(p, port);
962 else
963 strcpy(bind_str, port);
964 } else
Jens Axboebebe6392011-10-07 10:00:51 +0200965 strcpy(bind_str, bind_sock);
966
967 log_info("fio: server listening on %s\n", bind_str);
968
Jens Axboe89c17072011-10-11 10:15:51 +0200969 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200970 log_err("fio: listen: %s\n", strerror(errno));
971 return -1;
972 }
973
Jens Axboe87aa8f12011-10-06 21:24:13 +0200974 return sk;
975}
976
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100977int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
978 struct in6_addr *inp6)
979
980{
981 int ret = 0;
982
983 if (*ipv6)
984 ret = inet_pton(AF_INET6, host, inp6);
985 else
986 ret = inet_pton(AF_INET, host, inp);
987
988 if (ret != 1) {
989 struct hostent *hent;
990
991 hent = gethostbyname(host);
992 if (!hent) {
993 log_err("fio: failed to resolve <%s>\n", host);
994 return 0;
995 }
996
997 if (*ipv6) {
998 if (hent->h_addrtype != AF_INET6) {
999 log_info("fio: falling back to IPv4\n");
1000 *ipv6 = 0;
1001 } else
1002 memcpy(inp6, hent->h_addr_list[0], 16);
1003 }
1004 if (!*ipv6) {
1005 if (hent->h_addrtype != AF_INET) {
1006 log_err("fio: lookup type mismatch\n");
1007 return 0;
1008 }
1009 memcpy(inp, hent->h_addr_list[0], 4);
1010 }
1011 ret = 1;
1012 }
1013
1014 return !(ret == 1);
1015}
1016
Jens Axboe660a2bf2011-10-24 09:35:06 +02001017/*
1018 * Parse a host/ip/port string. Reads from 'str'.
1019 *
1020 * Outputs:
1021 *
1022 * For IPv4:
1023 * *ptr is the host, *port is the port, inp is the destination.
1024 * For IPv6:
1025 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1026 * For local domain sockets:
1027 * *ptr is the filename, *is_sock is 1.
1028 */
Jens Axboebebe6392011-10-07 10:00:51 +02001029int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001030 int *port, struct in_addr *inp,
1031 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001032{
Jens Axboe76867622011-10-25 09:52:51 +02001033 const char *host = str;
1034 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001035 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001036
Jens Axboebebe6392011-10-07 10:00:51 +02001037 *ptr = NULL;
1038 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001039 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001040 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001041
1042 if (!strncmp(str, "sock:", 5)) {
1043 *ptr = strdup(str + 5);
1044 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001045
Jens Axboe76867622011-10-25 09:52:51 +02001046 return 0;
1047 }
1048
1049 /*
1050 * Is it ip:<ip or host>:port
1051 */
1052 if (!strncmp(host, "ip:", 3))
1053 host += 3;
1054 else if (!strncmp(host, "ip4:", 4))
1055 host += 4;
1056 else if (!strncmp(host, "ip6:", 4)) {
1057 host += 4;
1058 *ipv6 = 1;
1059 } else if (host[0] == ':') {
1060 /* String is :port */
1061 host++;
1062 lport = atoi(host);
1063 if (!lport || lport > 65535) {
1064 log_err("fio: bad server port %u\n", port);
1065 return 1;
1066 }
1067 /* no hostname given, we are done */
1068 *port = lport;
1069 return 0;
1070 }
1071
1072 /*
1073 * If no port seen yet, check if there's a last ':' at the end
1074 */
1075 if (!lport) {
1076 portp = strchr(host, ',');
1077 if (portp) {
1078 *portp = '\0';
1079 portp++;
1080 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001081 if (!lport || lport > 65535) {
1082 log_err("fio: bad server port %u\n", port);
1083 return 1;
1084 }
Jens Axboe76867622011-10-25 09:52:51 +02001085 }
1086 }
1087
1088 if (lport)
1089 *port = lport;
1090
1091 if (!strlen(host))
1092 return 0;
1093
1094 *ptr = strdup(host);
1095
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001096 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1097 free(*ptr);
1098 *ptr = NULL;
1099 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001100 }
1101
1102 if (*port == 0)
1103 *port = fio_net_port;
1104
1105 return 0;
1106}
1107
Jens Axboe87aa8f12011-10-06 21:24:13 +02001108/*
1109 * Server arg should be one of:
1110 *
1111 * sock:/path/to/socket
1112 * ip:1.2.3.4
1113 * 1.2.3.4
1114 *
1115 * Where sock uses unix domain sockets, and ip binds the server to
1116 * a specific interface. If no arguments are given to the server, it
1117 * uses IP and binds to 0.0.0.0.
1118 *
1119 */
1120static int fio_handle_server_arg(void)
1121{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001122 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001123 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001124
Jens Axboe87aa8f12011-10-06 21:24:13 +02001125 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1126
1127 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001128 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001129
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001130 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001131 &port, &saddr_in.sin_addr,
1132 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001133
1134 if (!is_sock && bind_sock) {
1135 free(bind_sock);
1136 bind_sock = NULL;
1137 }
1138
Jens Axboea7de0a12011-10-07 12:55:14 +02001139out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001140 fio_net_port = port;
1141 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001142 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001143 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001144}
1145
1146static int fio_server(void)
1147{
1148 int sk, ret;
1149
1150 dprint(FD_NET, "starting server\n");
1151
1152 if (fio_handle_server_arg())
1153 return -1;
1154
1155 sk = fio_init_server_connection();
1156 if (sk < 0)
1157 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001158
1159 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001160
Jens Axboe81179ee2011-10-04 12:42:06 +02001161 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001162
1163 if (fio_server_arg) {
1164 free(fio_server_arg);
1165 fio_server_arg = NULL;
1166 }
Jens Axboebebe6392011-10-07 10:00:51 +02001167 if (bind_sock)
1168 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001169
Jens Axboe81179ee2011-10-04 12:42:06 +02001170 return ret;
1171}
1172
Jens Axboe7b821682011-10-11 12:16:32 +02001173void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001174{
Jens Axboe7b821682011-10-11 12:16:32 +02001175 if (signal == SIGPIPE)
1176 server_fd = -1;
1177 else {
1178 log_info("\nfio: terminating on signal %d\n", signal);
1179 exit_backend = 1;
1180 }
Jens Axboe9abea482011-10-04 13:21:54 +02001181}
1182
Jens Axboe13755d92011-10-10 19:51:26 +02001183static int check_existing_pidfile(const char *pidfile)
1184{
1185 struct stat sb;
1186 char buf[16];
1187 pid_t pid;
1188 FILE *f;
1189
1190 if (stat(pidfile, &sb))
1191 return 0;
1192
1193 f = fopen(pidfile, "r");
1194 if (!f)
1195 return 0;
1196
Jens Axboebfc3b172011-10-10 21:16:55 +02001197 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001198 fclose(f);
1199 return 1;
1200 }
1201 fclose(f);
1202
1203 pid = atoi(buf);
1204 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001205 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001206
1207 return 1;
1208}
1209
1210static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001211{
1212 FILE *fpid;
1213
1214 fpid = fopen(pidfile, "w");
1215 if (!fpid) {
1216 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001217 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001218 }
1219
1220 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001221 fclose(fpid);
1222 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001223}
1224
1225/*
1226 * If pidfile is specified, background us.
1227 */
1228int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001229{
1230 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001231 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001232
Bruce Cran93bcfd22012-02-20 20:18:19 +01001233#if defined(WIN32)
1234 WSADATA wsd;
1235 WSAStartup(MAKEWORD(2,2), &wsd);
1236#endif
1237
Jens Axboe402668f2011-10-10 15:28:58 +02001238 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001239 return fio_server();
1240
Jens Axboe13755d92011-10-10 19:51:26 +02001241 if (check_existing_pidfile(pidfile)) {
1242 log_err("fio: pidfile %s exists and server appears alive\n",
1243 pidfile);
1244 return -1;
1245 }
1246
Jens Axboee46d8092011-10-03 09:11:02 +02001247 pid = fork();
1248 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001249 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001250 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001251 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001252 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001253 int ret = write_pid(pid, pidfile);
1254
1255 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001256 }
Jens Axboee46d8092011-10-03 09:11:02 +02001257
1258 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001259 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1260 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001261 close(STDIN_FILENO);
1262 close(STDOUT_FILENO);
1263 close(STDERR_FILENO);
1264 f_out = NULL;
1265 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001266
1267 ret = fio_server();
1268
1269 closelog();
1270 unlink(pidfile);
1271 free(pidfile);
1272 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001273}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001274
Jens Axboebebe6392011-10-07 10:00:51 +02001275void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001276{
1277 fio_server_arg = strdup(arg);
1278}