Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1 | /* |
Jens Axboe | da751ca | 2007-03-14 10:59:33 +0100 | [diff] [blame] | 2 | * net engine |
| 3 | * |
| 4 | * IO engine that reads/writes to/from sockets. |
| 5 | * |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <unistd.h> |
| 10 | #include <errno.h> |
| 11 | #include <assert.h> |
| 12 | #include <netinet/in.h> |
| 13 | #include <arpa/inet.h> |
| 14 | #include <netdb.h> |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 15 | #include <sys/poll.h> |
Jens Axboe | 7292056 | 2008-06-02 12:30:06 +0200 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/socket.h> |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 18 | |
| 19 | #include "../fio.h" |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 20 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 21 | struct netio_data { |
| 22 | int listenfd; |
| 23 | int send_to_net; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 24 | int use_splice; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 25 | int net_protocol; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 26 | int pipes[2]; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 27 | char host[64]; |
| 28 | struct sockaddr_in addr; |
| 29 | }; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 30 | |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 31 | /* |
| 32 | * Return -1 for error and 'nr events' for a positive number |
| 33 | * of events |
| 34 | */ |
| 35 | static int poll_wait(struct thread_data *td, int fd, short events) |
| 36 | { |
| 37 | struct pollfd pfd; |
| 38 | int ret; |
| 39 | |
| 40 | while (!td->terminate) { |
| 41 | pfd.fd = fd; |
| 42 | pfd.events = events; |
| 43 | ret = poll(&pfd, 1, -1); |
| 44 | if (ret < 0) { |
| 45 | if (errno == EINTR) |
| 46 | continue; |
| 47 | |
| 48 | td_verror(td, errno, "poll"); |
| 49 | return -1; |
| 50 | } else if (!ret) |
| 51 | continue; |
| 52 | |
| 53 | break; |
| 54 | } |
| 55 | |
| 56 | if (pfd.revents & events) |
| 57 | return 1; |
| 58 | else if (td->terminate) |
| 59 | return 1; |
| 60 | |
| 61 | return -1; |
| 62 | } |
| 63 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 64 | static int fio_netio_prep(struct thread_data *td, struct io_u *io_u) |
| 65 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 66 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 67 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 68 | /* |
| 69 | * Make sure we don't see spurious reads to a receiver, and vice versa |
| 70 | */ |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 71 | if ((nd->send_to_net && io_u->ddir == DDIR_READ) || |
| 72 | (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 73 | td_verror(td, EINVAL, "bad direction"); |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 74 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 75 | } |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 76 | |
Jens Axboe | f85ac25 | 2008-03-01 18:09:49 +0100 | [diff] [blame] | 77 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 80 | #ifdef FIO_HAVE_SPLICE |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 81 | static int splice_io_u(int fdin, int fdout, unsigned int len) |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 82 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 83 | int bytes = 0; |
| 84 | |
| 85 | while (len) { |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 86 | int ret = splice(fdin, NULL, fdout, NULL, len, 0); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 87 | |
| 88 | if (ret < 0) { |
| 89 | if (!bytes) |
| 90 | bytes = ret; |
| 91 | |
| 92 | break; |
| 93 | } else if (!ret) |
| 94 | break; |
| 95 | |
| 96 | bytes += ret; |
Jens Axboe | f657a2f | 2007-06-22 20:40:10 +0200 | [diff] [blame] | 97 | len -= ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return bytes; |
| 101 | } |
| 102 | |
| 103 | /* |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 104 | * Receive bytes from a socket and fill them into the internal pipe |
| 105 | */ |
| 106 | static int splice_in(struct thread_data *td, struct io_u *io_u) |
| 107 | { |
| 108 | struct netio_data *nd = td->io_ops->data; |
| 109 | |
| 110 | return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen); |
| 111 | } |
| 112 | |
| 113 | /* |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 114 | * Transmit 'len' bytes from the internal pipe |
| 115 | */ |
| 116 | static int splice_out(struct thread_data *td, struct io_u *io_u, |
| 117 | unsigned int len) |
| 118 | { |
| 119 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 120 | |
| 121 | return splice_io_u(nd->pipes[0], io_u->file->fd, len); |
| 122 | } |
| 123 | |
| 124 | static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len) |
| 125 | { |
| 126 | struct iovec iov = { |
| 127 | .iov_base = io_u->xfer_buf, |
| 128 | .iov_len = len, |
| 129 | }; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 130 | int bytes = 0; |
| 131 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 132 | while (iov.iov_len) { |
| 133 | int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 134 | |
| 135 | if (ret < 0) { |
| 136 | if (!bytes) |
| 137 | bytes = ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 138 | break; |
| 139 | } else if (!ret) |
| 140 | break; |
| 141 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 142 | iov.iov_len -= ret; |
| 143 | iov.iov_base += ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 144 | bytes += ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return bytes; |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 148 | |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
| 152 | * vmsplice() pipe to io_u buffer |
| 153 | */ |
| 154 | static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u, |
| 155 | unsigned int len) |
| 156 | { |
| 157 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 158 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 159 | return vmsplice_io_u(io_u, nd->pipes[0], len); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
| 163 | * vmsplice() io_u to pipe |
| 164 | */ |
| 165 | static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u) |
| 166 | { |
| 167 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 168 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 169 | return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 172 | /* |
| 173 | * splice receive - transfer socket data into a pipe using splice, then map |
| 174 | * that pipe data into the io_u using vmsplice. |
| 175 | */ |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 176 | static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u) |
| 177 | { |
| 178 | int ret; |
| 179 | |
| 180 | ret = splice_in(td, io_u); |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 181 | if (ret > 0) |
| 182 | return vmsplice_io_u_out(td, io_u, ret); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 183 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 184 | return ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 187 | /* |
| 188 | * splice transmit - map data from the io_u into a pipe by using vmsplice, |
| 189 | * then transfer that pipe to a socket using splice. |
| 190 | */ |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 191 | static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u) |
| 192 | { |
| 193 | int ret; |
| 194 | |
| 195 | ret = vmsplice_io_u_in(td, io_u); |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 196 | if (ret > 0) |
| 197 | return splice_out(td, io_u, ret); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 198 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 199 | return ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 200 | } |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 201 | #else |
| 202 | static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u) |
| 203 | { |
Jens Axboe | af8771b | 2008-05-30 22:58:28 +0200 | [diff] [blame] | 204 | errno = EOPNOTSUPP; |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u) |
| 209 | { |
Jens Axboe | af8771b | 2008-05-30 22:58:28 +0200 | [diff] [blame] | 210 | errno = EOPNOTSUPP; |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 211 | return -1; |
| 212 | } |
| 213 | #endif |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 214 | |
| 215 | static int fio_netio_send(struct thread_data *td, struct io_u *io_u) |
| 216 | { |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 217 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 218 | int ret, flags = 0; |
| 219 | |
| 220 | ret = poll_wait(td, io_u->file->fd, POLLOUT); |
| 221 | if (ret <= 0) |
| 222 | return ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * if we are going to write more, set MSG_MORE |
| 226 | */ |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 227 | #ifdef MSG_MORE |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 228 | if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size) |
| 229 | flags = MSG_MORE; |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 230 | #endif |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 231 | |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 232 | if (nd->net_protocol == IPPROTO_UDP) { |
| 233 | return sendto(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, |
Jens Axboe | 3565182 | 2009-01-19 12:33:51 +0100 | [diff] [blame^] | 234 | 0, &nd->addr, sizeof(nd->addr)); |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 235 | } else { |
| 236 | return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, |
| 237 | flags); |
| 238 | } |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 241 | static int fio_netio_recv(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 242 | { |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 243 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 244 | int ret, flags = MSG_WAITALL; |
| 245 | |
| 246 | ret = poll_wait(td, io_u->file->fd, POLLIN); |
| 247 | if (ret <= 0) |
| 248 | return ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 249 | |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 250 | if (nd->net_protocol == IPPROTO_UDP) { |
| 251 | socklen_t len = sizeof(nd->addr); |
| 252 | |
| 253 | return recvfrom(io_u->file->fd, io_u->xfer_buf, |
| 254 | io_u->xfer_buflen, 0, &nd->addr, &len); |
| 255 | } else { |
| 256 | return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, |
| 257 | flags); |
| 258 | } |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 261 | static int fio_netio_queue(struct thread_data *td, struct io_u *io_u) |
| 262 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 263 | struct netio_data *nd = td->io_ops->data; |
| 264 | int ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 265 | |
Jens Axboe | 7101d9c | 2007-09-12 13:12:39 +0200 | [diff] [blame] | 266 | fio_ro_check(td, io_u); |
| 267 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 268 | if (io_u->ddir == DDIR_WRITE) { |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 269 | if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP) |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 270 | ret = fio_netio_send(td, io_u); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 271 | else |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 272 | ret = fio_netio_splice_out(td, io_u); |
| 273 | } else if (io_u->ddir == DDIR_READ) { |
| 274 | if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP) |
| 275 | ret = fio_netio_recv(td, io_u); |
| 276 | else |
| 277 | ret = fio_netio_splice_in(td, io_u); |
Jens Axboe | d4f12dd | 2007-02-08 12:59:02 +0100 | [diff] [blame] | 278 | } else |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 279 | ret = 0; /* must be a SYNC */ |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 280 | |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 281 | if (ret != (int) io_u->xfer_buflen) { |
Jens Axboe | 22819ec | 2007-02-18 07:47:14 +0100 | [diff] [blame] | 282 | if (ret >= 0) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 283 | io_u->resid = io_u->xfer_buflen - ret; |
| 284 | io_u->error = 0; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 285 | return FIO_Q_COMPLETED; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 286 | } else { |
| 287 | int err = errno; |
| 288 | |
| 289 | if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE) |
| 290 | return FIO_Q_BUSY; |
| 291 | |
| 292 | io_u->error = err; |
| 293 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 296 | if (io_u->error) |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 297 | td_verror(td, io_u->error, "xfer"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 298 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 299 | return FIO_Q_COMPLETED; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 302 | static int fio_netio_connect(struct thread_data *td, struct fio_file *f) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 303 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 304 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 305 | int type; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 306 | |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 307 | if (nd->net_protocol == IPPROTO_TCP) |
| 308 | type = SOCK_STREAM; |
| 309 | else |
| 310 | type = SOCK_DGRAM; |
| 311 | |
| 312 | f->fd = socket(AF_INET, type, nd->net_protocol); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 313 | if (f->fd < 0) { |
| 314 | td_verror(td, errno, "socket"); |
| 315 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 318 | if (nd->net_protocol == IPPROTO_UDP) |
| 319 | return 0; |
| 320 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 321 | if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) { |
| 322 | td_verror(td, errno, "connect"); |
| 323 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 329 | static int fio_netio_accept(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 330 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 331 | struct netio_data *nd = td->io_ops->data; |
| 332 | socklen_t socklen = sizeof(nd->addr); |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 333 | |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 334 | if (nd->net_protocol == IPPROTO_UDP) { |
| 335 | f->fd = nd->listenfd; |
| 336 | return 0; |
| 337 | } |
| 338 | |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 339 | log_info("fio: waiting for connection\n"); |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 340 | |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 341 | if (poll_wait(td, nd->listenfd, POLLIN) < 0) |
| 342 | return 1; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 343 | |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 344 | f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen); |
| 345 | if (f->fd < 0) { |
| 346 | td_verror(td, errno, "accept"); |
| 347 | return 1; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 353 | static int fio_netio_open_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 354 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 355 | if (td_read(td)) |
| 356 | return fio_netio_accept(td, f); |
| 357 | else |
| 358 | return fio_netio_connect(td, f); |
| 359 | } |
| 360 | |
| 361 | static int fio_netio_setup_connect(struct thread_data *td, const char *host, |
| 362 | unsigned short port) |
| 363 | { |
| 364 | struct netio_data *nd = td->io_ops->data; |
| 365 | |
| 366 | nd->addr.sin_family = AF_INET; |
| 367 | nd->addr.sin_port = htons(port); |
| 368 | |
| 369 | if (inet_aton(host, &nd->addr.sin_addr) != 1) { |
| 370 | struct hostent *hent; |
| 371 | |
| 372 | hent = gethostbyname(host); |
| 373 | if (!hent) { |
| 374 | td_verror(td, errno, "gethostbyname"); |
| 375 | return 1; |
| 376 | } |
| 377 | |
| 378 | memcpy(&nd->addr.sin_addr, hent->h_addr, 4); |
| 379 | } |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static int fio_netio_setup_listen(struct thread_data *td, short port) |
| 385 | { |
| 386 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 387 | int fd, opt, type; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 388 | |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 389 | if (nd->net_protocol == IPPROTO_TCP) |
| 390 | type = SOCK_STREAM; |
| 391 | else |
| 392 | type = SOCK_DGRAM; |
| 393 | |
| 394 | fd = socket(AF_INET, type, nd->net_protocol); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 395 | if (fd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 396 | td_verror(td, errno, "socket"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 397 | return 1; |
| 398 | } |
| 399 | |
| 400 | opt = 1; |
| 401 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 402 | td_verror(td, errno, "setsockopt"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 403 | return 1; |
| 404 | } |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 405 | #ifdef SO_REUSEPORT |
| 406 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 407 | td_verror(td, errno, "setsockopt"); |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 408 | return 1; |
| 409 | } |
| 410 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 411 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 412 | nd->addr.sin_family = AF_INET; |
| 413 | nd->addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 414 | nd->addr.sin_port = htons(port); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 415 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 416 | if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 417 | td_verror(td, errno, "bind"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 418 | return 1; |
| 419 | } |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 420 | if (nd->net_protocol == IPPROTO_TCP && listen(fd, 1) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 421 | td_verror(td, errno, "listen"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 422 | return 1; |
| 423 | } |
| 424 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 425 | nd->listenfd = fd; |
| 426 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 429 | static int fio_netio_init(struct thread_data *td) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 430 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 431 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 443662e | 2008-05-30 13:29:03 +0200 | [diff] [blame] | 432 | unsigned int port; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 433 | char host[64], buf[128]; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 434 | char *sep, *portp, *modep; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 435 | int ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 436 | |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 437 | if (td_rw(td)) { |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 438 | log_err("fio: network connections must be read OR write\n"); |
| 439 | return 1; |
| 440 | } |
Jens Axboe | 16d55aa | 2007-05-22 09:21:37 +0200 | [diff] [blame] | 441 | if (td_random(td)) { |
| 442 | log_err("fio: network IO can't be random\n"); |
| 443 | return 1; |
| 444 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 445 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 446 | strcpy(buf, td->o.filename); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 447 | |
Jens Axboe | 9f9214f | 2007-03-13 14:02:16 +0100 | [diff] [blame] | 448 | sep = strchr(buf, '/'); |
Jens Axboe | 443662e | 2008-05-30 13:29:03 +0200 | [diff] [blame] | 449 | if (!sep) |
| 450 | goto bad_host; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 451 | |
| 452 | *sep = '\0'; |
| 453 | sep++; |
| 454 | strcpy(host, buf); |
Jens Axboe | 443662e | 2008-05-30 13:29:03 +0200 | [diff] [blame] | 455 | if (!strlen(host)) |
| 456 | goto bad_host; |
| 457 | |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 458 | modep = NULL; |
| 459 | portp = sep; |
| 460 | sep = strchr(portp, '/'); |
| 461 | if (sep) { |
| 462 | *sep = '\0'; |
| 463 | modep = sep + 1; |
| 464 | } |
| 465 | |
| 466 | port = strtol(portp, NULL, 10); |
Jens Axboe | 443662e | 2008-05-30 13:29:03 +0200 | [diff] [blame] | 467 | if (!port || port > 65535) |
| 468 | goto bad_host; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 469 | |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 470 | if (modep) { |
Jens Axboe | 3f8fc5a | 2009-01-16 13:22:26 +0100 | [diff] [blame] | 471 | if (!strncmp("tcp", modep, strlen(modep)) || |
| 472 | !strncmp("TCP", modep, strlen(modep))) |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 473 | nd->net_protocol = IPPROTO_TCP; |
Jens Axboe | 3f8fc5a | 2009-01-16 13:22:26 +0100 | [diff] [blame] | 474 | else if (!strncmp("udp", modep, strlen(modep)) || |
| 475 | !strncmp("UDP", modep, strlen(modep))) |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 476 | nd->net_protocol = IPPROTO_UDP; |
| 477 | else |
| 478 | goto bad_host; |
| 479 | } else |
| 480 | nd->net_protocol = IPPROTO_TCP; |
| 481 | |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 482 | if (td_read(td)) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 483 | nd->send_to_net = 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 484 | ret = fio_netio_setup_listen(td, port); |
| 485 | } else { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 486 | nd->send_to_net = 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 487 | ret = fio_netio_setup_connect(td, host, port); |
| 488 | } |
| 489 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 490 | return ret; |
Jens Axboe | 443662e | 2008-05-30 13:29:03 +0200 | [diff] [blame] | 491 | bad_host: |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 492 | log_err("fio: bad network host/port/protocol: %s\n", td->o.filename); |
Jens Axboe | 443662e | 2008-05-30 13:29:03 +0200 | [diff] [blame] | 493 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 496 | static void fio_netio_cleanup(struct thread_data *td) |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 497 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 498 | struct netio_data *nd = td->io_ops->data; |
| 499 | |
| 500 | if (nd) { |
Jens Axboe | 64b24cd | 2007-06-24 21:28:39 +0200 | [diff] [blame] | 501 | if (nd->listenfd != -1) |
| 502 | close(nd->listenfd); |
| 503 | if (nd->pipes[0] != -1) |
| 504 | close(nd->pipes[0]); |
| 505 | if (nd->pipes[1] != -1) |
| 506 | close(nd->pipes[1]); |
| 507 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 508 | free(nd); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | static int fio_netio_setup(struct thread_data *td) |
| 513 | { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 514 | struct netio_data *nd; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 515 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 516 | if (!td->io_ops->data) { |
| 517 | nd = malloc(sizeof(*nd));; |
| 518 | |
| 519 | memset(nd, 0, sizeof(*nd)); |
| 520 | nd->listenfd = -1; |
Jens Axboe | 64b24cd | 2007-06-24 21:28:39 +0200 | [diff] [blame] | 521 | nd->pipes[0] = nd->pipes[1] = -1; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 522 | td->io_ops->data = nd; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 523 | } |
| 524 | |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 525 | return 0; |
| 526 | } |
| 527 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 528 | #ifdef FIO_HAVE_SPLICE |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 529 | static int fio_netio_setup_splice(struct thread_data *td) |
| 530 | { |
| 531 | struct netio_data *nd; |
| 532 | |
| 533 | fio_netio_setup(td); |
| 534 | |
| 535 | nd = td->io_ops->data; |
| 536 | if (nd) { |
| 537 | if (pipe(nd->pipes) < 0) |
| 538 | return 1; |
| 539 | |
| 540 | nd->use_splice = 1; |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | return 1; |
| 545 | } |
| 546 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 547 | static struct ioengine_ops ioengine_splice = { |
| 548 | .name = "netsplice", |
| 549 | .version = FIO_IOOPS_VERSION, |
| 550 | .prep = fio_netio_prep, |
| 551 | .queue = fio_netio_queue, |
| 552 | .setup = fio_netio_setup_splice, |
| 553 | .init = fio_netio_init, |
| 554 | .cleanup = fio_netio_cleanup, |
| 555 | .open_file = fio_netio_open_file, |
| 556 | .close_file = generic_close_file, |
| 557 | .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | |
| 558 | FIO_SIGQUIT, |
| 559 | }; |
| 560 | #endif |
| 561 | |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 562 | static struct ioengine_ops ioengine_rw = { |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 563 | .name = "net", |
| 564 | .version = FIO_IOOPS_VERSION, |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 565 | .prep = fio_netio_prep, |
| 566 | .queue = fio_netio_queue, |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 567 | .setup = fio_netio_setup, |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 568 | .init = fio_netio_init, |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 569 | .cleanup = fio_netio_cleanup, |
| 570 | .open_file = fio_netio_open_file, |
| 571 | .close_file = generic_close_file, |
Jens Axboe | ad830ed | 2008-02-18 21:11:24 +0100 | [diff] [blame] | 572 | .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | |
| 573 | FIO_SIGQUIT, |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 574 | }; |
| 575 | |
| 576 | static void fio_init fio_netio_register(void) |
| 577 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 578 | register_ioengine(&ioengine_rw); |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 579 | #ifdef FIO_HAVE_SPLICE |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 580 | register_ioengine(&ioengine_splice); |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 581 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static void fio_exit fio_netio_unregister(void) |
| 585 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 586 | unregister_ioengine(&ioengine_rw); |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 587 | #ifdef FIO_HAVE_SPLICE |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 588 | unregister_ioengine(&ioengine_splice); |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 589 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 590 | } |