Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <limits.h> |
| 5 | #include <errno.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <sys/poll.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <sys/wait.h> |
| 11 | #include <sys/mman.h> |
| 12 | #include <netinet/in.h> |
| 13 | #include <arpa/inet.h> |
| 14 | #include <netdb.h> |
| 15 | |
| 16 | #include "fio.h" |
| 17 | #include "server.h" |
| 18 | #include "crc/crc32.h" |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 19 | #include "flist.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 20 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 21 | struct fio_client { |
| 22 | struct flist_head list; |
| 23 | struct sockaddr_in addr; |
| 24 | char *hostname; |
| 25 | int fd; |
| 26 | }; |
| 27 | |
| 28 | static FLIST_HEAD(client_list); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 29 | |
| 30 | static struct fio_client *find_client_by_fd(int fd) |
| 31 | { |
| 32 | struct fio_client *client; |
| 33 | struct flist_head *entry; |
| 34 | |
| 35 | flist_for_each(entry, &client_list) { |
| 36 | client = flist_entry(entry, struct fio_client, list); |
| 37 | |
| 38 | if (client->fd == fd) |
| 39 | return client; |
| 40 | } |
| 41 | |
| 42 | return NULL; |
| 43 | } |
| 44 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 45 | #if 0 |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 46 | static struct fio_client *find_client_by_name(const char *name) |
| 47 | { |
| 48 | struct fio_client *client; |
| 49 | struct flist_head *entry; |
| 50 | |
| 51 | flist_for_each(entry, &client_list) { |
| 52 | client = flist_entry(entry, struct fio_client, list); |
| 53 | |
| 54 | if (!strcmp(name, client->hostname)) |
| 55 | return client; |
| 56 | } |
| 57 | |
| 58 | return NULL; |
| 59 | } |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 60 | #endif |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 61 | |
| 62 | static void remove_client(struct fio_client *client) |
| 63 | { |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame^] | 64 | dprint(FD_NET, "removed client <%s>\n", client->hostname); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 65 | flist_del(&client->list); |
| 66 | nr_clients--; |
| 67 | free(client->hostname); |
| 68 | free(client); |
| 69 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 70 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 71 | void fio_client_add(const char *hostname) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 72 | { |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 73 | struct fio_client *client; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 74 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame^] | 75 | dprint(FD_NET, "added client <%s>\n", hostname); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 76 | client = malloc(sizeof(*client)); |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 77 | memset(client, 0, sizeof(*client)); |
| 78 | client->hostname = strdup(hostname); |
| 79 | client->fd = -1; |
| 80 | flist_add(&client->list, &client_list); |
| 81 | nr_clients++; |
| 82 | } |
| 83 | |
| 84 | static int fio_client_connect(struct fio_client *client) |
| 85 | { |
| 86 | int fd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 87 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame^] | 88 | dprint(FD_NET, "connect to host %s\n", client->hostname); |
| 89 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 90 | memset(&client->addr, 0, sizeof(client->addr)); |
| 91 | client->addr.sin_family = AF_INET; |
| 92 | client->addr.sin_port = htons(fio_net_port); |
| 93 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 94 | if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 95 | struct hostent *hent; |
| 96 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 97 | hent = gethostbyname(client->hostname); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 98 | if (!hent) { |
| 99 | log_err("fio: gethostbyname: %s\n", strerror(errno)); |
| 100 | return 1; |
| 101 | } |
| 102 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 103 | memcpy(&client->addr.sin_addr, hent->h_addr, 4); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 107 | if (fd < 0) { |
| 108 | log_err("fio: socket: %s\n", strerror(errno)); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 109 | free(client); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 110 | return 1; |
| 111 | } |
| 112 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 113 | if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 114 | log_err("fio: connect: %s\n", strerror(errno)); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 115 | free(client); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 116 | return 1; |
| 117 | } |
| 118 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 119 | client->fd = fd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 123 | int fio_clients_connect(void) |
| 124 | { |
| 125 | struct fio_client *client; |
| 126 | struct flist_head *entry, *tmp; |
| 127 | int ret; |
| 128 | |
| 129 | flist_for_each_safe(entry, tmp, &client_list) { |
| 130 | client = flist_entry(entry, struct fio_client, list); |
| 131 | |
| 132 | ret = fio_client_connect(client); |
| 133 | if (ret) |
| 134 | remove_client(client); |
| 135 | } |
| 136 | |
| 137 | return !nr_clients; |
| 138 | } |
| 139 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 140 | static int send_file_buf(struct fio_client *client, char *buf, off_t size) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 141 | { |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 142 | return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Send file contents to server backend. We could use sendfile(), but to remain |
| 147 | * more portable lets just read/write the darn thing. |
| 148 | */ |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 149 | static int fio_client_send_ini(struct fio_client *client, const char *filename) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 150 | { |
| 151 | struct stat sb; |
| 152 | char *p, *buf; |
| 153 | off_t len; |
| 154 | int fd, ret; |
| 155 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame^] | 156 | dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname); |
| 157 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 158 | fd = open(filename, O_RDONLY); |
| 159 | if (fd < 0) { |
| 160 | log_err("fio: job file open: %s\n", strerror(errno)); |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | if (fstat(fd, &sb) < 0) { |
| 165 | log_err("fio: job file stat: %s\n", strerror(errno)); |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | buf = malloc(sb.st_size); |
| 170 | |
| 171 | len = sb.st_size; |
| 172 | p = buf; |
| 173 | do { |
| 174 | ret = read(fd, p, len); |
| 175 | if (ret > 0) { |
| 176 | len -= ret; |
| 177 | if (!len) |
| 178 | break; |
| 179 | p += ret; |
| 180 | continue; |
| 181 | } else if (!ret) |
| 182 | break; |
| 183 | else if (errno == EAGAIN || errno == EINTR) |
| 184 | continue; |
| 185 | } while (1); |
| 186 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 187 | ret = send_file_buf(client, buf, sb.st_size); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 188 | free(buf); |
| 189 | return ret; |
| 190 | } |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 191 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 192 | int fio_clients_send_ini(const char *filename) |
| 193 | { |
| 194 | struct fio_client *client; |
| 195 | struct flist_head *entry, *tmp; |
| 196 | |
| 197 | flist_for_each_safe(entry, tmp, &client_list) { |
| 198 | client = flist_entry(entry, struct fio_client, list); |
| 199 | |
| 200 | if (fio_client_send_ini(client, filename)) |
| 201 | remove_client(client); |
| 202 | } |
| 203 | |
| 204 | return !nr_clients; |
| 205 | } |
| 206 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 207 | static int handle_client(struct fio_client *client) |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 208 | { |
| 209 | struct fio_net_cmd *cmd; |
| 210 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 211 | while ((cmd = fio_net_cmd_read(client->fd)) != NULL) { |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame^] | 212 | dprint(FD_NET, "%s: got cmd op %d\n", client->hostname, |
| 213 | cmd->opcode); |
| 214 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 215 | if (cmd->opcode == FIO_NET_CMD_ACK) { |
| 216 | free(cmd); |
| 217 | continue; |
| 218 | } |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 219 | if (cmd->opcode == FIO_NET_CMD_QUIT) { |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 220 | remove_client(client); |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 221 | free(cmd); |
| 222 | break; |
| 223 | } |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 224 | if (cmd->opcode != FIO_NET_CMD_TEXT) { |
| 225 | printf("non text: %d\n", cmd->opcode); |
| 226 | free(cmd); |
| 227 | continue; |
| 228 | } |
Jens Axboe | 49d14c0 | 2011-10-01 00:08:39 -0600 | [diff] [blame] | 229 | fwrite(cmd->payload, cmd->pdu_len, 1, stdout); |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 230 | fflush(stdout); |
| 231 | free(cmd); |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 236 | |
| 237 | int fio_handle_clients(void) |
| 238 | { |
| 239 | struct fio_client *client; |
| 240 | struct flist_head *entry; |
| 241 | struct pollfd *pfds; |
Jens Axboe | 82a4be1 | 2011-10-01 12:40:32 -0600 | [diff] [blame] | 242 | int i, ret = 0; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 243 | |
| 244 | pfds = malloc(nr_clients * sizeof(struct pollfd)); |
| 245 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 246 | while (!exit_backend && nr_clients) { |
Jens Axboe | 82a4be1 | 2011-10-01 12:40:32 -0600 | [diff] [blame] | 247 | i = 0; |
| 248 | flist_for_each(entry, &client_list) { |
| 249 | client = flist_entry(entry, struct fio_client, list); |
| 250 | |
| 251 | pfds[i].fd = client->fd; |
| 252 | pfds[i].events = POLLIN; |
| 253 | i++; |
| 254 | } |
| 255 | |
| 256 | assert(i == nr_clients); |
| 257 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 258 | ret = poll(pfds, nr_clients, 100); |
| 259 | if (ret < 0) { |
| 260 | if (errno == EINTR) |
| 261 | continue; |
| 262 | log_err("fio: poll clients: %s\n", strerror(errno)); |
| 263 | break; |
| 264 | } else if (!ret) |
| 265 | continue; |
| 266 | |
| 267 | for (i = 0; i < nr_clients; i++) { |
| 268 | if (!(pfds[i].revents & POLLIN)) |
| 269 | continue; |
| 270 | |
| 271 | client = find_client_by_fd(pfds[i].fd); |
| 272 | if (!client) { |
| 273 | log_err("fio: unknown client\n"); |
| 274 | continue; |
| 275 | } |
| 276 | handle_client(client); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | free(pfds); |
| 281 | |
| 282 | return 0; |
| 283 | } |