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