blob: a366f68862a5c018d8b36da8fdca35ecb7aeaceb [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) {
236 char *buf = (char *) cmdret->payload;
237
238 buf[cmdret->pdu_len ] = '\0';
239 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200240 }
Jens Axboe084d1c62012-03-03 20:28:07 +0100241
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200242 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200243 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200244 }
Jens Axboe132159a2011-09-30 15:01:32 -0600245
Jens Axboea64e88d2011-10-03 14:20:01 +0200246 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600247}
248
249void fio_net_cmd_crc(struct fio_net_cmd *cmd)
250{
251 uint32_t pdu_len;
252
Jens Axboe25dfa842012-02-29 10:01:34 +0100253 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600254
255 pdu_len = le32_to_cpu(cmd->pdu_len);
256 if (pdu_len)
Jens Axboe25dfa842012-02-29 10:01:34 +0100257 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600258}
259
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200260int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
261 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600262{
Jens Axboe7f868312011-10-10 09:55:21 +0200263 struct fio_net_cmd *cmd = NULL;
264 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600265 int ret;
266
267 do {
268 this_len = size;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100269 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
270 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
Jens Axboe794d69c2011-10-01 08:48:50 -0600271
Jens Axboe7f868312011-10-10 09:55:21 +0200272 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
273 if (cmd)
274 free(cmd);
275
276 cur_len = sizeof(*cmd) + this_len;
277 cmd = malloc(cur_len);
278 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600279
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200280 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600281
282 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200283 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600284
285 fio_net_cmd_crc(cmd);
286
287 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600288 size -= this_len;
289 buf += this_len;
290 } while (!ret && size);
291
Jens Axboe7f868312011-10-10 09:55:21 +0200292 if (cmd)
293 free(cmd);
294
Jens Axboe794d69c2011-10-01 08:48:50 -0600295 return ret;
296}
297
Jens Axboe89c17072011-10-11 10:15:51 +0200298static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600299{
Jens Axboe178cde92011-10-05 22:14:31 +0200300 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600301
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200302 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600303 fio_net_cmd_crc(&cmd);
304
305 return fio_send_data(sk, &cmd, sizeof(cmd));
306}
307
Jens Axboe89c17072011-10-11 10:15:51 +0200308/*
309 * If 'list' is non-NULL, then allocate and store the sent command for
310 * later verification.
311 */
312int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
313 struct flist_head *list)
314{
315 struct fio_net_int_cmd *cmd;
316 int ret;
317
318 if (!list)
319 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
320
321 cmd = malloc(sizeof(*cmd));
322
Jens Axboedf380932011-10-11 14:25:08 +0200323 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
Jens Axboe89c17072011-10-11 10:15:51 +0200324 fio_net_cmd_crc(&cmd->cmd);
325
326 INIT_FLIST_HEAD(&cmd->list);
327 gettimeofday(&cmd->tv, NULL);
328 cmd->saved_tag = tag;
329
330 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
331 if (ret) {
332 free(cmd);
333 return ret;
334 }
335
336 flist_add_tail(&cmd->list, list);
337 return 0;
338}
339
Jens Axboe9abea482011-10-04 13:21:54 +0200340static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600341{
Jens Axboe46c48f12011-10-01 12:50:51 -0600342 dprint(FD_NET, "server: sending quit\n");
Jens Axboe89c17072011-10-11 10:15:51 +0200343 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600344}
345
Jens Axboeb9d2f302012-03-08 20:36:28 +0100346static int handle_run_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600347{
Jens Axboe11e950b2011-10-16 21:34:14 +0200348 struct cmd_end_pdu epdu;
Jens Axboea64e88d2011-10-03 14:20:01 +0200349 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600350
Jens Axboe2e1df072012-02-09 11:15:02 +0100351 ret = fio_backend();
Jens Axboe11e950b2011-10-16 21:34:14 +0200352
353 epdu.error = ret;
354 fio_net_send_cmd(server_fd, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0);
355
Jens Axboe9abea482011-10-04 13:21:54 +0200356 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200357 reset_fio_state();
Jens Axboe8663ea62012-03-02 14:04:30 +0100358 first_cmd_check = 0;
Jens Axboe81179ee2011-10-04 12:42:06 +0200359 return ret;
360}
361
Jens Axboeb9d2f302012-03-08 20:36:28 +0100362static int handle_job_cmd(struct fio_net_cmd *cmd)
363{
364 char *buf = (char *) cmd->payload;
365 struct cmd_start_pdu spdu;
366
367 if (parse_jobs_ini(buf, 1, 0)) {
368 fio_server_send_quit_cmd();
369 return -1;
370 }
371
372 spdu.jobs = cpu_to_le32(thread_number);
373 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
374 return 0;
375}
376
Jens Axboe81179ee2011-10-04 12:42:06 +0200377static int handle_jobline_cmd(struct fio_net_cmd *cmd)
378{
Jens Axboefa2ea802011-10-08 21:07:29 +0200379 void *pdu = cmd->payload;
380 struct cmd_single_line_pdu *cslp;
381 struct cmd_line_pdu *clp;
382 unsigned long offset;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100383 struct cmd_start_pdu spdu;
Jens Axboefa2ea802011-10-08 21:07:29 +0200384 char **argv;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100385 int i;
Jens Axboe81179ee2011-10-04 12:42:06 +0200386
Jens Axboefa2ea802011-10-08 21:07:29 +0200387 clp = pdu;
388 clp->lines = le16_to_cpu(clp->lines);
389 argv = malloc(clp->lines * sizeof(char *));
390 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200391
Jens Axboefa2ea802011-10-08 21:07:29 +0200392 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200393
Jens Axboefa2ea802011-10-08 21:07:29 +0200394 for (i = 0; i < clp->lines; i++) {
395 cslp = pdu + offset;
396 argv[i] = (char *) cslp->text;
397
398 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200399 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
400 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200401
Jens Axboefa2ea802011-10-08 21:07:29 +0200402 if (parse_cmd_line(clp->lines, argv)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200403 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200404 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200405 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200406 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200407
Jens Axboefa2ea802011-10-08 21:07:29 +0200408 free(argv);
409
Jens Axboeb9d2f302012-03-08 20:36:28 +0100410 spdu.jobs = cpu_to_le32(thread_number);
411 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
412 return 0;
Jens Axboe132159a2011-09-30 15:01:32 -0600413}
414
Jens Axboec28e8e82011-10-04 08:57:39 +0200415static int handle_probe_cmd(struct fio_net_cmd *cmd)
416{
417 struct cmd_probe_pdu probe;
418
Jens Axboe89c17072011-10-11 10:15:51 +0200419 dprint(FD_NET, "server: sending probe reply\n");
420
Jens Axboec28e8e82011-10-04 08:57:39 +0200421 memset(&probe, 0, sizeof(probe));
422 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200423#ifdef FIO_BIG_ENDIAN
424 probe.bigendian = 1;
425#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200426 probe.fio_major = FIO_MAJOR;
427 probe.fio_minor = FIO_MINOR;
428 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200429
Jens Axboecca84642011-10-07 12:47:57 +0200430 probe.os = FIO_OS;
431 probe.arch = FIO_ARCH;
432
Jens Axboe38fdef22011-10-11 14:30:06 +0200433 probe.bpp = sizeof(void *);
434
Jens Axboe89c17072011-10-11 10:15:51 +0200435 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200436}
437
438static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
439{
440 struct jobs_eta *je;
441 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200442 int i;
443
Jens Axboeb814fb22011-10-12 09:20:34 +0200444 if (!thread_number)
445 return 0;
446
447 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200448 je = malloc(size);
449 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200450
451 if (!calc_thread_status(je, 1)) {
452 free(je);
453 return 0;
454 }
455
456 dprint(FD_NET, "server sending status\n");
457
458 je->nr_running = cpu_to_le32(je->nr_running);
459 je->nr_ramp = cpu_to_le32(je->nr_ramp);
460 je->nr_pending = cpu_to_le32(je->nr_pending);
461 je->files_open = cpu_to_le32(je->files_open);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200462
463 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100464 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
465 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
466 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
467 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200468 je->rate[i] = cpu_to_le32(je->rate[i]);
469 je->iops[i] = cpu_to_le32(je->iops[i]);
470 }
471
Jens Axboec1970b92012-03-06 15:37:40 +0100472 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200473 je->eta_sec = cpu_to_le64(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100474 je->nr_threads = cpu_to_le32(je->nr_threads);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200475
Jens Axboe7f868312011-10-10 09:55:21 +0200476 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200477 free(je);
478 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200479}
480
Jens Axboe132159a2011-09-30 15:01:32 -0600481static int handle_command(struct fio_net_cmd *cmd)
482{
483 int ret;
484
Jens Axboe89c17072011-10-11 10:15:51 +0200485 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
486 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600487
Jens Axboe132159a2011-09-30 15:01:32 -0600488 switch (cmd->opcode) {
489 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200490 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200491 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200492 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600493 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200494 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600495 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200496 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600497 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200498 case FIO_NET_CMD_JOBLINE:
499 ret = handle_jobline_cmd(cmd);
500 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200501 case FIO_NET_CMD_PROBE:
502 ret = handle_probe_cmd(cmd);
503 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200504 case FIO_NET_CMD_SEND_ETA:
505 ret = handle_send_eta_cmd(cmd);
506 break;
Jens Axboeb9d2f302012-03-08 20:36:28 +0100507 case FIO_NET_CMD_RUN:
508 ret = handle_run_cmd(cmd);
509 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600510 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200511 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600512 ret = 1;
513 }
514
515 return ret;
516}
517
Jens Axboe70e0c312011-10-04 09:18:30 +0200518static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600519{
520 struct fio_net_cmd *cmd = NULL;
521 int ret = 0;
522
523 /* read forever */
524 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200525 struct pollfd pfd = {
526 .fd = sk,
527 .events = POLLIN,
528 };
529
530 ret = 0;
531 do {
532 ret = poll(&pfd, 1, 100);
533 if (ret < 0) {
534 if (errno == EINTR)
535 break;
536 log_err("fio: poll: %s\n", strerror(errno));
537 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200538 } else if (!ret) {
539 if (!block)
540 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200541 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200542 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200543
544 if (pfd.revents & POLLIN)
545 break;
546 if (pfd.revents & (POLLERR|POLLHUP)) {
547 ret = 1;
548 break;
549 }
Jens Axboe19c65172011-10-05 22:05:37 +0200550 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200551
552 if (ret < 0)
553 break;
554
555 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600556 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200557 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600558 break;
559 }
560
Jens Axboe132159a2011-09-30 15:01:32 -0600561 ret = handle_command(cmd);
562 if (ret)
563 break;
564
565 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400566 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600567 }
568
569 if (cmd)
570 free(cmd);
571
572 return ret;
573}
574
Jens Axboecc0df002011-10-03 20:53:32 +0200575void fio_server_idle_loop(void)
576{
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100577 if (!first_cmd_check) {
Jens Axboe5d7793a2012-03-08 19:59:16 +0100578 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100579 first_cmd_check = 1;
580 }
Jens Axboecc0df002011-10-03 20:53:32 +0200581 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200582 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200583}
584
Jens Axboe50d16972011-09-29 17:45:28 -0600585static int accept_loop(int listen_sk)
586{
Jens Axboebb447a22011-10-04 15:06:42 +0200587 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200588 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600589 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600590 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600591
Jens Axboe60efd142011-10-04 13:30:11 +0200592 dprint(FD_NET, "server enter accept loop\n");
593
Jens Axboe009b1be2011-09-29 18:27:02 -0600594 flags = fcntl(listen_sk, F_GETFL);
595 flags |= O_NONBLOCK;
596 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600597again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600598 pfd.fd = listen_sk;
599 pfd.events = POLLIN;
600 do {
601 ret = poll(&pfd, 1, 100);
602 if (ret < 0) {
603 if (errno == EINTR)
604 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600605 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600606 goto out;
607 } else if (!ret)
608 continue;
609
610 if (pfd.revents & POLLIN)
611 break;
612 } while (!exit_backend);
613
614 if (exit_backend)
615 goto out;
616
Jens Axboe6b976bf2011-10-04 15:07:43 +0200617 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600618 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600619 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600620 return -1;
621 }
622
Jens Axboebb447a22011-10-04 15:06:42 +0200623 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600624
Jens Axboe37db14f2011-09-30 17:00:42 -0600625 server_fd = sk;
626
Jens Axboe70e0c312011-10-04 09:18:30 +0200627 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600628
Jens Axboe37db14f2011-09-30 17:00:42 -0600629 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600630 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600631
Jens Axboe009b1be2011-09-29 18:27:02 -0600632 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600633 goto again;
634
Jens Axboe009b1be2011-09-29 18:27:02 -0600635out:
Jens Axboe132159a2011-09-30 15:01:32 -0600636 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600637}
638
Jens Axboe084d1c62012-03-03 20:28:07 +0100639int fio_server_text_output(int level, const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600640{
Jens Axboe084d1c62012-03-03 20:28:07 +0100641 struct cmd_text_pdu *pdu;
642 unsigned int tlen;
643 struct timeval tv;
Jens Axboe337d75a2011-10-01 12:36:32 -0600644
Jens Axboe084d1c62012-03-03 20:28:07 +0100645 if (server_fd == -1)
646 return log_local_buf(buf, len);
647
648 tlen = sizeof(*pdu) + len;
649 pdu = malloc(tlen);
650
651 pdu->level = __cpu_to_le32(level);
652 pdu->buf_len = __cpu_to_le32(len);
653
654 gettimeofday(&tv, NULL);
655 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
656 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
657
658 memcpy(pdu->buf, buf, len);
659
660 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, 0);
661 free(pdu);
662 return len;
Jens Axboe142575e2011-09-30 17:35:45 -0600663}
664
Jens Axboea64e88d2011-10-03 14:20:01 +0200665static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
666{
667 dst->max_val = cpu_to_le64(src->max_val);
668 dst->min_val = cpu_to_le64(src->min_val);
669 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200670
671 /*
672 * Encode to IEEE 754 for network transfer
673 */
674 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
675 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200676}
677
678static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
679{
680 int i;
681
682 for (i = 0; i < 2; i++) {
683 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
684 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
685 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
686 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
687 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
688 dst->agg[i] = cpu_to_le64(src->agg[i]);
689 }
690
691 dst->kb_base = cpu_to_le32(src->kb_base);
692 dst->groupid = cpu_to_le32(src->groupid);
693}
694
695/*
696 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
697 * into a single payload.
698 */
699void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
700{
701 struct cmd_ts_pdu p;
702 int i, j;
703
Jens Axboe60efd142011-10-04 13:30:11 +0200704 dprint(FD_NET, "server sending end stats\n");
705
Jens Axboe317b3c82011-10-03 21:47:27 +0200706 memset(&p, 0, sizeof(p));
707
Jens Axboea64e88d2011-10-03 14:20:01 +0200708 strcpy(p.ts.name, ts->name);
709 strcpy(p.ts.verror, ts->verror);
710 strcpy(p.ts.description, ts->description);
711
Jens Axboeddcc0b62011-10-03 14:45:27 +0200712 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200713 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200714 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200715 p.ts.members = cpu_to_le32(ts->members);
716
717 for (i = 0; i < 2; i++) {
718 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
719 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
720 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
721 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
722 }
723
724 p.ts.usr_time = cpu_to_le64(ts->usr_time);
725 p.ts.sys_time = cpu_to_le64(ts->sys_time);
726 p.ts.ctx = cpu_to_le64(ts->ctx);
727 p.ts.minf = cpu_to_le64(ts->minf);
728 p.ts.majf = cpu_to_le64(ts->majf);
729 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200730
731 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
Jens Axboecfc03e42011-10-12 21:20:42 +0200732 fio_fp64_t *src = &ts->percentile_list[i];
733 fio_fp64_t *dst = &p.ts.percentile_list[i];
Jens Axboe802ad4a2011-10-05 09:51:58 +0200734
Jens Axboecfc03e42011-10-12 21:20:42 +0200735 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
Jens Axboe802ad4a2011-10-05 09:51:58 +0200736 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200737
738 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
739 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
740 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
741 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
742 }
743
744 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
745 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
746 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
747 }
748
749 for (i = 0; i < 2; i++)
750 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
751 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
752
753 for (i = 0; i < 3; i++) {
754 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200755 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200756 }
757
Jens Axboe93eee042011-10-03 21:08:48 +0200758 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200759 p.ts.total_complete = cpu_to_le64(ts->total_complete);
760
761 for (i = 0; i < 2; i++) {
762 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
763 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
764 }
765
766 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
767 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
768 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200769 p.ts.first_error = cpu_to_le32(ts->first_error);
770 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200771
772 convert_gs(&p.rs, rs);
773
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200774 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200775}
776
777void fio_server_send_gs(struct group_run_stats *rs)
778{
779 struct group_run_stats gs;
780
Jens Axboe60efd142011-10-04 13:30:11 +0200781 dprint(FD_NET, "server sending group run stats\n");
782
Jens Axboea64e88d2011-10-03 14:20:01 +0200783 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200784 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200785}
786
Jens Axboed09a64a2011-10-13 11:38:56 +0200787static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
788{
789 int i;
790
791 for (i = 0; i < 2; i++) {
792 dst->ios[i] = cpu_to_le32(src->ios[i]);
793 dst->merges[i] = cpu_to_le32(src->merges[i]);
794 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
795 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
796 }
797
798 dst->io_ticks = cpu_to_le32(src->io_ticks);
799 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
800 dst->slavecount = cpu_to_le32(src->slavecount);
801 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
802}
803
804static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
805{
806 int i;
807
808 strcpy((char *) dst->name, (char *) src->name);
809
810 for (i = 0; i < 2; i++) {
811 dst->ios[i] = cpu_to_le32(src->ios[i]);
812 dst->merges[i] = cpu_to_le32(src->merges[i]);
813 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
814 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
815 }
816
817 dst->io_ticks = cpu_to_le32(src->io_ticks);
818 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
819 dst->msec = cpu_to_le64(src->msec);
820}
821
822void fio_server_send_du(void)
823{
824 struct disk_util *du;
825 struct flist_head *entry;
826 struct cmd_du_pdu pdu;
827
828 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
829
Jens Axboe0766d922011-10-13 12:02:08 +0200830 memset(&pdu, 0, sizeof(pdu));
831
Jens Axboed09a64a2011-10-13 11:38:56 +0200832 flist_for_each(entry, &disk_list) {
833 du = flist_entry(entry, struct disk_util, list);
834
835 convert_dus(&pdu.dus, &du->dus);
836 convert_agg(&pdu.agg, &du->agg);
837
838 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
839 }
840}
841
Jens Axboe807f9972012-03-02 10:25:24 +0100842void fio_server_send_add_job(struct thread_options *o, const char *ioengine)
843{
844 struct cmd_add_job_pdu pdu;
Jens Axboe807f9972012-03-02 10:25:24 +0100845
Jens Axboedcaeb602012-03-08 19:45:37 +0100846 convert_thread_options_to_net(&pdu.top, o);
Jens Axboe807f9972012-03-02 10:25:24 +0100847
848 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), 0);
849}
850
Jens Axboe87aa8f12011-10-06 21:24:13 +0200851static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200852{
Jens Axboe811826b2011-10-24 09:11:50 +0200853 struct sockaddr *addr;
854 fio_socklen_t socklen;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200855 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200856
Jens Axboe811826b2011-10-24 09:11:50 +0200857 if (use_ipv6)
858 sk = socket(AF_INET6, SOCK_STREAM, 0);
859 else
860 sk = socket(AF_INET, SOCK_STREAM, 0);
861
Jens Axboe81179ee2011-10-04 12:42:06 +0200862 if (sk < 0) {
863 log_err("fio: socket: %s\n", strerror(errno));
864 return -1;
865 }
866
867 opt = 1;
868 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
869 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200870 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200871 return -1;
872 }
873#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200874 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200875 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200876 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200877 return -1;
878 }
879#endif
880
Jens Axboe811826b2011-10-24 09:11:50 +0200881 if (use_ipv6) {
882 addr = (struct sockaddr *) &saddr_in6;
883 socklen = sizeof(saddr_in6);
884 saddr_in6.sin6_family = AF_INET6;
885 } else {
886 addr = (struct sockaddr *) &saddr_in;
887 socklen = sizeof(saddr_in);
888 saddr_in.sin_family = AF_INET;
889 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200890
Jens Axboe811826b2011-10-24 09:11:50 +0200891 if (bind(sk, addr, socklen) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200892 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200893 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200894 return -1;
895 }
896
Jens Axboe87aa8f12011-10-06 21:24:13 +0200897 return sk;
898}
899
900static int fio_init_server_sock(void)
901{
902 struct sockaddr_un addr;
903 fio_socklen_t len;
904 mode_t mode;
905 int sk;
906
907 sk = socket(AF_UNIX, SOCK_STREAM, 0);
908 if (sk < 0) {
909 log_err("fio: socket: %s\n", strerror(errno));
910 return -1;
911 }
912
913 mode = umask(000);
914
915 memset(&addr, 0, sizeof(addr));
916 addr.sun_family = AF_UNIX;
917 strcpy(addr.sun_path, bind_sock);
918 unlink(bind_sock);
919
920 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
921
922 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
923 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200924 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200925 return -1;
926 }
927
928 umask(mode);
929 return sk;
930}
931
932static int fio_init_server_connection(void)
933{
Jens Axboebebe6392011-10-07 10:00:51 +0200934 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +0200935 int sk;
936
937 dprint(FD_NET, "starting server\n");
938
939 if (!bind_sock)
940 sk = fio_init_server_ip();
941 else
942 sk = fio_init_server_sock();
943
944 if (sk < 0)
945 return sk;
946
Jens Axboe811826b2011-10-24 09:11:50 +0200947 if (!bind_sock) {
948 char *p, port[16];
949 const void *src;
950 int af;
951
952 if (use_ipv6) {
953 af = AF_INET6;
954 src = &saddr_in6.sin6_addr;
955 } else {
956 af = AF_INET;
957 src = &saddr_in.sin_addr;
958 }
959
960 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
961
962 sprintf(port, ",%u", fio_net_port);
963 if (p)
964 strcat(p, port);
965 else
966 strcpy(bind_str, port);
967 } else
Jens Axboebebe6392011-10-07 10:00:51 +0200968 strcpy(bind_str, bind_sock);
969
970 log_info("fio: server listening on %s\n", bind_str);
971
Jens Axboe89c17072011-10-11 10:15:51 +0200972 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200973 log_err("fio: listen: %s\n", strerror(errno));
974 return -1;
975 }
976
Jens Axboe87aa8f12011-10-06 21:24:13 +0200977 return sk;
978}
979
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100980int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
981 struct in6_addr *inp6)
982
983{
984 int ret = 0;
985
986 if (*ipv6)
987 ret = inet_pton(AF_INET6, host, inp6);
988 else
989 ret = inet_pton(AF_INET, host, inp);
990
991 if (ret != 1) {
992 struct hostent *hent;
993
994 hent = gethostbyname(host);
995 if (!hent) {
996 log_err("fio: failed to resolve <%s>\n", host);
997 return 0;
998 }
999
1000 if (*ipv6) {
1001 if (hent->h_addrtype != AF_INET6) {
1002 log_info("fio: falling back to IPv4\n");
1003 *ipv6 = 0;
1004 } else
1005 memcpy(inp6, hent->h_addr_list[0], 16);
1006 }
1007 if (!*ipv6) {
1008 if (hent->h_addrtype != AF_INET) {
1009 log_err("fio: lookup type mismatch\n");
1010 return 0;
1011 }
1012 memcpy(inp, hent->h_addr_list[0], 4);
1013 }
1014 ret = 1;
1015 }
1016
1017 return !(ret == 1);
1018}
1019
Jens Axboe660a2bf2011-10-24 09:35:06 +02001020/*
1021 * Parse a host/ip/port string. Reads from 'str'.
1022 *
1023 * Outputs:
1024 *
1025 * For IPv4:
1026 * *ptr is the host, *port is the port, inp is the destination.
1027 * For IPv6:
1028 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1029 * For local domain sockets:
1030 * *ptr is the filename, *is_sock is 1.
1031 */
Jens Axboebebe6392011-10-07 10:00:51 +02001032int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001033 int *port, struct in_addr *inp,
1034 struct in6_addr *inp6, int *ipv6)
Jens Axboebebe6392011-10-07 10:00:51 +02001035{
Jens Axboe76867622011-10-25 09:52:51 +02001036 const char *host = str;
1037 char *portp;
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001038 int lport = 0;
Jens Axboe76867622011-10-25 09:52:51 +02001039
Jens Axboebebe6392011-10-07 10:00:51 +02001040 *ptr = NULL;
1041 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +02001042 *port = fio_net_port;
Jens Axboe811826b2011-10-24 09:11:50 +02001043 *ipv6 = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001044
1045 if (!strncmp(str, "sock:", 5)) {
1046 *ptr = strdup(str + 5);
1047 *is_sock = 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001048
Jens Axboe76867622011-10-25 09:52:51 +02001049 return 0;
1050 }
1051
1052 /*
1053 * Is it ip:<ip or host>:port
1054 */
1055 if (!strncmp(host, "ip:", 3))
1056 host += 3;
1057 else if (!strncmp(host, "ip4:", 4))
1058 host += 4;
1059 else if (!strncmp(host, "ip6:", 4)) {
1060 host += 4;
1061 *ipv6 = 1;
1062 } else if (host[0] == ':') {
1063 /* String is :port */
1064 host++;
1065 lport = atoi(host);
1066 if (!lport || lport > 65535) {
1067 log_err("fio: bad server port %u\n", port);
1068 return 1;
1069 }
1070 /* no hostname given, we are done */
1071 *port = lport;
1072 return 0;
1073 }
1074
1075 /*
1076 * If no port seen yet, check if there's a last ':' at the end
1077 */
1078 if (!lport) {
1079 portp = strchr(host, ',');
1080 if (portp) {
1081 *portp = '\0';
1082 portp++;
1083 lport = atoi(portp);
Jens Axboebebe6392011-10-07 10:00:51 +02001084 if (!lport || lport > 65535) {
1085 log_err("fio: bad server port %u\n", port);
1086 return 1;
1087 }
Jens Axboe76867622011-10-25 09:52:51 +02001088 }
1089 }
1090
1091 if (lport)
1092 *port = lport;
1093
1094 if (!strlen(host))
1095 return 0;
1096
1097 *ptr = strdup(host);
1098
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001099 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1100 free(*ptr);
1101 *ptr = NULL;
1102 return 1;
Jens Axboebebe6392011-10-07 10:00:51 +02001103 }
1104
1105 if (*port == 0)
1106 *port = fio_net_port;
1107
1108 return 0;
1109}
1110
Jens Axboe87aa8f12011-10-06 21:24:13 +02001111/*
1112 * Server arg should be one of:
1113 *
1114 * sock:/path/to/socket
1115 * ip:1.2.3.4
1116 * 1.2.3.4
1117 *
1118 * Where sock uses unix domain sockets, and ip binds the server to
1119 * a specific interface. If no arguments are given to the server, it
1120 * uses IP and binds to 0.0.0.0.
1121 *
1122 */
1123static int fio_handle_server_arg(void)
1124{
Jens Axboe6d2cf392011-10-07 13:19:28 +02001125 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +02001126 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +02001127
Jens Axboe87aa8f12011-10-06 21:24:13 +02001128 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1129
1130 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +02001131 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001132
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001133 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe811826b2011-10-24 09:11:50 +02001134 &port, &saddr_in.sin_addr,
1135 &saddr_in6.sin6_addr, &use_ipv6);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001136
1137 if (!is_sock && bind_sock) {
1138 free(bind_sock);
1139 bind_sock = NULL;
1140 }
1141
Jens Axboea7de0a12011-10-07 12:55:14 +02001142out:
Jens Axboe6d2cf392011-10-07 13:19:28 +02001143 fio_net_port = port;
1144 saddr_in.sin_port = htons(port);
Jens Axboe811826b2011-10-24 09:11:50 +02001145 saddr_in6.sin6_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +02001146 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +02001147}
1148
1149static int fio_server(void)
1150{
1151 int sk, ret;
1152
1153 dprint(FD_NET, "starting server\n");
1154
1155 if (fio_handle_server_arg())
1156 return -1;
1157
1158 sk = fio_init_server_connection();
1159 if (sk < 0)
1160 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001161
1162 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001163
Jens Axboe81179ee2011-10-04 12:42:06 +02001164 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001165
1166 if (fio_server_arg) {
1167 free(fio_server_arg);
1168 fio_server_arg = NULL;
1169 }
Jens Axboebebe6392011-10-07 10:00:51 +02001170 if (bind_sock)
1171 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +02001172
Jens Axboe81179ee2011-10-04 12:42:06 +02001173 return ret;
1174}
1175
Jens Axboe7b821682011-10-11 12:16:32 +02001176void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +02001177{
Jens Axboe7b821682011-10-11 12:16:32 +02001178 if (signal == SIGPIPE)
1179 server_fd = -1;
1180 else {
1181 log_info("\nfio: terminating on signal %d\n", signal);
1182 exit_backend = 1;
1183 }
Jens Axboe9abea482011-10-04 13:21:54 +02001184}
1185
Jens Axboe13755d92011-10-10 19:51:26 +02001186static int check_existing_pidfile(const char *pidfile)
1187{
1188 struct stat sb;
1189 char buf[16];
1190 pid_t pid;
1191 FILE *f;
1192
1193 if (stat(pidfile, &sb))
1194 return 0;
1195
1196 f = fopen(pidfile, "r");
1197 if (!f)
1198 return 0;
1199
Jens Axboebfc3b172011-10-10 21:16:55 +02001200 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001201 fclose(f);
1202 return 1;
1203 }
1204 fclose(f);
1205
1206 pid = atoi(buf);
1207 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001208 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001209
1210 return 1;
1211}
1212
1213static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001214{
1215 FILE *fpid;
1216
1217 fpid = fopen(pidfile, "w");
1218 if (!fpid) {
1219 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001220 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001221 }
1222
1223 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001224 fclose(fpid);
1225 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001226}
1227
1228/*
1229 * If pidfile is specified, background us.
1230 */
1231int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001232{
1233 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001234 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001235
Bruce Cran93bcfd22012-02-20 20:18:19 +01001236#if defined(WIN32)
1237 WSADATA wsd;
1238 WSAStartup(MAKEWORD(2,2), &wsd);
1239#endif
1240
Jens Axboe402668f2011-10-10 15:28:58 +02001241 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001242 return fio_server();
1243
Jens Axboe13755d92011-10-10 19:51:26 +02001244 if (check_existing_pidfile(pidfile)) {
1245 log_err("fio: pidfile %s exists and server appears alive\n",
1246 pidfile);
1247 return -1;
1248 }
1249
Jens Axboee46d8092011-10-03 09:11:02 +02001250 pid = fork();
1251 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001252 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001253 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001254 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001255 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001256 int ret = write_pid(pid, pidfile);
1257
1258 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001259 }
Jens Axboee46d8092011-10-03 09:11:02 +02001260
1261 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001262 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1263 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001264 close(STDIN_FILENO);
1265 close(STDOUT_FILENO);
1266 close(STDERR_FILENO);
1267 f_out = NULL;
1268 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001269
1270 ret = fio_server();
1271
1272 closelog();
1273 unlink(pidfile);
1274 free(pidfile);
1275 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001276}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001277
Jens Axboebebe6392011-10-07 10:00:51 +02001278void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001279{
1280 fio_server_arg = strdup(arg);
1281}