Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1 | /* |
Jens Axboe | d4f12dd | 2007-02-08 12:59:02 +0100 | [diff] [blame] | 2 | * Transfer data over the net. |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <unistd.h> |
| 7 | #include <errno.h> |
| 8 | #include <assert.h> |
| 9 | #include <netinet/in.h> |
| 10 | #include <arpa/inet.h> |
| 11 | #include <netdb.h> |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 12 | #include <sys/poll.h> |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 13 | |
| 14 | #include "../fio.h" |
| 15 | #include "../os.h" |
| 16 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 17 | #define send_to_net(td) ((td)->io_ops->priv) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 18 | |
| 19 | static int fio_netio_prep(struct thread_data *td, struct io_u *io_u) |
| 20 | { |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 21 | struct fio_file *f = io_u->file; |
| 22 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 23 | /* |
| 24 | * Make sure we don't see spurious reads to a receiver, and vice versa |
| 25 | */ |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 26 | if ((send_to_net(td) && io_u->ddir == DDIR_READ) || |
| 27 | (!send_to_net(td) && io_u->ddir == DDIR_WRITE)) { |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 28 | td_verror(td, EINVAL); |
| 29 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 30 | } |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 31 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 32 | if (io_u->ddir == DDIR_SYNC) |
| 33 | return 0; |
| 34 | if (io_u->offset == f->last_completed_pos) |
| 35 | return 0; |
| 36 | |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 37 | /* |
| 38 | * If offset is different from last end position, it's a seek. |
| 39 | * As network io is purely sequential, we don't allow seeks. |
| 40 | */ |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 41 | td_verror(td, EINVAL); |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | static int fio_netio_queue(struct thread_data *td, struct io_u *io_u) |
| 46 | { |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 47 | struct fio_file *f = io_u->file; |
Jens Axboe | d4f12dd | 2007-02-08 12:59:02 +0100 | [diff] [blame] | 48 | int ret, flags = 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 49 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 50 | if (io_u->ddir == DDIR_WRITE) { |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 51 | /* |
| 52 | * if we are going to write more, set MSG_MORE |
| 53 | */ |
| 54 | if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < |
| 55 | td->io_size) |
| 56 | flags = MSG_MORE; |
| 57 | |
| 58 | ret = send(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags); |
Jens Axboe | d4f12dd | 2007-02-08 12:59:02 +0100 | [diff] [blame] | 59 | } else if (io_u->ddir == DDIR_READ) { |
| 60 | flags = MSG_WAITALL; |
| 61 | ret = recv(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags); |
| 62 | } else |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 63 | ret = 0; /* must be a SYNC */ |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 64 | |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 65 | if (ret != (int) io_u->xfer_buflen) { |
Jens Axboe | 22819ec | 2007-02-18 07:47:14 +0100 | [diff] [blame^] | 66 | if (ret >= 0) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 67 | io_u->resid = io_u->xfer_buflen - ret; |
| 68 | io_u->error = 0; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 69 | return FIO_Q_COMPLETED; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 70 | } else |
| 71 | io_u->error = errno; |
| 72 | } |
| 73 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 74 | if (io_u->error) |
Jens Axboe | 95bcd81 | 2007-02-11 01:01:57 +0100 | [diff] [blame] | 75 | td_verror(td, io_u->error); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 76 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 77 | return FIO_Q_COMPLETED; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static int fio_netio_setup_connect(struct thread_data *td, const char *host, |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 81 | unsigned short port) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 82 | { |
| 83 | struct sockaddr_in addr; |
| 84 | struct fio_file *f; |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 85 | int i; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 86 | |
| 87 | memset(&addr, 0, sizeof(addr)); |
| 88 | addr.sin_family = AF_INET; |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 89 | addr.sin_port = htons(port); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 90 | |
| 91 | if (inet_aton(host, &addr.sin_addr) != 1) { |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 92 | struct hostent *hent; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 93 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 94 | hent = gethostbyname(host); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 95 | if (!hent) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 96 | td_verror(td, errno); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | memcpy(&addr.sin_addr, hent->h_addr, 4); |
| 101 | } |
| 102 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 103 | for_each_file(td, f, i) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 104 | f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 105 | if (f->fd < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 106 | td_verror(td, errno); |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 107 | return 1; |
| 108 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 109 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 110 | if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 111 | td_verror(td, errno); |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 112 | return 1; |
| 113 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | |
| 118 | } |
| 119 | |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 120 | static int fio_netio_accept_connections(struct thread_data *td, int fd, |
| 121 | struct sockaddr_in *addr) |
| 122 | { |
| 123 | socklen_t socklen = sizeof(*addr); |
| 124 | unsigned int accepts = 0; |
| 125 | struct pollfd pfd; |
| 126 | |
| 127 | fprintf(f_out, "fio: waiting for %u connections\n", td->nr_files); |
| 128 | |
| 129 | /* |
| 130 | * Accept loop. poll for incoming events, accept them. Repeat until we |
| 131 | * have all connections. |
| 132 | */ |
| 133 | while (!td->terminate && accepts < td->nr_files) { |
| 134 | struct fio_file *f; |
| 135 | int ret, i; |
| 136 | |
| 137 | pfd.fd = fd; |
| 138 | pfd.events = POLLIN; |
| 139 | |
| 140 | ret = poll(&pfd, 1, -1); |
| 141 | if (ret < 0) { |
| 142 | if (errno == EINTR) |
| 143 | continue; |
| 144 | |
| 145 | td_verror(td, errno); |
| 146 | break; |
| 147 | } else if (!ret) |
| 148 | continue; |
| 149 | |
Jens Axboe | 0c09442 | 2007-02-11 04:44:02 +0100 | [diff] [blame] | 150 | /* |
| 151 | * should be impossible |
| 152 | */ |
| 153 | if (!(pfd.revents & POLLIN)) |
| 154 | continue; |
| 155 | |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 156 | for_each_file(td, f, i) { |
| 157 | if (f->fd != -1) |
| 158 | continue; |
| 159 | |
| 160 | f->fd = accept(fd, (struct sockaddr *) addr, &socklen); |
| 161 | if (f->fd < 0) { |
| 162 | td_verror(td, errno); |
| 163 | return 1; |
| 164 | } |
| 165 | accepts++; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 173 | static int fio_netio_setup_listen(struct thread_data *td, unsigned short port) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 174 | { |
| 175 | struct sockaddr_in addr; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 176 | int fd, opt; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 177 | |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 178 | fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 179 | if (fd < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 180 | td_verror(td, errno); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | opt = 1; |
| 185 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 186 | td_verror(td, errno); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 187 | return 1; |
| 188 | } |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 189 | #ifdef SO_REUSEPORT |
| 190 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
| 191 | td_verror(td, errno); |
| 192 | return 1; |
| 193 | } |
| 194 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 195 | |
| 196 | memset(&addr, 0, sizeof(addr)); |
| 197 | addr.sin_family = AF_INET; |
| 198 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 199 | addr.sin_port = htons(port); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 200 | |
| 201 | if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 202 | td_verror(td, errno); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 203 | return 1; |
| 204 | } |
| 205 | if (listen(fd, 1) < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 206 | td_verror(td, errno); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 207 | return 1; |
| 208 | } |
| 209 | |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 210 | return fio_netio_accept_connections(td, fd, &addr); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static int fio_netio_setup(struct thread_data *td) |
| 214 | { |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 215 | char host[64], buf[128]; |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 216 | unsigned short port; |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 217 | struct fio_file *f; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 218 | char *sep; |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 219 | int ret, i; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 220 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 221 | if (!td->total_file_size) { |
| 222 | log_err("fio: need size= set\n"); |
| 223 | return 1; |
| 224 | } |
| 225 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 226 | if (td->iomix) { |
| 227 | log_err("fio: network connections must be read OR write\n"); |
| 228 | return 1; |
| 229 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 230 | |
| 231 | strcpy(buf, td->filename); |
| 232 | |
| 233 | sep = strchr(buf, ':'); |
| 234 | if (!sep) { |
| 235 | log_err("fio: bad network host:port <<%s>>\n", td->filename); |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | *sep = '\0'; |
| 240 | sep++; |
| 241 | strcpy(host, buf); |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 242 | port = atoi(sep); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 243 | |
Jens Axboe | 85eb1d4 | 2007-02-09 09:30:25 +0100 | [diff] [blame] | 244 | if (td->ddir == DDIR_READ) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 245 | send_to_net(td) = 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 246 | ret = fio_netio_setup_listen(td, port); |
| 247 | } else { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 248 | send_to_net(td) = 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 249 | ret = fio_netio_setup_connect(td, host, port); |
| 250 | } |
| 251 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 252 | if (ret) |
| 253 | return ret; |
| 254 | |
| 255 | td->io_size = td->total_file_size; |
| 256 | td->total_io_size = td->io_size; |
| 257 | |
| 258 | for_each_file(td, f, i) { |
| 259 | f->file_size = td->total_file_size / td->nr_files; |
| 260 | f->real_file_size = f->file_size; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 261 | } |
| 262 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 263 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 266 | static struct ioengine_ops ioengine = { |
| 267 | .name = "net", |
| 268 | .version = FIO_IOOPS_VERSION, |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 269 | .prep = fio_netio_prep, |
| 270 | .queue = fio_netio_queue, |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 271 | .setup = fio_netio_setup, |
| 272 | .flags = FIO_SYNCIO | FIO_NETIO, |
| 273 | }; |
| 274 | |
| 275 | static void fio_init fio_netio_register(void) |
| 276 | { |
| 277 | register_ioengine(&ioengine); |
| 278 | } |
| 279 | |
| 280 | static void fio_exit fio_netio_unregister(void) |
| 281 | { |
| 282 | unregister_ioengine(&ioengine); |
| 283 | } |