Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Peter Maydell | 2744d92 | 2016-01-29 17:50:00 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 25 | |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 26 | #include "net/net.h" |
Paolo Bonzini | a245fc1 | 2012-09-17 18:43:51 +0200 | [diff] [blame] | 27 | #include "clients.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 28 | #include "monitor/monitor.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 29 | #include "qapi/error.h" |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 30 | #include "qemu-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 31 | #include "qemu/error-report.h" |
| 32 | #include "qemu/option.h" |
| 33 | #include "qemu/sockets.h" |
| 34 | #include "qemu/iov.h" |
Alex Bligh | 6a1751b | 2013-08-21 16:02:47 +0100 | [diff] [blame] | 35 | #include "qemu/main-loop.h" |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 36 | |
| 37 | typedef struct NetSocketState { |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 38 | NetClientState nc; |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 39 | int listen_fd; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 40 | int fd; |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 41 | SocketReadState rs; |
Stefan Hajnoczi | 45a7f54 | 2012-08-20 10:14:35 +0100 | [diff] [blame] | 42 | unsigned int send_index; /* number of bytes sent (only SOCK_STREAM) */ |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 43 | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
Stefan Hajnoczi | 863f678 | 2012-08-20 10:21:54 +0100 | [diff] [blame] | 44 | IOHandler *send_fn; /* differs between SOCK_STREAM/SOCK_DGRAM */ |
| 45 | bool read_poll; /* waiting to receive data? */ |
| 46 | bool write_poll; /* waiting to transmit data? */ |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 47 | } NetSocketState; |
| 48 | |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 49 | static void net_socket_accept(void *opaque); |
Stefan Hajnoczi | 863f678 | 2012-08-20 10:21:54 +0100 | [diff] [blame] | 50 | static void net_socket_writable(void *opaque); |
| 51 | |
Stefan Hajnoczi | 863f678 | 2012-08-20 10:21:54 +0100 | [diff] [blame] | 52 | static void net_socket_update_fd_handler(NetSocketState *s) |
| 53 | { |
Fam Zheng | 82e1cc4 | 2015-06-04 14:45:18 +0800 | [diff] [blame] | 54 | qemu_set_fd_handler(s->fd, |
| 55 | s->read_poll ? s->send_fn : NULL, |
| 56 | s->write_poll ? net_socket_writable : NULL, |
| 57 | s); |
Stefan Hajnoczi | 863f678 | 2012-08-20 10:21:54 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void net_socket_read_poll(NetSocketState *s, bool enable) |
| 61 | { |
| 62 | s->read_poll = enable; |
| 63 | net_socket_update_fd_handler(s); |
| 64 | } |
| 65 | |
| 66 | static void net_socket_write_poll(NetSocketState *s, bool enable) |
| 67 | { |
| 68 | s->write_poll = enable; |
| 69 | net_socket_update_fd_handler(s); |
| 70 | } |
| 71 | |
| 72 | static void net_socket_writable(void *opaque) |
| 73 | { |
| 74 | NetSocketState *s = opaque; |
| 75 | |
| 76 | net_socket_write_poll(s, false); |
| 77 | |
| 78 | qemu_flush_queued_packets(&s->nc); |
| 79 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 80 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 81 | static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 82 | { |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 83 | NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
Stefan Hajnoczi | 45a7f54 | 2012-08-20 10:14:35 +0100 | [diff] [blame] | 84 | uint32_t len = htonl(size); |
| 85 | struct iovec iov[] = { |
| 86 | { |
| 87 | .iov_base = &len, |
| 88 | .iov_len = sizeof(len), |
| 89 | }, { |
| 90 | .iov_base = (void *)buf, |
| 91 | .iov_len = size, |
| 92 | }, |
| 93 | }; |
| 94 | size_t remaining; |
| 95 | ssize_t ret; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 96 | |
Stefan Hajnoczi | 45a7f54 | 2012-08-20 10:14:35 +0100 | [diff] [blame] | 97 | remaining = iov_size(iov, 2) - s->send_index; |
| 98 | ret = iov_send(s->fd, iov, 2, s->send_index, remaining); |
| 99 | |
| 100 | if (ret == -1 && errno == EAGAIN) { |
| 101 | ret = 0; /* handled further down */ |
| 102 | } |
| 103 | if (ret == -1) { |
| 104 | s->send_index = 0; |
| 105 | return -errno; |
| 106 | } |
| 107 | if (ret < (ssize_t)remaining) { |
| 108 | s->send_index += ret; |
| 109 | net_socket_write_poll(s, true); |
| 110 | return 0; |
| 111 | } |
| 112 | s->send_index = 0; |
| 113 | return size; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 116 | static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 117 | { |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 118 | NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
Stefan Hajnoczi | 213fd50 | 2012-08-20 10:28:53 +0100 | [diff] [blame] | 119 | ssize_t ret; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 120 | |
Stefan Hajnoczi | 213fd50 | 2012-08-20 10:28:53 +0100 | [diff] [blame] | 121 | do { |
Stefan Weil | 73062df | 2012-09-22 21:13:28 +0200 | [diff] [blame] | 122 | ret = qemu_sendto(s->fd, buf, size, 0, |
| 123 | (struct sockaddr *)&s->dgram_dst, |
| 124 | sizeof(s->dgram_dst)); |
Stefan Hajnoczi | 213fd50 | 2012-08-20 10:28:53 +0100 | [diff] [blame] | 125 | } while (ret == -1 && errno == EINTR); |
| 126 | |
| 127 | if (ret == -1 && errno == EAGAIN) { |
| 128 | net_socket_write_poll(s, true); |
| 129 | return 0; |
| 130 | } |
| 131 | return ret; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Fam Zheng | 6e99c63 | 2015-06-04 14:45:16 +0800 | [diff] [blame] | 134 | static void net_socket_send_completed(NetClientState *nc, ssize_t len) |
| 135 | { |
| 136 | NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
| 137 | |
| 138 | if (!s->read_poll) { |
| 139 | net_socket_read_poll(s, true); |
| 140 | } |
| 141 | } |
| 142 | |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 143 | static void net_socket_rs_finalize(SocketReadState *rs) |
| 144 | { |
| 145 | NetSocketState *s = container_of(rs, NetSocketState, rs); |
| 146 | |
| 147 | if (qemu_send_packet_async(&s->nc, rs->buf, |
| 148 | rs->packet_len, |
| 149 | net_socket_send_completed) == 0) { |
| 150 | net_socket_read_poll(s, false); |
| 151 | } |
| 152 | } |
| 153 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 154 | static void net_socket_send(void *opaque) |
| 155 | { |
| 156 | NetSocketState *s = opaque; |
Daniel P. Berrange | b16a44e | 2016-03-07 20:36:03 +0000 | [diff] [blame] | 157 | int size; |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 158 | int ret; |
Scott Feldman | d32fcad | 2013-03-18 11:43:44 -0700 | [diff] [blame] | 159 | uint8_t buf1[NET_BUFSIZE]; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 160 | const uint8_t *buf; |
| 161 | |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 162 | size = qemu_recv(s->fd, buf1, sizeof(buf1), 0); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 163 | if (size < 0) { |
Daniel P. Berrange | b16a44e | 2016-03-07 20:36:03 +0000 | [diff] [blame] | 164 | if (errno != EWOULDBLOCK) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 165 | goto eoc; |
| 166 | } else if (size == 0) { |
| 167 | /* end of connection */ |
| 168 | eoc: |
Stefan Hajnoczi | 863f678 | 2012-08-20 10:21:54 +0100 | [diff] [blame] | 169 | net_socket_read_poll(s, false); |
| 170 | net_socket_write_poll(s, false); |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 171 | if (s->listen_fd != -1) { |
| 172 | qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); |
| 173 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 174 | closesocket(s->fd); |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 175 | |
| 176 | s->fd = -1; |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 177 | net_socket_rs_init(&s->rs, net_socket_rs_finalize, false); |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 178 | s->nc.link_down = true; |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 179 | memset(s->nc.info_str, 0, sizeof(s->nc.info_str)); |
| 180 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | buf = buf1; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 184 | |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 185 | ret = net_fill_rstate(&s->rs, buf, size); |
| 186 | |
| 187 | if (ret == -1) { |
| 188 | goto eoc; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | static void net_socket_send_dgram(void *opaque) |
| 193 | { |
| 194 | NetSocketState *s = opaque; |
| 195 | int size; |
| 196 | |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 197 | size = qemu_recv(s->fd, s->rs.buf, sizeof(s->rs.buf), 0); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 198 | if (size < 0) |
| 199 | return; |
| 200 | if (size == 0) { |
| 201 | /* end of connection */ |
Stefan Hajnoczi | 863f678 | 2012-08-20 10:21:54 +0100 | [diff] [blame] | 202 | net_socket_read_poll(s, false); |
| 203 | net_socket_write_poll(s, false); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 204 | return; |
| 205 | } |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 206 | if (qemu_send_packet_async(&s->nc, s->rs.buf, size, |
Fam Zheng | 6e99c63 | 2015-06-04 14:45:16 +0800 | [diff] [blame] | 207 | net_socket_send_completed) == 0) { |
| 208 | net_socket_read_poll(s, false); |
| 209 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 212 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, |
| 213 | struct in_addr *localaddr, |
| 214 | Error **errp) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 215 | { |
| 216 | struct ip_mreq imr; |
| 217 | int fd; |
| 218 | int val, ret; |
Brad | 23ddf2b | 2011-08-07 11:06:43 +0000 | [diff] [blame] | 219 | #ifdef __OpenBSD__ |
| 220 | unsigned char loop; |
| 221 | #else |
| 222 | int loop; |
| 223 | #endif |
| 224 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 225 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 226 | error_setg(errp, "specified mcastaddr %s (0x%08x) " |
| 227 | "does not contain a multicast address", |
| 228 | inet_ntoa(mcastaddr->sin_addr), |
| 229 | (int)ntohl(mcastaddr->sin_addr.s_addr)); |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 230 | return -1; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 231 | } |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 232 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 233 | fd = qemu_socket(PF_INET, SOCK_DGRAM, 0); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 234 | if (fd < 0) { |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 235 | error_setg_errno(errp, errno, "can't create datagram socket"); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 236 | return -1; |
| 237 | } |
| 238 | |
Sebastian Ottlik | bcbe92f | 2013-10-02 12:23:14 +0200 | [diff] [blame] | 239 | /* Allow multiple sockets to bind the same multicast ip and port by setting |
| 240 | * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set |
| 241 | * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR |
| 242 | * only on posix systems. |
| 243 | */ |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 244 | val = 1; |
Stefan Weil | 9957fc7 | 2013-03-08 19:58:32 +0100 | [diff] [blame] | 245 | ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 246 | if (ret < 0) { |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 247 | error_setg_errno(errp, errno, |
| 248 | "can't set socket option SO_REUSEADDR"); |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 249 | goto fail; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
| 253 | if (ret < 0) { |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 254 | error_setg_errno(errp, errno, "can't bind ip=%s to socket", |
| 255 | inet_ntoa(mcastaddr->sin_addr)); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 256 | goto fail; |
| 257 | } |
| 258 | |
| 259 | /* Add host to multicast group */ |
| 260 | imr.imr_multiaddr = mcastaddr->sin_addr; |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 261 | if (localaddr) { |
| 262 | imr.imr_interface = *localaddr; |
| 263 | } else { |
| 264 | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
| 265 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 266 | |
Stefan Weil | 9957fc7 | 2013-03-08 19:58:32 +0100 | [diff] [blame] | 267 | ret = qemu_setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 268 | &imr, sizeof(struct ip_mreq)); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 269 | if (ret < 0) { |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 270 | error_setg_errno(errp, errno, |
| 271 | "can't add socket to multicast group %s", |
| 272 | inet_ntoa(imr.imr_multiaddr)); |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 273 | goto fail; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ |
Brad | 23ddf2b | 2011-08-07 11:06:43 +0000 | [diff] [blame] | 277 | loop = 1; |
Stefan Weil | 9957fc7 | 2013-03-08 19:58:32 +0100 | [diff] [blame] | 278 | ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
| 279 | &loop, sizeof(loop)); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 280 | if (ret < 0) { |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 281 | error_setg_errno(errp, errno, |
| 282 | "can't force multicast message to loopback"); |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 283 | goto fail; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 286 | /* If a bind address is given, only send packets from that address */ |
| 287 | if (localaddr != NULL) { |
Stefan Weil | 9957fc7 | 2013-03-08 19:58:32 +0100 | [diff] [blame] | 288 | ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
| 289 | localaddr, sizeof(*localaddr)); |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 290 | if (ret < 0) { |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 291 | error_setg_errno(errp, errno, |
| 292 | "can't set the default network send interface"); |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 293 | goto fail; |
| 294 | } |
| 295 | } |
| 296 | |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 297 | qemu_set_nonblock(fd); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 298 | return fd; |
| 299 | fail: |
| 300 | if (fd >= 0) |
| 301 | closesocket(fd); |
| 302 | return -1; |
| 303 | } |
| 304 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 305 | static void net_socket_cleanup(NetClientState *nc) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 306 | { |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 307 | NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 308 | if (s->fd != -1) { |
Stefan Hajnoczi | 863f678 | 2012-08-20 10:21:54 +0100 | [diff] [blame] | 309 | net_socket_read_poll(s, false); |
| 310 | net_socket_write_poll(s, false); |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 311 | close(s->fd); |
| 312 | s->fd = -1; |
| 313 | } |
| 314 | if (s->listen_fd != -1) { |
| 315 | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
| 316 | closesocket(s->listen_fd); |
| 317 | s->listen_fd = -1; |
| 318 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 321 | static NetClientInfo net_dgram_socket_info = { |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 322 | .type = NET_CLIENT_DRIVER_SOCKET, |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 323 | .size = sizeof(NetSocketState), |
| 324 | .receive = net_socket_receive_dgram, |
| 325 | .cleanup = net_socket_cleanup, |
| 326 | }; |
| 327 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 328 | static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 329 | const char *model, |
| 330 | const char *name, |
Jens Freimann | 0f8c289 | 2017-08-08 22:38:57 +0200 | [diff] [blame] | 331 | int fd, int is_connected, |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 332 | const char *mcast, |
| 333 | Error **errp) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 334 | { |
| 335 | struct sockaddr_in saddr; |
| 336 | int newfd; |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 337 | NetClientState *nc; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 338 | NetSocketState *s; |
| 339 | |
| 340 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it |
| 341 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() |
| 342 | * by ONLY ONE process: we must "clone" this dgram socket --jjo |
| 343 | */ |
| 344 | |
Jens Freimann | 0f8c289 | 2017-08-08 22:38:57 +0200 | [diff] [blame] | 345 | if (is_connected && mcast != NULL) { |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 346 | if (parse_host_port(&saddr, mcast, errp) < 0) { |
Jens Freimann | 0f8c289 | 2017-08-08 22:38:57 +0200 | [diff] [blame] | 347 | goto err; |
| 348 | } |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 349 | /* must be bound */ |
| 350 | if (saddr.sin_addr.s_addr == 0) { |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 351 | error_setg(errp, "can't setup multicast destination address"); |
Stefan Hajnoczi | e5d1fca | 2011-12-07 15:01:49 +0000 | [diff] [blame] | 352 | goto err; |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 353 | } |
| 354 | /* clone dgram socket */ |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 355 | newfd = net_socket_mcast_create(&saddr, NULL, errp); |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 356 | if (newfd < 0) { |
Stefan Hajnoczi | e5d1fca | 2011-12-07 15:01:49 +0000 | [diff] [blame] | 357 | goto err; |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 358 | } |
| 359 | /* clone newfd to fd, close newfd */ |
| 360 | dup2(newfd, fd); |
| 361 | close(newfd); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 362 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Stefan Hajnoczi | ab5f3f8 | 2012-07-24 16:35:08 +0100 | [diff] [blame] | 365 | nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name); |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 366 | |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 367 | s = DO_UPCAST(NetSocketState, nc, nc); |
| 368 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 369 | s->fd = fd; |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 370 | s->listen_fd = -1; |
Stefan Hajnoczi | 863f678 | 2012-08-20 10:21:54 +0100 | [diff] [blame] | 371 | s->send_fn = net_socket_send_dgram; |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 372 | net_socket_rs_init(&s->rs, net_socket_rs_finalize, false); |
Stefan Hajnoczi | 863f678 | 2012-08-20 10:21:54 +0100 | [diff] [blame] | 373 | net_socket_read_poll(s, true); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 374 | |
| 375 | /* mcast: save bound address as dst */ |
Jens Freimann | bb160b5 | 2017-11-06 15:05:46 +0100 | [diff] [blame] | 376 | if (is_connected && mcast != NULL) { |
Zhi Yong Wu | e34cde3 | 2012-07-20 14:25:52 +0100 | [diff] [blame] | 377 | s->dgram_dst = saddr; |
Gonglei | 8db804a | 2014-11-20 19:35:01 +0800 | [diff] [blame] | 378 | snprintf(nc->info_str, sizeof(nc->info_str), |
| 379 | "socket: fd=%d (cloned mcast=%s:%d)", |
| 380 | fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 381 | } else { |
| 382 | snprintf(nc->info_str, sizeof(nc->info_str), |
| 383 | "socket: fd=%d", fd); |
Zhi Yong Wu | e34cde3 | 2012-07-20 14:25:52 +0100 | [diff] [blame] | 384 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 385 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 386 | return s; |
Stefan Hajnoczi | e5d1fca | 2011-12-07 15:01:49 +0000 | [diff] [blame] | 387 | |
| 388 | err: |
| 389 | closesocket(fd); |
| 390 | return NULL; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static void net_socket_connect(void *opaque) |
| 394 | { |
| 395 | NetSocketState *s = opaque; |
Stefan Hajnoczi | 863f678 | 2012-08-20 10:21:54 +0100 | [diff] [blame] | 396 | s->send_fn = net_socket_send; |
| 397 | net_socket_read_poll(s, true); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 400 | static NetClientInfo net_socket_info = { |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 401 | .type = NET_CLIENT_DRIVER_SOCKET, |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 402 | .size = sizeof(NetSocketState), |
| 403 | .receive = net_socket_receive, |
| 404 | .cleanup = net_socket_cleanup, |
| 405 | }; |
| 406 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 407 | static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 408 | const char *model, |
| 409 | const char *name, |
| 410 | int fd, int is_connected) |
| 411 | { |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 412 | NetClientState *nc; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 413 | NetSocketState *s; |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 414 | |
Stefan Hajnoczi | ab5f3f8 | 2012-07-24 16:35:08 +0100 | [diff] [blame] | 415 | nc = qemu_new_net_client(&net_socket_info, peer, model, name); |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 416 | |
| 417 | snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd); |
| 418 | |
| 419 | s = DO_UPCAST(NetSocketState, nc, nc); |
| 420 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 421 | s->fd = fd; |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 422 | s->listen_fd = -1; |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 423 | net_socket_rs_init(&s->rs, net_socket_rs_finalize, false); |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 424 | |
Stefan Hajnoczi | 20048d0 | 2013-02-27 15:05:47 +0100 | [diff] [blame] | 425 | /* Disable Nagle algorithm on TCP sockets to reduce latency */ |
| 426 | socket_set_nodelay(fd); |
| 427 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 428 | if (is_connected) { |
| 429 | net_socket_connect(s); |
| 430 | } else { |
| 431 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); |
| 432 | } |
| 433 | return s; |
| 434 | } |
| 435 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 436 | static NetSocketState *net_socket_fd_init(NetClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 437 | const char *model, const char *name, |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 438 | int fd, int is_connected, |
| 439 | const char *mc, Error **errp) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 440 | { |
| 441 | int so_type = -1, optlen=sizeof(so_type); |
| 442 | |
| 443 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
| 444 | (socklen_t *)&optlen)< 0) { |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 445 | error_setg(errp, "can't get socket option SO_TYPE"); |
Stefan Hajnoczi | e5d1fca | 2011-12-07 15:01:49 +0000 | [diff] [blame] | 446 | closesocket(fd); |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 447 | return NULL; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 448 | } |
| 449 | switch(so_type) { |
| 450 | case SOCK_DGRAM: |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 451 | return net_socket_fd_init_dgram(peer, model, name, fd, is_connected, |
| 452 | mc, errp); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 453 | case SOCK_STREAM: |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 454 | return net_socket_fd_init_stream(peer, model, name, fd, is_connected); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 455 | default: |
Markus Armbruster | 8c42dbe | 2018-10-17 10:26:38 +0200 | [diff] [blame] | 456 | error_setg(errp, "socket type=%d for fd=%d must be either" |
| 457 | " SOCK_DGRAM or SOCK_STREAM", so_type, fd); |
Mao Zhongyi | e1b24b6 | 2017-09-04 22:35:37 +0800 | [diff] [blame] | 458 | closesocket(fd); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 459 | } |
| 460 | return NULL; |
| 461 | } |
| 462 | |
| 463 | static void net_socket_accept(void *opaque) |
| 464 | { |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 465 | NetSocketState *s = opaque; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 466 | struct sockaddr_in saddr; |
| 467 | socklen_t len; |
| 468 | int fd; |
| 469 | |
| 470 | for(;;) { |
| 471 | len = sizeof(saddr); |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 472 | fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 473 | if (fd < 0 && errno != EINTR) { |
| 474 | return; |
| 475 | } else if (fd >= 0) { |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 476 | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 477 | break; |
| 478 | } |
| 479 | } |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 480 | |
| 481 | s->fd = fd; |
| 482 | s->nc.link_down = false; |
| 483 | net_socket_connect(s); |
| 484 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
| 485 | "socket: connection from %s:%d", |
| 486 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 489 | static int net_socket_listen_init(NetClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 490 | const char *model, |
| 491 | const char *name, |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 492 | const char *host_str, |
| 493 | Error **errp) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 494 | { |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 495 | NetClientState *nc; |
| 496 | NetSocketState *s; |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 497 | struct sockaddr_in saddr; |
| 498 | int fd, ret; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 499 | |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 500 | if (parse_host_port(&saddr, host_str, errp) < 0) { |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 501 | return -1; |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 502 | } |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 503 | |
| 504 | fd = qemu_socket(PF_INET, SOCK_STREAM, 0); |
| 505 | if (fd < 0) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 506 | error_setg_errno(errp, errno, "can't create stream socket"); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 507 | return -1; |
| 508 | } |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 509 | qemu_set_nonblock(fd); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 510 | |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 511 | socket_set_fast_reuse(fd); |
| 512 | |
| 513 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 514 | if (ret < 0) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 515 | error_setg_errno(errp, errno, "can't bind ip=%s to socket", |
| 516 | inet_ntoa(saddr.sin_addr)); |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 517 | closesocket(fd); |
| 518 | return -1; |
| 519 | } |
| 520 | ret = listen(fd, 0); |
| 521 | if (ret < 0) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 522 | error_setg_errno(errp, errno, "can't listen on socket"); |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 523 | closesocket(fd); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 524 | return -1; |
| 525 | } |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 526 | |
| 527 | nc = qemu_new_net_client(&net_socket_info, peer, model, name); |
| 528 | s = DO_UPCAST(NetSocketState, nc, nc); |
| 529 | s->fd = -1; |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 530 | s->listen_fd = fd; |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 531 | s->nc.link_down = true; |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 532 | net_socket_rs_init(&s->rs, net_socket_rs_finalize, false); |
Zhi Yong Wu | 011de2b | 2012-07-20 14:25:53 +0100 | [diff] [blame] | 533 | |
| 534 | qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 538 | static int net_socket_connect_init(NetClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 539 | const char *model, |
| 540 | const char *name, |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 541 | const char *host_str, |
| 542 | Error **errp) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 543 | { |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 544 | NetSocketState *s; |
| 545 | int fd, connected, ret; |
| 546 | struct sockaddr_in saddr; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 547 | |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 548 | if (parse_host_port(&saddr, host_str, errp) < 0) { |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 549 | return -1; |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 550 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 551 | |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 552 | fd = qemu_socket(PF_INET, SOCK_STREAM, 0); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 553 | if (fd < 0) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 554 | error_setg_errno(errp, errno, "can't create stream socket"); |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 555 | return -1; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 556 | } |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 557 | qemu_set_nonblock(fd); |
Paolo Bonzini | 6160183 | 2016-08-30 14:04:12 +0200 | [diff] [blame] | 558 | |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 559 | connected = 0; |
| 560 | for(;;) { |
| 561 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 562 | if (ret < 0) { |
| 563 | if (errno == EINTR || errno == EWOULDBLOCK) { |
| 564 | /* continue */ |
| 565 | } else if (errno == EINPROGRESS || |
| 566 | errno == EALREADY || |
| 567 | errno == EINVAL) { |
| 568 | break; |
| 569 | } else { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 570 | error_setg_errno(errp, errno, "can't connect socket"); |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 571 | closesocket(fd); |
| 572 | return -1; |
| 573 | } |
| 574 | } else { |
| 575 | connected = 1; |
| 576 | break; |
| 577 | } |
| 578 | } |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 579 | s = net_socket_fd_init(peer, model, name, fd, connected, NULL, errp); |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 580 | if (!s) { |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 581 | return -1; |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 582 | } |
| 583 | |
Daniel P. Berrange | 6701e55 | 2017-05-05 17:23:05 +0100 | [diff] [blame] | 584 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
| 585 | "socket: connect to %s:%d", |
| 586 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 587 | return 0; |
| 588 | } |
| 589 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 590 | static int net_socket_mcast_init(NetClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 591 | const char *model, |
| 592 | const char *name, |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 593 | const char *host_str, |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 594 | const char *localaddr_str, |
| 595 | Error **errp) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 596 | { |
| 597 | NetSocketState *s; |
| 598 | int fd; |
| 599 | struct sockaddr_in saddr; |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 600 | struct in_addr localaddr, *param_localaddr; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 601 | |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 602 | if (parse_host_port(&saddr, host_str, errp) < 0) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 603 | return -1; |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 604 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 605 | |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 606 | if (localaddr_str != NULL) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 607 | if (inet_aton(localaddr_str, &localaddr) == 0) { |
| 608 | error_setg(errp, "localaddr '%s' is not a valid IPv4 address", |
| 609 | localaddr_str); |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 610 | return -1; |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 611 | } |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 612 | param_localaddr = &localaddr; |
| 613 | } else { |
| 614 | param_localaddr = NULL; |
| 615 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 616 | |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 617 | fd = net_socket_mcast_create(&saddr, param_localaddr, errp); |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 618 | if (fd < 0) { |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 619 | return -1; |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 620 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 621 | |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 622 | s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp); |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 623 | if (!s) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 624 | return -1; |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 625 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 626 | |
| 627 | s->dgram_dst = saddr; |
| 628 | |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 629 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 630 | "socket: mcast=%s:%d", |
| 631 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 632 | return 0; |
| 633 | |
| 634 | } |
| 635 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 636 | static int net_socket_udp_init(NetClientState *peer, |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 637 | const char *model, |
| 638 | const char *name, |
| 639 | const char *rhost, |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 640 | const char *lhost, |
| 641 | Error **errp) |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 642 | { |
| 643 | NetSocketState *s; |
Sebastian Ottlik | bcbe92f | 2013-10-02 12:23:14 +0200 | [diff] [blame] | 644 | int fd, ret; |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 645 | struct sockaddr_in laddr, raddr; |
| 646 | |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 647 | if (parse_host_port(&laddr, lhost, errp) < 0) { |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 648 | return -1; |
| 649 | } |
| 650 | |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 651 | if (parse_host_port(&raddr, rhost, errp) < 0) { |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 652 | return -1; |
| 653 | } |
| 654 | |
| 655 | fd = qemu_socket(PF_INET, SOCK_DGRAM, 0); |
| 656 | if (fd < 0) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 657 | error_setg_errno(errp, errno, "can't create datagram socket"); |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 658 | return -1; |
| 659 | } |
Sebastian Ottlik | bcbe92f | 2013-10-02 12:23:14 +0200 | [diff] [blame] | 660 | |
| 661 | ret = socket_set_fast_reuse(fd); |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 662 | if (ret < 0) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 663 | error_setg_errno(errp, errno, |
| 664 | "can't set socket option SO_REUSEADDR"); |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 665 | closesocket(fd); |
| 666 | return -1; |
| 667 | } |
| 668 | ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); |
| 669 | if (ret < 0) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 670 | error_setg_errno(errp, errno, "can't bind ip=%s to socket", |
| 671 | inet_ntoa(laddr.sin_addr)); |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 672 | closesocket(fd); |
| 673 | return -1; |
| 674 | } |
Stefan Hajnoczi | fc13fa0 | 2013-03-27 10:10:44 +0100 | [diff] [blame] | 675 | qemu_set_nonblock(fd); |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 676 | |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 677 | s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp); |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 678 | if (!s) { |
| 679 | return -1; |
| 680 | } |
| 681 | |
| 682 | s->dgram_dst = raddr; |
| 683 | |
| 684 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
| 685 | "socket: udp=%s:%d", |
| 686 | inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port)); |
| 687 | return 0; |
| 688 | } |
| 689 | |
Kővágó, Zoltán | cebea51 | 2016-07-13 21:50:12 -0600 | [diff] [blame] | 690 | int net_init_socket(const Netdev *netdev, const char *name, |
Markus Armbruster | a30ecde | 2015-05-15 13:58:50 +0200 | [diff] [blame] | 691 | NetClientState *peer, Error **errp) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 692 | { |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 693 | const NetdevSocketOptions *sock; |
| 694 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 695 | assert(netdev->type == NET_CLIENT_DRIVER_SOCKET); |
| 696 | sock = &netdev->u.socket; |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 697 | |
Jens Freimann | ff86d57 | 2017-09-27 17:21:18 +0200 | [diff] [blame] | 698 | if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast + |
| 699 | sock->has_udp != 1) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 700 | error_setg(errp, "exactly one of listen=, connect=, mcast= or udp=" |
| 701 | " is required"); |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 702 | return -1; |
| 703 | } |
| 704 | |
| 705 | if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 706 | error_setg(errp, "localaddr= is only valid with mcast= or udp="); |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 707 | return -1; |
| 708 | } |
| 709 | |
| 710 | if (sock->has_fd) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 711 | int fd; |
| 712 | |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 713 | fd = monitor_fd_param(cur_mon, sock->fd, errp); |
Stefan Hajnoczi | fc13fa0 | 2013-03-27 10:10:44 +0100 | [diff] [blame] | 714 | if (fd == -1) { |
| 715 | return -1; |
| 716 | } |
| 717 | qemu_set_nonblock(fd); |
Mao Zhongyi | c37f0bb | 2017-09-04 22:35:38 +0800 | [diff] [blame] | 718 | if (!net_socket_fd_init(peer, "socket", name, fd, 1, sock->mcast, |
| 719 | errp)) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 720 | return -1; |
| 721 | } |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 722 | return 0; |
| 723 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 724 | |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 725 | if (sock->has_listen) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 726 | if (net_socket_listen_init(peer, "socket", name, sock->listen, errp) |
| 727 | < 0) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 728 | return -1; |
| 729 | } |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 730 | return 0; |
| 731 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 732 | |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 733 | if (sock->has_connect) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 734 | if (net_socket_connect_init(peer, "socket", name, sock->connect, errp) |
| 735 | < 0) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 736 | return -1; |
| 737 | } |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 738 | return 0; |
| 739 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 740 | |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 741 | if (sock->has_mcast) { |
| 742 | /* if sock->localaddr is missing, it has been initialized to "all bits |
| 743 | * zero" */ |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 744 | if (net_socket_mcast_init(peer, "socket", name, sock->mcast, |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 745 | sock->localaddr, errp) < 0) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 746 | return -1; |
| 747 | } |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 748 | return 0; |
| 749 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 750 | |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 751 | assert(sock->has_udp); |
| 752 | if (!sock->has_localaddr) { |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 753 | error_setg(errp, "localaddr= is mandatory with udp="); |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 754 | return -1; |
| 755 | } |
Mao Zhongyi | 0522a95 | 2017-09-04 22:35:40 +0800 | [diff] [blame] | 756 | if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr, |
| 757 | errp) < 0) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 758 | return -1; |
| 759 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 760 | return 0; |
| 761 | } |