Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Transfer data over the net. Pretty basic setup, will only support |
| 3 | * 1 file per thread/job. |
| 4 | */ |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
| 9 | #include <assert.h> |
| 10 | #include <netinet/in.h> |
| 11 | #include <arpa/inet.h> |
| 12 | #include <netdb.h> |
| 13 | |
| 14 | #include "../fio.h" |
| 15 | #include "../os.h" |
| 16 | |
| 17 | struct net_data { |
| 18 | int send_to_net; |
| 19 | struct io_u *last_io_u; |
| 20 | }; |
| 21 | |
| 22 | static int fio_netio_getevents(struct thread_data *td, int fio_unused min, |
| 23 | int max, struct timespec fio_unused *t) |
| 24 | { |
| 25 | assert(max <= 1); |
| 26 | |
| 27 | /* |
| 28 | * we can only have one finished io_u for sync io, since the depth |
| 29 | * is always 1 |
| 30 | */ |
| 31 | if (list_empty(&td->io_u_busylist)) |
| 32 | return 0; |
| 33 | |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | static struct io_u *fio_netio_event(struct thread_data *td, int event) |
| 38 | { |
| 39 | struct net_data *nd = td->io_ops->data; |
| 40 | |
| 41 | assert(event == 0); |
| 42 | |
| 43 | return nd->last_io_u; |
| 44 | } |
| 45 | |
| 46 | static int fio_netio_prep(struct thread_data *td, struct io_u *io_u) |
| 47 | { |
| 48 | struct net_data *nd = td->io_ops->data; |
| 49 | struct fio_file *f = io_u->file; |
| 50 | |
| 51 | if (nd->send_to_net) { |
| 52 | if (io_u->ddir == DDIR_READ) { |
| 53 | td_verror(td, EINVAL); |
| 54 | return 1; |
| 55 | } |
| 56 | } else { |
| 57 | if (io_u->ddir == DDIR_WRITE) { |
| 58 | td_verror(td, EINVAL); |
| 59 | return 1; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (io_u->ddir == DDIR_SYNC) |
| 64 | return 0; |
| 65 | if (io_u->offset == f->last_completed_pos) |
| 66 | return 0; |
| 67 | |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 68 | /* |
| 69 | * If offset is different from last end position, it's a seek. |
| 70 | * As network io is purely sequential, we don't allow seeks. |
| 71 | */ |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 72 | td_verror(td, EINVAL); |
| 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | static int fio_netio_queue(struct thread_data *td, struct io_u *io_u) |
| 77 | { |
| 78 | struct net_data *nd = td->io_ops->data; |
| 79 | struct fio_file *f = io_u->file; |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame^] | 80 | int ret = 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 81 | |
| 82 | if (io_u->ddir == DDIR_WRITE) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame^] | 83 | ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 84 | else if (io_u->ddir == DDIR_READ) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame^] | 85 | ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 86 | |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame^] | 87 | if (ret != (int) io_u->xfer_buflen) { |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 88 | if (ret > 0) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame^] | 89 | io_u->resid = io_u->xfer_buflen - ret; |
| 90 | io_u->error = 0; |
| 91 | return ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 92 | } else |
| 93 | io_u->error = errno; |
| 94 | } |
| 95 | |
| 96 | if (!io_u->error) |
| 97 | nd->last_io_u = io_u; |
| 98 | |
| 99 | return io_u->error; |
| 100 | } |
| 101 | |
| 102 | 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] | 103 | unsigned short port) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 104 | { |
| 105 | struct sockaddr_in addr; |
| 106 | struct fio_file *f; |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 107 | int i; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 108 | |
| 109 | memset(&addr, 0, sizeof(addr)); |
| 110 | addr.sin_family = AF_INET; |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 111 | addr.sin_port = htons(port); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 112 | |
| 113 | if (inet_aton(host, &addr.sin_addr) != 1) { |
| 114 | struct hostent *hent = gethostbyname(host); |
| 115 | |
| 116 | if (!hent) { |
| 117 | td_vmsg(td, errno, "gethostbyname"); |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | memcpy(&addr.sin_addr, hent->h_addr, 4); |
| 122 | } |
| 123 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 124 | for_each_file(td, f, i) { |
| 125 | f->fd = socket(AF_INET, SOCK_STREAM, 0); |
| 126 | if (f->fd < 0) { |
| 127 | td_vmsg(td, errno, "socket"); |
| 128 | return 1; |
| 129 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 130 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 131 | if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 132 | td_vmsg(td, errno, "connect"); |
| 133 | return 1; |
| 134 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | |
| 139 | } |
| 140 | |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 141 | 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] | 142 | { |
| 143 | struct sockaddr_in addr; |
| 144 | socklen_t socklen; |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 145 | struct fio_file *f; |
| 146 | int fd, opt, i; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 147 | |
| 148 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 149 | if (fd < 0) { |
| 150 | td_vmsg(td, errno, "socket"); |
| 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | opt = 1; |
| 155 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
| 156 | td_vmsg(td, errno, "setsockopt"); |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | memset(&addr, 0, sizeof(addr)); |
| 161 | addr.sin_family = AF_INET; |
| 162 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 163 | addr.sin_port = htons(port); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 164 | |
| 165 | if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 166 | td_vmsg(td, errno, "bind"); |
| 167 | return 1; |
| 168 | } |
| 169 | if (listen(fd, 1) < 0) { |
| 170 | td_vmsg(td, errno, "listen"); |
| 171 | return 1; |
| 172 | } |
| 173 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 174 | fprintf(f_out, "fio: waiting for %u connections\n", td->nr_files); |
| 175 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 176 | socklen = sizeof(addr); |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 177 | for_each_file(td, f, i) { |
| 178 | f->fd = accept(fd, (struct sockaddr *) &addr, &socklen); |
| 179 | if (f->fd < 0) { |
| 180 | td_vmsg(td, errno, "accept"); |
| 181 | return 1; |
| 182 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int fio_netio_setup(struct thread_data *td) |
| 189 | { |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 190 | char host[64], buf[128]; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 191 | struct net_data *nd; |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 192 | unsigned short port; |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 193 | struct fio_file *f; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 194 | char *sep; |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 195 | int ret, i; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 196 | |
| 197 | /* |
| 198 | * work around for late init call |
| 199 | */ |
| 200 | if (td->io_ops->init(td)) |
| 201 | return 1; |
| 202 | |
| 203 | nd = td->io_ops->data; |
| 204 | |
| 205 | if (td->iomix) { |
| 206 | log_err("fio: network connections must be read OR write\n"); |
| 207 | return 1; |
| 208 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 209 | |
| 210 | strcpy(buf, td->filename); |
| 211 | |
| 212 | sep = strchr(buf, ':'); |
| 213 | if (!sep) { |
| 214 | log_err("fio: bad network host:port <<%s>>\n", td->filename); |
| 215 | return 1; |
| 216 | } |
| 217 | |
| 218 | *sep = '\0'; |
| 219 | sep++; |
| 220 | strcpy(host, buf); |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 221 | port = atoi(sep); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 222 | |
| 223 | if (td->ddir == READ) { |
| 224 | nd->send_to_net = 0; |
| 225 | ret = fio_netio_setup_listen(td, port); |
| 226 | } else { |
| 227 | nd->send_to_net = 1; |
| 228 | ret = fio_netio_setup_connect(td, host, port); |
| 229 | } |
| 230 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 231 | if (ret) |
| 232 | return ret; |
| 233 | |
| 234 | td->io_size = td->total_file_size; |
| 235 | td->total_io_size = td->io_size; |
| 236 | |
| 237 | for_each_file(td, f, i) { |
| 238 | f->file_size = td->total_file_size / td->nr_files; |
| 239 | f->real_file_size = f->file_size; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 242 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static void fio_netio_cleanup(struct thread_data *td) |
| 246 | { |
| 247 | if (td->io_ops->data) { |
| 248 | free(td->io_ops->data); |
| 249 | td->io_ops->data = NULL; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | static int fio_netio_init(struct thread_data *td) |
| 254 | { |
| 255 | struct net_data *nd; |
| 256 | |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 257 | /* |
| 258 | * Hack to work-around the ->setup() function calling init on its |
| 259 | * own, since it needs ->io_ops->data to be set up. |
| 260 | */ |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 261 | if (td->io_ops->data) |
| 262 | return 0; |
| 263 | |
| 264 | nd = malloc(sizeof(*nd)); |
| 265 | nd->last_io_u = NULL; |
| 266 | td->io_ops->data = nd; |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static struct ioengine_ops ioengine = { |
| 271 | .name = "net", |
| 272 | .version = FIO_IOOPS_VERSION, |
| 273 | .init = fio_netio_init, |
| 274 | .prep = fio_netio_prep, |
| 275 | .queue = fio_netio_queue, |
| 276 | .getevents = fio_netio_getevents, |
| 277 | .event = fio_netio_event, |
| 278 | .cleanup = fio_netio_cleanup, |
| 279 | .setup = fio_netio_setup, |
| 280 | .flags = FIO_SYNCIO | FIO_NETIO, |
| 281 | }; |
| 282 | |
| 283 | static void fio_init fio_netio_register(void) |
| 284 | { |
| 285 | register_ioengine(&ioengine); |
| 286 | } |
| 287 | |
| 288 | static void fio_exit fio_netio_unregister(void) |
| 289 | { |
| 290 | unregister_ioengine(&ioengine); |
| 291 | } |