Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
| 35 | |
| 36 | #ifdef GPR_POSIX_SOCKET |
| 37 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 38 | #include "src/core/iomgr/tcp_client.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | |
| 40 | #include <errno.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 41 | #include <netinet/in.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include <string.h> |
| 43 | #include <unistd.h> |
| 44 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 45 | #include "src/core/iomgr/alarm.h" |
| 46 | #include "src/core/iomgr/iomgr_posix.h" |
| 47 | #include "src/core/iomgr/pollset_posix.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 48 | #include "src/core/iomgr/sockaddr_utils.h" |
| 49 | #include "src/core/iomgr/socket_utils_posix.h" |
| 50 | #include "src/core/iomgr/tcp_posix.h" |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 51 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | #include <grpc/support/alloc.h> |
| 53 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 54 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | #include <grpc/support/time.h> |
| 56 | |
| 57 | typedef struct { |
| 58 | void (*cb)(void *arg, grpc_endpoint *tcp); |
| 59 | void *cb_arg; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 60 | gpr_mu mu; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 61 | grpc_fd *fd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | gpr_timespec deadline; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 63 | grpc_alarm alarm; |
| 64 | int refs; |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 65 | grpc_iomgr_closure write_closure; |
Craig Tiller | b4973682 | 2015-06-30 08:15:08 -0700 | [diff] [blame] | 66 | grpc_pollset_set *interested_parties; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 67 | char *addr_str; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | } async_connect; |
| 69 | |
Craig Tiller | d209ed0 | 2015-02-13 23:18:15 -0800 | [diff] [blame] | 70 | static int prepare_socket(const struct sockaddr *addr, int fd) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | if (fd < 0) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | goto error; |
| 73 | } |
| 74 | |
| 75 | if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) || |
Craig Tiller | 2da0296 | 2015-05-06 16:14:25 -0700 | [diff] [blame] | 76 | (addr->sa_family != AF_UNIX && !grpc_set_socket_low_latency(fd, 1)) || |
| 77 | !grpc_set_socket_no_sigpipe_if_possible(fd)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd, |
| 79 | strerror(errno)); |
| 80 | goto error; |
| 81 | } |
| 82 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 83 | return 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | |
| 85 | error: |
| 86 | if (fd >= 0) { |
| 87 | close(fd); |
| 88 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 89 | return 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | } |
| 91 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 92 | static void on_alarm(void *acp, int success) { |
| 93 | int done; |
| 94 | async_connect *ac = acp; |
| 95 | gpr_mu_lock(&ac->mu); |
| 96 | if (ac->fd != NULL && success) { |
| 97 | grpc_fd_shutdown(ac->fd); |
| 98 | } |
| 99 | done = (--ac->refs == 0); |
| 100 | gpr_mu_unlock(&ac->mu); |
| 101 | if (done) { |
| 102 | gpr_mu_destroy(&ac->mu); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 103 | gpr_free(ac->addr_str); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 104 | gpr_free(ac); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void on_writable(void *acp, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | async_connect *ac = acp; |
| 110 | int so_error = 0; |
| 111 | socklen_t so_error_size; |
| 112 | int err; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 113 | int fd = ac->fd->fd; |
| 114 | int done; |
| 115 | grpc_endpoint *ep = NULL; |
| 116 | void (*cb)(void *arg, grpc_endpoint *tcp) = ac->cb; |
| 117 | void *cb_arg = ac->cb_arg; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | |
Craig Tiller | 2620536 | 2015-07-21 08:21:57 -0700 | [diff] [blame] | 119 | grpc_alarm_cancel(&ac->alarm); |
| 120 | |
Craig Tiller | 71bf1be | 2015-07-09 14:51:24 -0700 | [diff] [blame] | 121 | gpr_mu_lock(&ac->mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 122 | if (success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 123 | do { |
| 124 | so_error_size = sizeof(so_error); |
| 125 | err = getsockopt(fd, SOL_SOCKET, SO_ERROR, &so_error, &so_error_size); |
| 126 | } while (err < 0 && errno == EINTR); |
| 127 | if (err < 0) { |
| 128 | gpr_log(GPR_ERROR, "getsockopt(ERROR): %s", strerror(errno)); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 129 | goto finish; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 130 | } else if (so_error != 0) { |
| 131 | if (so_error == ENOBUFS) { |
| 132 | /* We will get one of these errors if we have run out of |
| 133 | memory in the kernel for the data structures allocated |
| 134 | when you connect a socket. If this happens it is very |
| 135 | likely that if we wait a little bit then try again the |
| 136 | connection will work (since other programs or this |
| 137 | program will close their network connections and free up |
| 138 | memory). This does _not_ indicate that there is anything |
| 139 | wrong with the server we are connecting to, this is a |
| 140 | local problem. |
| 141 | |
| 142 | If you are looking at this code, then chances are that |
| 143 | your program or another program on the same computer |
| 144 | opened too many network connections. The "easy" fix: |
| 145 | don't do that! */ |
| 146 | gpr_log(GPR_ERROR, "kernel out of buffers"); |
Craig Tiller | 71bf1be | 2015-07-09 14:51:24 -0700 | [diff] [blame] | 147 | gpr_mu_unlock(&ac->mu); |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 148 | grpc_fd_notify_on_write(ac->fd, &ac->write_closure); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 149 | return; |
| 150 | } else { |
ctiller | ccd27fd | 2014-12-11 09:12:02 -0800 | [diff] [blame] | 151 | switch (so_error) { |
| 152 | case ECONNREFUSED: |
| 153 | gpr_log(GPR_ERROR, "socket error: connection refused"); |
| 154 | break; |
| 155 | default: |
| 156 | gpr_log(GPR_ERROR, "socket error: %d", so_error); |
| 157 | break; |
| 158 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 159 | goto finish; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 160 | } |
| 161 | } else { |
Craig Tiller | b4973682 | 2015-06-30 08:15:08 -0700 | [diff] [blame] | 162 | grpc_pollset_set_del_fd(ac->interested_parties, ac->fd); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 163 | ep = grpc_tcp_create(ac->fd, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, |
| 164 | ac->addr_str); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 165 | goto finish; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | } |
| 167 | } else { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 168 | gpr_log(GPR_ERROR, "on_writable failed during connect"); |
| 169 | goto finish; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | abort(); |
| 173 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 174 | finish: |
Craig Tiller | 71bf1be | 2015-07-09 14:51:24 -0700 | [diff] [blame] | 175 | if (ep == NULL) { |
Craig Tiller | b4973682 | 2015-06-30 08:15:08 -0700 | [diff] [blame] | 176 | grpc_pollset_set_del_fd(ac->interested_parties, ac->fd); |
Craig Tiller | 4b678bd | 2015-06-02 16:12:24 -0700 | [diff] [blame] | 177 | grpc_fd_orphan(ac->fd, NULL, "tcp_client_orphan"); |
Craig Tiller | 71bf1be | 2015-07-09 14:51:24 -0700 | [diff] [blame] | 178 | } else { |
| 179 | ac->fd = NULL; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 180 | } |
| 181 | done = (--ac->refs == 0); |
| 182 | gpr_mu_unlock(&ac->mu); |
| 183 | if (done) { |
| 184 | gpr_mu_destroy(&ac->mu); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 185 | gpr_free(ac->addr_str); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 186 | gpr_free(ac); |
| 187 | } |
| 188 | cb(cb_arg, ep); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void grpc_tcp_client_connect(void (*cb)(void *arg, grpc_endpoint *ep), |
Craig Tiller | 8ed24e2 | 2015-05-08 09:59:51 -0700 | [diff] [blame] | 192 | void *arg, grpc_pollset_set *interested_parties, |
| 193 | const struct sockaddr *addr, int addr_len, |
| 194 | gpr_timespec deadline) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 195 | int fd; |
| 196 | grpc_dualstack_mode dsmode; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 197 | int err; |
| 198 | async_connect *ac; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 199 | struct sockaddr_in6 addr6_v4mapped; |
| 200 | struct sockaddr_in addr4_copy; |
Craig Tiller | 9bcc751 | 2015-05-11 14:59:48 -0700 | [diff] [blame] | 201 | grpc_fd *fdobj; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 202 | char *name; |
| 203 | char *addr_str; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 204 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 205 | /* Use dualstack sockets where available. */ |
| 206 | if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) { |
| 207 | addr = (const struct sockaddr *)&addr6_v4mapped; |
| 208 | addr_len = sizeof(addr6_v4mapped); |
| 209 | } |
| 210 | |
| 211 | fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | if (fd < 0) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 213 | gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno)); |
| 214 | } |
| 215 | if (dsmode == GRPC_DSMODE_IPV4) { |
| 216 | /* If we got an AF_INET socket, map the address back to IPv4. */ |
| 217 | GPR_ASSERT(grpc_sockaddr_is_v4mapped(addr, &addr4_copy)); |
| 218 | addr = (struct sockaddr *)&addr4_copy; |
| 219 | addr_len = sizeof(addr4_copy); |
| 220 | } |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 221 | if (!prepare_socket(addr, fd)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 222 | cb(arg, NULL); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | do { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 227 | err = connect(fd, addr, addr_len); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 228 | } while (err < 0 && errno == EINTR); |
| 229 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 230 | addr_str = grpc_sockaddr_to_uri(addr); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 231 | gpr_asprintf(&name, "tcp-client:%s", addr_str); |
| 232 | |
Craig Tiller | 8e0b08a | 2015-06-01 17:04:17 -0700 | [diff] [blame] | 233 | fdobj = grpc_fd_create(fd, name); |
Craig Tiller | 9bcc751 | 2015-05-11 14:59:48 -0700 | [diff] [blame] | 234 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 235 | if (err >= 0) { |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 236 | cb(arg, grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str)); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 237 | goto done; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | if (errno != EWOULDBLOCK && errno != EINPROGRESS) { |
Craig Tiller | 4b678bd | 2015-06-02 16:12:24 -0700 | [diff] [blame] | 241 | gpr_log(GPR_ERROR, "connect error to '%s': %s", addr_str, strerror(errno)); |
| 242 | grpc_fd_orphan(fdobj, NULL, "tcp_client_connect_error"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 243 | cb(arg, NULL); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 244 | goto done; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Craig Tiller | 4b678bd | 2015-06-02 16:12:24 -0700 | [diff] [blame] | 247 | grpc_pollset_set_add_fd(interested_parties, fdobj); |
| 248 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 249 | ac = gpr_malloc(sizeof(async_connect)); |
| 250 | ac->cb = cb; |
| 251 | ac->cb_arg = arg; |
Craig Tiller | 9bcc751 | 2015-05-11 14:59:48 -0700 | [diff] [blame] | 252 | ac->fd = fdobj; |
Craig Tiller | b4973682 | 2015-06-30 08:15:08 -0700 | [diff] [blame] | 253 | ac->interested_parties = interested_parties; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 254 | ac->addr_str = addr_str; |
| 255 | addr_str = NULL; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 256 | gpr_mu_init(&ac->mu); |
| 257 | ac->refs = 2; |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 258 | ac->write_closure.cb = on_writable; |
| 259 | ac->write_closure.cb_arg = ac; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 260 | |
Craig Tiller | 0317b3d | 2015-06-01 21:57:03 -0700 | [diff] [blame] | 261 | gpr_mu_lock(&ac->mu); |
Craig Tiller | 6a7626c | 2015-07-19 22:21:41 -0700 | [diff] [blame] | 262 | grpc_alarm_init(&ac->alarm, gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC), |
| 263 | on_alarm, ac, gpr_now(GPR_CLOCK_MONOTONIC)); |
Craig Tiller | 0317b3d | 2015-06-01 21:57:03 -0700 | [diff] [blame] | 264 | grpc_fd_notify_on_write(ac->fd, &ac->write_closure); |
| 265 | gpr_mu_unlock(&ac->mu); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 266 | |
| 267 | done: |
| 268 | gpr_free(name); |
| 269 | gpr_free(addr_str); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 270 | } |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 271 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 272 | #endif |