blob: f393873f44486f949ff397408a31cdd53e431a07 [file] [log] [blame]
Jens Axboe50d16972011-09-29 17:45:28 -06001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <limits.h>
Jens Axboe50d16972011-09-29 17:45:28 -06005#include <errno.h>
6#include <fcntl.h>
7#include <sys/poll.h>
Jens Axboe50d16972011-09-29 17:45:28 -06008#include <sys/types.h>
9#include <sys/wait.h>
Jens Axboe50d16972011-09-29 17:45:28 -060010#include <sys/mman.h>
Jens Axboe50d16972011-09-29 17:45:28 -060011#include <netinet/in.h>
12#include <arpa/inet.h>
13#include <netdb.h>
14
15#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060016#include "server.h"
17#include "crc/crc32.h"
Jens Axboe50d16972011-09-29 17:45:28 -060018
Jens Axboe132159a2011-09-30 15:01:32 -060019int fio_net_port = 8765;
Jens Axboe50d16972011-09-29 17:45:28 -060020
Jens Axboe009b1be2011-09-29 18:27:02 -060021int exit_backend = 0;
22
Jens Axboe132159a2011-09-30 15:01:32 -060023static char *job_buf;
24static unsigned int job_cur_len;
25static unsigned int job_max_len;
26
27int fio_send_data(int sk, const void *p, unsigned int len)
28{
29 do {
30 int ret = send(sk, p, len, 0);
31
32 if (ret > 0) {
33 len -= ret;
34 if (!len)
35 break;
36 p += ret;
37 continue;
38 } else if (!ret)
39 break;
40 else if (errno == EAGAIN || errno == EINTR)
41 continue;
42 } while (!exit_backend);
43
44 if (!len)
45 return 0;
46
47 return 1;
48}
49
50int fio_recv_data(int sk, void *p, unsigned int len)
51{
52 do {
53 int ret = recv(sk, p, len, MSG_WAITALL);
54
55 if (ret > 0) {
56 len -= ret;
57 if (!len)
58 break;
59 p += ret;
60 continue;
61 } else if (!ret)
62 break;
63 else if (errno == EAGAIN || errno == EINTR)
64 continue;
65 } while (!exit_backend);
66
67 if (!len)
68 return 0;
69
70 return -1;
71}
72
73static int verify_convert_cmd(struct fio_net_cmd *cmd)
74{
75 uint32_t crc;
76
77 cmd->cmd_crc32 = le32_to_cpu(cmd->cmd_crc32);
78 cmd->pdu_crc32 = le32_to_cpu(cmd->pdu_crc32);
79
80 crc = crc32(cmd, sizeof(*cmd) - 2 * sizeof(uint32_t));
81 if (crc != cmd->cmd_crc32) {
82 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
83 cmd->cmd_crc32, crc);
84 return 1;
85 }
86
87 cmd->version = le16_to_cpu(cmd->version);
88 cmd->opcode = le16_to_cpu(cmd->opcode);
89 cmd->flags = le32_to_cpu(cmd->flags);
90 cmd->serial = le64_to_cpu(cmd->serial);
91 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
92
93 switch (cmd->version) {
94 case FIO_SERVER_VER1:
95 break;
96 default:
97 log_err("fio: bad server cmd version %d\n", cmd->version);
98 return 1;
99 }
100
101 if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
102 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
103 return 1;
104 }
105
106 return 0;
107}
108
109static struct fio_net_cmd *read_command(int sk)
110{
111 struct fio_net_cmd cmd, *ret = NULL;
112 uint32_t crc;
113
114 if (fio_recv_data(sk, &cmd, sizeof(cmd)))
115 return NULL;
116
117 /* We have a command, verify it and swap if need be */
118 if (verify_convert_cmd(&cmd))
119 return NULL;
120
121 /* Command checks out, alloc real command and fill in */
122 ret = malloc(sizeof(cmd) + cmd.pdu_len);
123 memcpy(ret, &cmd, sizeof(cmd));
124
125 if (!ret->pdu_len)
126 return ret;
127
128 /* There's payload, get it */
129 if (fio_recv_data(sk, (void *) ret + sizeof(*ret), ret->pdu_len)) {
130 free(ret);
131 return NULL;
132 }
133
134 /* Verify payload crc */
135 crc = crc32(ret->payload, ret->pdu_len);
136 if (crc != ret->pdu_crc32) {
137 log_err("fio: server bad crc on payload (got %x, wanted %x)\n",
138 ret->pdu_crc32, crc);
139 free(ret);
140 return NULL;
141 }
142
143
144 return ret;
145}
146
147void fio_net_cmd_crc(struct fio_net_cmd *cmd)
148{
149 uint32_t pdu_len;
150
151 cmd->cmd_crc32 = cpu_to_le32(crc32(cmd,
152 sizeof(*cmd) - 2 * sizeof(uint32_t)));
153
154 pdu_len = le32_to_cpu(cmd->pdu_len);
155 if (pdu_len)
156 cmd->pdu_crc32 = cpu_to_le32(crc32(cmd->payload, pdu_len));
157}
158
159static int send_simple_command(int sk, uint16_t opcode, uint64_t serial)
160{
161 struct fio_net_cmd cmd = {
162 .version = cpu_to_le16(FIO_SERVER_VER1),
163 .opcode = cpu_to_le16(opcode),
164 .serial = cpu_to_le16(serial),
165 };
166
167 fio_net_cmd_crc(&cmd);
168
169 return fio_send_data(sk, &cmd, sizeof(cmd));
170}
171
172/*
173 * Send an ack for this command
174 */
175static int ack_command(int sk, struct fio_net_cmd *cmd)
176{
177 return send_simple_command(sk, FIO_NET_CMD_ACK, cmd->serial);
178}
179
180#if 0
181static int nak_command(int sk, struct fio_net_cmd *cmd)
182{
183 return send_simple_command(sk, FIO_NET_CMD_NAK, cmd->serial);
184}
185#endif
186
187static int handle_cur_job(struct fio_net_cmd *cmd, int done)
188{
189 unsigned int left = job_max_len - job_cur_len;
190 int ret = 0;
191
192 if (left < cmd->pdu_len) {
193 job_buf = realloc(job_buf, job_max_len + 2 * cmd->pdu_len);
194 job_max_len += 2 * cmd->pdu_len;
195 }
196
197 memcpy(job_buf + job_cur_len, cmd->payload, cmd->pdu_len);
198 job_cur_len += cmd->pdu_len;
199
200 if (done) {
201 parse_jobs_ini(job_buf, 1, 0);
202 ret = exec_run();
203 reset_fio_state();
204 free(job_buf);
205 job_buf = NULL;
206 job_cur_len = job_max_len = 0;
207 }
208
209 return ret;
210}
211
212static int handle_command(struct fio_net_cmd *cmd)
213{
214 int ret;
215
216 switch (cmd->opcode) {
217 case FIO_NET_CMD_QUIT:
218 exit_backend = 1;
219 return 1;
220 case FIO_NET_CMD_ACK:
221 return 0;
222 case FIO_NET_CMD_NAK:
223 return 1;
224 case FIO_NET_CMD_JOB:
225 ret = handle_cur_job(cmd, 0);
226 break;
227 case FIO_NET_CMD_JOB_END:
228 ret = handle_cur_job(cmd, 1);
229 break;
230 default:
231 log_err("fio: unknown opcode: %d\n", cmd->opcode);
232 ret = 1;
233 }
234
235 return ret;
236}
237
238static int handle_connection(int sk)
239{
240 struct fio_net_cmd *cmd = NULL;
241 int ret = 0;
242
243 /* read forever */
244 while (!exit_backend) {
245 cmd = read_command(sk);
246 if (!cmd) {
247 ret = 1;
248 break;
249 }
250
251 ret = ack_command(sk, cmd);
252 if (ret)
253 break;
254
255 ret = handle_command(cmd);
256 if (ret)
257 break;
258
259 free(cmd);
260 }
261
262 if (cmd)
263 free(cmd);
264
265 return ret;
266}
267
Jens Axboe50d16972011-09-29 17:45:28 -0600268static int accept_loop(int listen_sk)
269{
270 struct sockaddr addr;
271 unsigned int len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600272 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600273 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600274
Jens Axboe009b1be2011-09-29 18:27:02 -0600275 flags = fcntl(listen_sk, F_GETFL);
276 flags |= O_NONBLOCK;
277 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600278again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600279 pfd.fd = listen_sk;
280 pfd.events = POLLIN;
281 do {
282 ret = poll(&pfd, 1, 100);
283 if (ret < 0) {
284 if (errno == EINTR)
285 break;
286 perror("poll");
287 goto out;
288 } else if (!ret)
289 continue;
290
291 if (pfd.revents & POLLIN)
292 break;
293 } while (!exit_backend);
294
295 if (exit_backend)
296 goto out;
297
Jens Axboe50d16972011-09-29 17:45:28 -0600298 sk = accept(listen_sk, &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600299 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600300 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600301 return -1;
302 }
303
Jens Axboe132159a2011-09-30 15:01:32 -0600304 exitval = handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -0600305
306 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600307
Jens Axboe009b1be2011-09-29 18:27:02 -0600308 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600309 goto again;
310
Jens Axboe009b1be2011-09-29 18:27:02 -0600311out:
Jens Axboe132159a2011-09-30 15:01:32 -0600312 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600313}
314
315int fio_server(void)
316{
317 struct sockaddr_in saddr_in;
318 struct sockaddr addr;
319 unsigned int len;
Jens Axboeafcf7752011-09-29 18:28:46 -0600320 int sk, opt, ret;
Jens Axboe50d16972011-09-29 17:45:28 -0600321
322 sk = socket(AF_INET, SOCK_STREAM, 0);
323 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600324 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600325 return -1;
326 }
327
328 opt = 1;
329 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600330 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600331 return -1;
332 }
333#ifdef SO_REUSEPORT
334 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600335 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600336 return 1;
337 }
338#endif
339
340 saddr_in.sin_family = AF_INET;
341 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
Jens Axboe132159a2011-09-30 15:01:32 -0600342 saddr_in.sin_port = htons(fio_net_port);
Jens Axboe50d16972011-09-29 17:45:28 -0600343
344 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600345 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600346 return -1;
347 }
348
349 if (listen(sk, 1) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600350 log_err("fio: listen: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600351 return -1;
352 }
353
354 len = sizeof(addr);
355 if (getsockname(sk, &addr, &len) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600356 log_err("fio: getsockname: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600357 return -1;
358 }
359
Jens Axboeafcf7752011-09-29 18:28:46 -0600360 ret = accept_loop(sk);
361 close(sk);
362 return ret;
Jens Axboe50d16972011-09-29 17:45:28 -0600363}