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