blob: 89422e96781dae80cd0fad5ec2ed473691820376 [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
Jens Axboe8b4e61b2012-03-09 17:10:54 +010094 if (errno)
95 return -errno;
96
Jens Axboe132159a2011-09-30 15:01:32 -060097 return 1;
98}
99
100int fio_recv_data(int sk, void *p, unsigned int len)
101{
102 do {
103 int ret = recv(sk, p, len, MSG_WAITALL);
104
105 if (ret > 0) {
106 len -= ret;
107 if (!len)
108 break;
109 p += ret;
110 continue;
111 } else if (!ret)
112 break;
113 else if (errno == EAGAIN || errno == EINTR)
114 continue;
Jens Axboe7b821682011-10-11 12:16:32 +0200115 else
116 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600117 } while (!exit_backend);
118
119 if (!len)
120 return 0;
121
122 return -1;
123}
124
125static int verify_convert_cmd(struct fio_net_cmd *cmd)
126{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600127 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600128
Jens Axboefcee5ff2011-09-30 22:50:39 -0600129 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
130 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600131
Jens Axboe25dfa842012-02-29 10:01:34 +0100132 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600133 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600134 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600135 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600136 return 1;
137 }
138
139 cmd->version = le16_to_cpu(cmd->version);
140 cmd->opcode = le16_to_cpu(cmd->opcode);
141 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200142 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600143 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
144
145 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200146 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600147 break;
148 default:
149 log_err("fio: bad server cmd version %d\n", cmd->version);
150 return 1;
151 }
152
Jens Axboeb9d2f302012-03-08 20:36:28 +0100153 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
Jens Axboe132159a2011-09-30 15:01:32 -0600154 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
155 return 1;
156 }
157
158 return 0;
159}
160
Jens Axboea64e88d2011-10-03 14:20:01 +0200161/*
162 * Read (and defragment, if necessary) incoming commands
163 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200164struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600165{
Jens Axboea64e88d2011-10-03 14:20:01 +0200166 struct fio_net_cmd cmd, *cmdret = NULL;
167 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600168 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200169 int ret, first = 1;
170 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600171
Jens Axboea64e88d2011-10-03 14:20:01 +0200172 do {
173 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
174 if (ret)
175 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600176
Jens Axboea64e88d2011-10-03 14:20:01 +0200177 /* We have a command, verify it and swap if need be */
178 ret = verify_convert_cmd(&cmd);
179 if (ret)
180 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600181
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200182 if (first) {
183 /* if this is text, add room for \0 at the end */
184 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
185 assert(!cmdret);
186 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200187 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600188
Jens Axboea64e88d2011-10-03 14:20:01 +0200189 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600190
Jens Axboea64e88d2011-10-03 14:20:01 +0200191 if (first)
192 memcpy(cmdret, &cmd, sizeof(cmd));
Jens Axboe67f15dc2011-10-15 16:07:40 +0200193 else if (cmdret->opcode != cmd.opcode) {
194 log_err("fio: fragment opcode mismatch (%d != %d)\n",
195 cmdret->opcode, cmd.opcode);
196 ret = 1;
197 break;
198 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200199
200 if (!cmd.pdu_len)
201 break;
202
203 /* There's payload, get it */
204 pdu = (void *) cmdret->payload + pdu_offset;
205 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
206 if (ret)
207 break;
208
209 /* Verify payload crc */
Jens Axboe25dfa842012-02-29 10:01:34 +0100210 crc = fio_crc16(pdu, cmd.pdu_len);
Jens Axboea64e88d2011-10-03 14:20:01 +0200211 if (crc != cmd.pdu_crc16) {
212 log_err("fio: server bad crc on payload ");
213 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
214 ret = 1;
215 break;
216 }
217
218 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200219 if (!first)
220 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200221 first = 0;
222 } while (cmd.flags & FIO_NET_CMD_F_MORE);
223
224 if (ret) {
225 free(cmdret);
226 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200227 } else if (cmdret) {
228 /* zero-terminate text input */
Jens Axboe084d1c62012-03-03 20:28:07 +0100229 if (cmdret->pdu_len) {
230 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
231 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
232 char *buf = (char *) pdu->buf;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200233
Jens Axboe084d1c62012-03-03 20:28:07 +0100234 buf[pdu->buf_len ] = '\0';
235 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
Jens Axboe46bcd492012-03-14 11:31:21 +0100236 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
237 char *buf = (char *) pdu->buf;
238 int len = le32_to_cpu(pdu->buf_len);
Jens Axboe084d1c62012-03-03 20:28:07 +0100239
Jens Axboe46bcd492012-03-14 11:31:21 +0100240 buf[len] = '\0';
Jens Axboe084d1c62012-03-03 20:28:07 +0100241 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200242 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100243
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200244 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200245 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200246 }
Jens Axboe132159a2011-09-30 15:01:32 -0600247
Jens Axboea64e88d2011-10-03 14:20:01 +0200248 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600249}
250
251void fio_net_cmd_crc(struct fio_net_cmd *cmd)
252{
253 uint32_t pdu_len;
254
Jens Axboe25dfa842012-02-29 10:01:34 +0100255 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600256
257 pdu_len = le32_to_cpu(cmd->pdu_len);
258 if (pdu_len)
Jens Axboe25dfa842012-02-29 10:01:34 +0100259 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600260}
261
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200262int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
263 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600264{
Jens Axboe7f868312011-10-10 09:55:21 +0200265 struct fio_net_cmd *cmd = NULL;
266 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600267 int ret;
268
269 do {
270 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100271 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
272 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600273
Jens Axboe7f868312011-10-10 09:55:21 +0200274 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
275 if (cmd)
276 free(cmd);
277
278 cur_len = sizeof(*cmd) + this_len;
279 cmd = malloc(cur_len);
280 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600281
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200282 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600283
284 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200285 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600286
287 fio_net_cmd_crc(cmd);
288
289 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600290 size -= this_len;
291 buf += this_len;
292 } while (!ret && size);
293
Jens Axboe7f868312011-10-10 09:55:21 +0200294 if (cmd)
295 free(cmd);
296
Jens Axboe794d69c2011-10-01 08:48:50 -0600297 return ret;
298}
299
Jens Axboe89c17072011-10-11 10:15:51 +0200300static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600301{
Jens Axboe178cde92011-10-05 22:14:31 +0200302 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600303
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200304 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600305 fio_net_cmd_crc(&cmd);
306
307 return fio_send_data(sk, &cmd, sizeof(cmd));
308}
309
Jens Axboe89c17072011-10-11 10:15:51 +0200310/*
311 * If 'list' is non-NULL, then allocate and store the sent command for
312 * later verification.
313 */
314int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
315 struct flist_head *list)
316{
317 struct fio_net_int_cmd *cmd;
318 int ret;
319
320 if (!list)
321 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
322
323 cmd = malloc(sizeof(*cmd));
324
Jens Axboedf380932011-10-11 14:25:08 +0200325 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
Jens Axboe89c17072011-10-11 10:15:51 +0200326 fio_net_cmd_crc(&cmd->cmd);
327
328 INIT_FLIST_HEAD(&cmd->list);
329 gettimeofday(&cmd->tv, NULL);
330 cmd->saved_tag = tag;
331
332 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
333 if (ret) {
334 free(cmd);
335 return ret;
336 }
337
338 flist_add_tail(&cmd->list, list);
339 return 0;
340}
341
Jens Axboe9abea482011-10-04 13:21:54 +0200342static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600343{
Jens Axboe46c48f12011-10-01 12:50:51 -0600344 dprint(FD_NET, "server: sending quit\n");
Jens Axboe89c17072011-10-11 10:15:51 +0200345 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600346}
347
Jens Axboeb9d2f302012-03-08 20:36:28 +0100348static int handle_run_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600349{
Jens Axboe11e950b2011-10-16 21:34:14 +0200350 struct cmd_end_pdu epdu;
Jens Axboea64e88d2011-10-03 14:20:01 +0200351 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600352
Jens Axboe2e1df072012-02-09 11:15:02 +0100353 ret = fio_backend();
Jens Axboe11e950b2011-10-16 21:34:14 +0200354
355 epdu.error = ret;
356 fio_net_send_cmd(server_fd, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0);
357
Jens Axboe9abea482011-10-04 13:21:54 +0200358 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200359 reset_fio_state();
Jens Axboe8663ea62012-03-02 14:04:30 +0100360 first_cmd_check = 0;
Jens Axboe81179ee2011-10-04 12:42:06 +0200361 return ret;
362}
363
Jens Axboeb9d2f302012-03-08 20:36:28 +0100364static int handle_job_cmd(struct fio_net_cmd *cmd)
365{
Jens Axboe46bcd492012-03-14 11:31:21 +0100366 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
367 void *buf = pdu->buf;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100368 struct cmd_start_pdu spdu;
369
Jens Axboe46bcd492012-03-14 11:31:21 +0100370 pdu->buf_len = le32_to_cpu(pdu->buf_len);
371 pdu->client_type = le32_to_cpu(pdu->client_type);
372
373 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
Jens Axboeb9d2f302012-03-08 20:36:28 +0100374 fio_server_send_quit_cmd();
375 return -1;
376 }
377
378 spdu.jobs = cpu_to_le32(thread_number);
379 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
380 return 0;
381}
382
Jens Axboe81179ee2011-10-04 12:42:06 +0200383static int handle_jobline_cmd(struct fio_net_cmd *cmd)
384{
Jens Axboefa2ea802011-10-08 21:07:29 +0200385 void *pdu = cmd->payload;
386 struct cmd_single_line_pdu *cslp;
387 struct cmd_line_pdu *clp;
388 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100389 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200390 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100391 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200392
Jens Axboefa2ea802011-10-08 21:07:29 +0200393 clp = pdu;
394 clp->lines = le16_to_cpu(clp->lines);
Jens Axboe46bcd492012-03-14 11:31:21 +0100395 clp->client_type = le16_to_cpu(clp->client_type);
Jens Axboefa2ea802011-10-08 21:07:29 +0200396 argv = malloc(clp->lines * sizeof(char *));
397 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200398
Jens Axboefa2ea802011-10-08 21:07:29 +0200399 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200400
Jens Axboefa2ea802011-10-08 21:07:29 +0200401 for (i = 0; i < clp->lines; i++) {
402 cslp = pdu + offset;
403 argv[i] = (char *) cslp->text;
404
405 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200406 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
407 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200408
Jens Axboe46bcd492012-03-14 11:31:21 +0100409 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200410 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200411 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200412 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200413 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200414
Jens Axboefa2ea802011-10-08 21:07:29 +0200415 free(argv);
416
Jens Axboeb9d2f302012-03-08 20:36:28 +0100417 spdu.jobs = cpu_to_le32(thread_number);
418 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
419 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600420}
421
Jens Axboec28e8e82011-10-04 08:57:39 +0200422static int handle_probe_cmd(struct fio_net_cmd *cmd)
423{
424 struct cmd_probe_pdu probe;
425
Jens Axboe89c17072011-10-11 10:15:51 +0200426 dprint(FD_NET, "server: sending probe reply\n");
427
Jens Axboec28e8e82011-10-04 08:57:39 +0200428 memset(&probe, 0, sizeof(probe));
429 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200430#ifdef FIO_BIG_ENDIAN
431 probe.bigendian = 1;
432#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200433 probe.fio_major = FIO_MAJOR;
434 probe.fio_minor = FIO_MINOR;
435 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200436
Jens Axboecca84642011-10-07 12:47:57 +0200437 probe.os = FIO_OS;
438 probe.arch = FIO_ARCH;
439
Jens Axboe38fdef22011-10-11 14:30:06 +0200440 probe.bpp = sizeof(void *);
441
Jens Axboe89c17072011-10-11 10:15:51 +0200442 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200443}
444
445static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
446{
447 struct jobs_eta *je;
448 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200449 int i;
450
Jens Axboeb814fb22011-10-12 09:20:34 +0200451 if (!thread_number)
452 return 0;
453
454 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200455 je = malloc(size);
456 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200457
458 if (!calc_thread_status(je, 1)) {
459 free(je);
460 return 0;
461 }
462
463 dprint(FD_NET, "server sending status\n");
464
465 je->nr_running = cpu_to_le32(je->nr_running);
466 je->nr_ramp = cpu_to_le32(je->nr_ramp);
467 je->nr_pending = cpu_to_le32(je->nr_pending);
468 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200469
470 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100471 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
472 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
473 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
474 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200475 je->rate[i] = cpu_to_le32(je->rate[i]);
476 je->iops[i] = cpu_to_le32(je->iops[i]);
477 }
478
Jens Axboec1970b92012-03-06 15:37:40 +0100479 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200480 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100481 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200482
Jens Axboe7f868312011-10-10 09:55:21 +0200483 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200484 free(je);
485 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200486}
487
Jens Axboe132159a2011-09-30 15:01:32 -0600488static int handle_command(struct fio_net_cmd *cmd)
489{
490 int ret;
491
Jens Axboe89c17072011-10-11 10:15:51 +0200492 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
493 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600494
Jens Axboe132159a2011-09-30 15:01:32 -0600495 switch (cmd->opcode) {
496 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200497 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200498 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200499 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600500 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200501 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600502 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200503 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600504 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200505 case FIO_NET_CMD_JOBLINE:
506 ret = handle_jobline_cmd(cmd);
507 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200508 case FIO_NET_CMD_PROBE:
509 ret = handle_probe_cmd(cmd);
510 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200511 case FIO_NET_CMD_SEND_ETA:
512 ret = handle_send_eta_cmd(cmd);
513 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100514 case FIO_NET_CMD_RUN:
515 ret = handle_run_cmd(cmd);
516 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600517 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200518 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600519 ret = 1;
520 }
521
522 return ret;
523}
524
Jens Axboe70e0c312011-10-04 09:18:30 +0200525static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600526{
527 struct fio_net_cmd *cmd = NULL;
528 int ret = 0;
529
530 /* read forever */
531 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200532 struct pollfd pfd = {
533 .fd = sk,
534 .events = POLLIN,
535 };
536
537 ret = 0;
538 do {
539 ret = poll(&pfd, 1, 100);
540 if (ret < 0) {
541 if (errno == EINTR)
542 break;
543 log_err("fio: poll: %s\n", strerror(errno));
544 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200545 } else if (!ret) {
546 if (!block)
547 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200548 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200549 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200550
551 if (pfd.revents & POLLIN)
552 break;
553 if (pfd.revents & (POLLERR|POLLHUP)) {
554 ret = 1;
555 break;
556 }
Jens Axboe19c65172011-10-05 22:05:37 +0200557 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200558
559 if (ret < 0)
560 break;
561
562 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600563 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200564 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600565 break;
566 }
567
Jens Axboe132159a2011-09-30 15:01:32 -0600568 ret = handle_command(cmd);
569 if (ret)
570 break;
571
572 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400573 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600574 }
575
576 if (cmd)
577 free(cmd);
578
579 return ret;
580}
581
Jens Axboecc0df002011-10-03 20:53:32 +0200582void fio_server_idle_loop(void)
583{
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100584 if (!first_cmd_check) {
Jens Axboe5d7793a2012-03-08 19:59:16 +0100585 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100586 first_cmd_check = 1;
587 }
Jens Axboecc0df002011-10-03 20:53:32 +0200588 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200589 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200590}
591
Jens Axboe50d16972011-09-29 17:45:28 -0600592static int accept_loop(int listen_sk)
593{
Jens Axboebb447a22011-10-04 15:06:42 +0200594 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200595 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600596 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600597 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600598
Jens Axboe60efd142011-10-04 13:30:11 +0200599 dprint(FD_NET, "server enter accept loop\n");
600
Jens Axboe009b1be2011-09-29 18:27:02 -0600601 flags = fcntl(listen_sk, F_GETFL);
602 flags |= O_NONBLOCK;
603 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600604again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600605 pfd.fd = listen_sk;
606 pfd.events = POLLIN;
607 do {
608 ret = poll(&pfd, 1, 100);
609 if (ret < 0) {
610 if (errno == EINTR)
611 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600612 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600613 goto out;
614 } else if (!ret)
615 continue;
616
617 if (pfd.revents & POLLIN)
618 break;
619 } while (!exit_backend);
620
621 if (exit_backend)
622 goto out;
623
Jens Axboe6b976bf2011-10-04 15:07:43 +0200624 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600625 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600626 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600627 return -1;
628 }
629
Jens Axboebb447a22011-10-04 15:06:42 +0200630 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600631
Jens Axboe37db14f2011-09-30 17:00:42 -0600632 server_fd = sk;
633
Jens Axboe70e0c312011-10-04 09:18:30 +0200634 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600635
Jens Axboe37db14f2011-09-30 17:00:42 -0600636 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600637 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600638
Jens Axboe009b1be2011-09-29 18:27:02 -0600639 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600640 goto again;
641
Jens Axboe009b1be2011-09-29 18:27:02 -0600642out:
Jens Axboe132159a2011-09-30 15:01:32 -0600643 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600644}
645
Jens Axboe084d1c62012-03-03 20:28:07 +0100646int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600647{
Jens Axboe084d1c62012-03-03 20:28:07 +0100648 struct cmd_text_pdu *pdu;
649 unsigned int tlen;
650 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600651
Jens Axboe084d1c62012-03-03 20:28:07 +0100652 if (server_fd == -1)
653 return log_local_buf(buf, len);
654
655 tlen = sizeof(*pdu) + len;
656 pdu = malloc(tlen);
657
658 pdu->level = __cpu_to_le32(level);
659 pdu->buf_len = __cpu_to_le32(len);
660
661 gettimeofday(&tv, NULL);
662 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
663 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
664
665 memcpy(pdu->buf, buf, len);
666
667 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, 0);
668 free(pdu);
669 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600670}
671
Jens Axboea64e88d2011-10-03 14:20:01 +0200672static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
673{
674 dst->max_val = cpu_to_le64(src->max_val);
675 dst->min_val = cpu_to_le64(src->min_val);
676 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200677
678 /*
679 * Encode to IEEE 754 for network transfer
680 */
681 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
682 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200683}
684
685static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
686{
687 int i;
688
689 for (i = 0; i < 2; i++) {
690 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
691 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
692 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
693 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
694 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
695 dst->agg[i] = cpu_to_le64(src->agg[i]);
696 }
697
698 dst->kb_base = cpu_to_le32(src->kb_base);
699 dst->groupid = cpu_to_le32(src->groupid);
700}
701
702/*
703 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
704 * into a single payload.
705 */
706void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
707{
708 struct cmd_ts_pdu p;
709 int i, j;
710
Jens Axboe60efd142011-10-04 13:30:11 +0200711 dprint(FD_NET, "server sending end stats\n");
712
Jens Axboe317b3c82011-10-03 21:47:27 +0200713 memset(&p, 0, sizeof(p));
714
Jens Axboea64e88d2011-10-03 14:20:01 +0200715 strcpy(p.ts.name, ts->name);
716 strcpy(p.ts.verror, ts->verror);
717 strcpy(p.ts.description, ts->description);
718
Jens Axboeddcc0b62011-10-03 14:45:27 +0200719 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200720 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200721 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200722 p.ts.members = cpu_to_le32(ts->members);
723
724 for (i = 0; i < 2; i++) {
725 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
726 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
727 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
728 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
729 }
730
731 p.ts.usr_time = cpu_to_le64(ts->usr_time);
732 p.ts.sys_time = cpu_to_le64(ts->sys_time);
733 p.ts.ctx = cpu_to_le64(ts->ctx);
734 p.ts.minf = cpu_to_le64(ts->minf);
735 p.ts.majf = cpu_to_le64(ts->majf);
736 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200737
738 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200739 fio_fp64_t *src = &ts->percentile_list[i];
740 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200741
Jens Axboecfc03e42011-10-12 21:20:42 +0200742 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200743 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200744
745 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
746 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
747 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
748 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
749 }
750
751 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
752 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
753 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
754 }
755
756 for (i = 0; i < 2; i++)
757 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
758 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
759
760 for (i = 0; i < 3; i++) {
761 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200762 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200763 }
764
Jens Axboe93eee042011-10-03 21:08:48 +0200765 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200766 p.ts.total_complete = cpu_to_le64(ts->total_complete);
767
768 for (i = 0; i < 2; i++) {
769 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
770 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
771 }
772
773 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
774 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
775 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200776 p.ts.first_error = cpu_to_le32(ts->first_error);
777 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200778
779 convert_gs(&p.rs, rs);
780
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200781 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200782}
783
784void fio_server_send_gs(struct group_run_stats *rs)
785{
786 struct group_run_stats gs;
787
Jens Axboe60efd142011-10-04 13:30:11 +0200788 dprint(FD_NET, "server sending group run stats\n");
789
Jens Axboea64e88d2011-10-03 14:20:01 +0200790 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200791 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200792}
793
Jens Axboed09a64a2011-10-13 11:38:56 +0200794static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
795{
796 int i;
797
798 for (i = 0; i < 2; i++) {
799 dst->ios[i] = cpu_to_le32(src->ios[i]);
800 dst->merges[i] = cpu_to_le32(src->merges[i]);
801 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
802 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
803 }
804
805 dst->io_ticks = cpu_to_le32(src->io_ticks);
806 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
807 dst->slavecount = cpu_to_le32(src->slavecount);
808 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
809}
810
811static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
812{
813 int i;
814
815 strcpy((char *) dst->name, (char *) src->name);
816
817 for (i = 0; i < 2; i++) {
818 dst->ios[i] = cpu_to_le32(src->ios[i]);
819 dst->merges[i] = cpu_to_le32(src->merges[i]);
820 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
821 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
822 }
823
824 dst->io_ticks = cpu_to_le32(src->io_ticks);
825 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
826 dst->msec = cpu_to_le64(src->msec);
827}
828
829void fio_server_send_du(void)
830{
831 struct disk_util *du;
832 struct flist_head *entry;
833 struct cmd_du_pdu pdu;
834
835 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
836
Jens Axboe0766d922011-10-13 12:02:08 +0200837 memset(&pdu, 0, sizeof(pdu));
838
Jens Axboed09a64a2011-10-13 11:38:56 +0200839 flist_for_each(entry, &disk_list) {
840 du = flist_entry(entry, struct disk_util, list);
841
842 convert_dus(&pdu.dus, &du->dus);
843 convert_agg(&pdu.agg, &du->agg);
844
845 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
846 }
847}
848
Jens Axboe807f9972012-03-02 10:25:24 +0100849void fio_server_send_add_job(struct thread_options *o, const char *ioengine)
850{
851 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +0100852
Jens Axboedcaeb602012-03-08 19:45:37 +0100853 convert_thread_options_to_net(&pdu.top, o);
Jens Axboe807f9972012-03-02 10:25:24 +0100854
855 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), 0);
856}
857
Jens Axboe87aa8f12011-10-06 21:24:13 +0200858static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200859{
Jens Axboe811826b2011-10-24 09:11:50 +0200860 struct sockaddr *addr;
861 fio_socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200862 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200863
Jens Axboe811826b2011-10-24 09:11:50 +0200864 if (use_ipv6)
865 sk = socket(AF_INET6, SOCK_STREAM, 0);
866 else
867 sk = socket(AF_INET, SOCK_STREAM, 0);
868
Jens Axboe81179ee2011-10-04 12:42:06 +0200869 if (sk < 0) {
870 log_err("fio: socket: %s\n", strerror(errno));
871 return -1;
872 }
873
874 opt = 1;
875 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
876 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200877 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200878 return -1;
879 }
880#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200881 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200882 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200883 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200884 return -1;
885 }
886#endif
887
Jens Axboe811826b2011-10-24 09:11:50 +0200888 if (use_ipv6) {
889 addr = (struct sockaddr *) &saddr_in6;
890 socklen = sizeof(saddr_in6);
891 saddr_in6.sin6_family = AF_INET6;
892 } else {
893 addr = (struct sockaddr *) &saddr_in;
894 socklen = sizeof(saddr_in);
895 saddr_in.sin_family = AF_INET;
896 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200897
Jens Axboe811826b2011-10-24 09:11:50 +0200898 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200899 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200900 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200901 return -1;
902 }
903
Jens Axboe87aa8f12011-10-06 21:24:13 +0200904 return sk;
905}
906
907static int fio_init_server_sock(void)
908{
909 struct sockaddr_un addr;
910 fio_socklen_t len;
911 mode_t mode;
912 int sk;
913
914 sk = socket(AF_UNIX, SOCK_STREAM, 0);
915 if (sk < 0) {
916 log_err("fio: socket: %s\n", strerror(errno));
917 return -1;
918 }
919
920 mode = umask(000);
921
922 memset(&addr, 0, sizeof(addr));
923 addr.sun_family = AF_UNIX;
924 strcpy(addr.sun_path, bind_sock);
925 unlink(bind_sock);
926
927 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
928
929 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
930 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200931 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200932 return -1;
933 }
934
935 umask(mode);
936 return sk;
937}
938
939static int fio_init_server_connection(void)
940{
Jens Axboebebe6392011-10-07 10:00:51 +0200941 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +0200942 int sk;
943
944 dprint(FD_NET, "starting server\n");
945
946 if (!bind_sock)
947 sk = fio_init_server_ip();
948 else
949 sk = fio_init_server_sock();
950
951 if (sk < 0)
952 return sk;
953
Jens Axboe811826b2011-10-24 09:11:50 +0200954 if (!bind_sock) {
955 char *p, port[16];
956 const void *src;
957 int af;
958
959 if (use_ipv6) {
960 af = AF_INET6;
961 src = &saddr_in6.sin6_addr;
962 } else {
963 af = AF_INET;
964 src = &saddr_in.sin_addr;
965 }
966
967 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
968
969 sprintf(port, ",%u", fio_net_port);
970 if (p)
971 strcat(p, port);
972 else
973 strcpy(bind_str, port);
974 } else
Jens Axboebebe6392011-10-07 10:00:51 +0200975 strcpy(bind_str, bind_sock);
976
977 log_info("fio: server listening on %s\n", bind_str);
978
Jens Axboe89c17072011-10-11 10:15:51 +0200979 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200980 log_err("fio: listen: %s\n", strerror(errno));
981 return -1;
982 }
983
Jens Axboe87aa8f12011-10-06 21:24:13 +0200984 return sk;
985}
986
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100987int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
988 struct in6_addr *inp6)
989
990{
991 int ret = 0;
992
993 if (*ipv6)
994 ret = inet_pton(AF_INET6, host, inp6);
995 else
996 ret = inet_pton(AF_INET, host, inp);
997
998 if (ret != 1) {
999 struct hostent *hent;
1000
1001 hent = gethostbyname(host);
1002 if (!hent) {
1003 log_err("fio: failed to resolve <%s>\n", host);
1004 return 0;
1005 }
1006
1007 if (*ipv6) {
1008 if (hent->h_addrtype != AF_INET6) {
1009 log_info("fio: falling back to IPv4\n");
1010 *ipv6 = 0;
1011 } else
1012 memcpy(inp6, hent->h_addr_list[0], 16);
1013 }
1014 if (!*ipv6) {
1015 if (hent->h_addrtype != AF_INET) {
1016 log_err("fio: lookup type mismatch\n");
1017 return 0;
1018 }
1019 memcpy(inp, hent->h_addr_list[0], 4);
1020 }
1021 ret = 1;
1022 }
1023
1024 return !(ret == 1);
1025}
1026
Jens Axboe660a2bf2011-10-24 09:35:06 +02001027/*
1028 * Parse a host/ip/port string. Reads from 'str'.
1029 *
1030 * Outputs:
1031 *
1032 * For IPv4:
1033 * *ptr is the host, *port is the port, inp is the destination.
1034 * For IPv6:
1035 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1036 * For local domain sockets:
1037 * *ptr is the filename, *is_sock is 1.
1038 */
Jens Axboebebe6392011-10-07 10:00:51 +02001039int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001040 int *port, struct in_addr *inp,
1041 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001042{
Jens Axboe76867622011-10-25 09:52:51 +02001043 const char *host = str;
1044 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001045 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001046
Jens Axboebebe6392011-10-07 10:00:51 +02001047 *ptr = NULL;
1048 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001049 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001050 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001051
1052 if (!strncmp(str, "sock:", 5)) {
1053 *ptr = strdup(str + 5);
1054 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001055
Jens Axboe76867622011-10-25 09:52:51 +02001056 return 0;
1057 }
1058
1059 /*
1060 * Is it ip:<ip or host>:port
1061 */
1062 if (!strncmp(host, "ip:", 3))
1063 host += 3;
1064 else if (!strncmp(host, "ip4:", 4))
1065 host += 4;
1066 else if (!strncmp(host, "ip6:", 4)) {
1067 host += 4;
1068 *ipv6 = 1;
1069 } else if (host[0] == ':') {
1070 /* String is :port */
1071 host++;
1072 lport = atoi(host);
1073 if (!lport || lport > 65535) {
1074 log_err("fio: bad server port %u\n", port);
1075 return 1;
1076 }
1077 /* no hostname given, we are done */
1078 *port = lport;
1079 return 0;
1080 }
1081
1082 /*
1083 * If no port seen yet, check if there's a last ':' at the end
1084 */
1085 if (!lport) {
1086 portp = strchr(host, ',');
1087 if (portp) {
1088 *portp = '\0';
1089 portp++;
1090 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001091 if (!lport || lport > 65535) {
1092 log_err("fio: bad server port %u\n", port);
1093 return 1;
1094 }
Jens Axboe76867622011-10-25 09:52:51 +02001095 }
1096 }
1097
1098 if (lport)
1099 *port = lport;
1100
1101 if (!strlen(host))
1102 return 0;
1103
1104 *ptr = strdup(host);
1105
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001106 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1107 free(*ptr);
1108 *ptr = NULL;
1109 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001110 }
1111
1112 if (*port == 0)
1113 *port = fio_net_port;
1114
1115 return 0;
1116}
1117
Jens Axboe87aa8f12011-10-06 21:24:13 +02001118/*
1119 * Server arg should be one of:
1120 *
1121 * sock:/path/to/socket
1122 * ip:1.2.3.4
1123 * 1.2.3.4
1124 *
1125 * Where sock uses unix domain sockets, and ip binds the server to
1126 * a specific interface. If no arguments are given to the server, it
1127 * uses IP and binds to 0.0.0.0.
1128 *
1129 */
1130static int fio_handle_server_arg(void)
1131{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001132 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001133 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001134
Jens Axboe87aa8f12011-10-06 21:24:13 +02001135 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1136
1137 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001138 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001139
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001140 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001141 &port, &saddr_in.sin_addr,
1142 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001143
1144 if (!is_sock && bind_sock) {
1145 free(bind_sock);
1146 bind_sock = NULL;
1147 }
1148
Jens Axboea7de0a12011-10-07 12:55:14 +02001149out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001150 fio_net_port = port;
1151 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001152 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001153 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001154}
1155
1156static int fio_server(void)
1157{
1158 int sk, ret;
1159
1160 dprint(FD_NET, "starting server\n");
1161
1162 if (fio_handle_server_arg())
1163 return -1;
1164
1165 sk = fio_init_server_connection();
1166 if (sk < 0)
1167 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001168
1169 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001170
Jens Axboe81179ee2011-10-04 12:42:06 +02001171 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001172
1173 if (fio_server_arg) {
1174 free(fio_server_arg);
1175 fio_server_arg = NULL;
1176 }
Jens Axboebebe6392011-10-07 10:00:51 +02001177 if (bind_sock)
1178 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001179
Jens Axboe81179ee2011-10-04 12:42:06 +02001180 return ret;
1181}
1182
Jens Axboe7b821682011-10-11 12:16:32 +02001183void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001184{
Jens Axboe7b821682011-10-11 12:16:32 +02001185 if (signal == SIGPIPE)
1186 server_fd = -1;
1187 else {
1188 log_info("\nfio: terminating on signal %d\n", signal);
1189 exit_backend = 1;
1190 }
Jens Axboe9abea482011-10-04 13:21:54 +02001191}
1192
Jens Axboe13755d92011-10-10 19:51:26 +02001193static int check_existing_pidfile(const char *pidfile)
1194{
1195 struct stat sb;
1196 char buf[16];
1197 pid_t pid;
1198 FILE *f;
1199
1200 if (stat(pidfile, &sb))
1201 return 0;
1202
1203 f = fopen(pidfile, "r");
1204 if (!f)
1205 return 0;
1206
Jens Axboebfc3b172011-10-10 21:16:55 +02001207 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001208 fclose(f);
1209 return 1;
1210 }
1211 fclose(f);
1212
1213 pid = atoi(buf);
1214 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001215 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001216
1217 return 1;
1218}
1219
1220static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001221{
1222 FILE *fpid;
1223
1224 fpid = fopen(pidfile, "w");
1225 if (!fpid) {
1226 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001227 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001228 }
1229
1230 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001231 fclose(fpid);
1232 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001233}
1234
1235/*
1236 * If pidfile is specified, background us.
1237 */
1238int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001239{
1240 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001241 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001242
Bruce Cran93bcfd22012-02-20 20:18:19 +01001243#if defined(WIN32)
Jens Axboe905c78b2012-03-06 19:34:22 +01001244 WSADATA wsd;
1245 WSAStartup(MAKEWORD(2,2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +01001246#endif
1247
Jens Axboe402668f2011-10-10 15:28:58 +02001248 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001249 return fio_server();
1250
Jens Axboe13755d92011-10-10 19:51:26 +02001251 if (check_existing_pidfile(pidfile)) {
1252 log_err("fio: pidfile %s exists and server appears alive\n",
1253 pidfile);
1254 return -1;
1255 }
1256
Jens Axboee46d8092011-10-03 09:11:02 +02001257 pid = fork();
1258 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001259 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001260 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001261 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001262 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001263 int ret = write_pid(pid, pidfile);
1264
1265 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001266 }
Jens Axboee46d8092011-10-03 09:11:02 +02001267
1268 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001269 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1270 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001271 close(STDIN_FILENO);
1272 close(STDOUT_FILENO);
1273 close(STDERR_FILENO);
1274 f_out = NULL;
1275 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001276
1277 ret = fio_server();
1278
1279 closelog();
1280 unlink(pidfile);
1281 free(pidfile);
1282 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001283}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001284
Jens Axboebebe6392011-10-07 10:00:51 +02001285void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001286{
1287 fio_server_arg = strdup(arg);
1288}