• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/iomgr/port.h"
22 
23 #ifdef GRPC_WINSOCK_SOCKET
24 
25 #include <winsock2.h>
26 
27 // must be included after winsock2.h
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log_windows.h>
30 #include <mswsock.h>
31 
32 #include "absl/log/check.h"
33 #include "absl/log/log.h"
34 #include "absl/strings/str_format.h"
35 #include "src/core/lib/iomgr/iocp_windows.h"
36 #include "src/core/lib/iomgr/iomgr_internal.h"
37 #include "src/core/lib/iomgr/pollset.h"
38 #include "src/core/lib/iomgr/pollset_windows.h"
39 #include "src/core/lib/iomgr/sockaddr_windows.h"
40 #include "src/core/lib/iomgr/socket_windows.h"
41 #include "src/core/util/crash.h"
42 
43 static DWORD s_wsa_socket_flags;
44 
grpc_winsocket_create(SOCKET socket,const char * name)45 grpc_winsocket* grpc_winsocket_create(SOCKET socket, const char* name) {
46   grpc_winsocket* r = (grpc_winsocket*)gpr_malloc(sizeof(grpc_winsocket));
47   memset(r, 0, sizeof(grpc_winsocket));
48   r->socket = socket;
49   gpr_mu_init(&r->state_mu);
50   grpc_iomgr_register_object(
51       &r->iomgr_object, absl::StrFormat("%s:socket=0x%p", name, r).c_str());
52   grpc_iocp_add_socket(r);
53   return r;
54 }
55 
grpc_winsocket_wrapped_socket(grpc_winsocket * socket)56 SOCKET grpc_winsocket_wrapped_socket(grpc_winsocket* socket) {
57   return socket->socket;
58 }
59 
60 // Schedule a shutdown of the socket operations. Will call the pending
61 // operations to abort them. We need to do that this way because of the
62 // various callsites of that function, which happens to be in various
63 // mutex hold states, and that'd be unsafe to call them directly.
grpc_winsocket_shutdown(grpc_winsocket * winsocket)64 void grpc_winsocket_shutdown(grpc_winsocket* winsocket) {
65   // Grab the function pointer for DisconnectEx for that specific socket.
66   // It may change depending on the interface.
67   int status;
68   GUID guid = WSAID_DISCONNECTEX;
69   LPFN_DISCONNECTEX DisconnectEx;
70   DWORD ioctl_num_bytes;
71 
72   gpr_mu_lock(&winsocket->state_mu);
73   if (winsocket->shutdown_called) {
74     gpr_mu_unlock(&winsocket->state_mu);
75     return;
76   }
77   winsocket->shutdown_called = true;
78   bool register_shutdown = false;
79   // If there is already a scheduled read closure, run it immediately. This
80   // follows the same semantics applied to posix endpoint which also runs any
81   // already registered closure immediately in the event of a shutdown.
82   if (winsocket->read_info.closure && !winsocket->read_info.has_pending_iocp) {
83     winsocket->read_info.bytes_transferred = 0;
84     winsocket->read_info.wsa_error = WSA_OPERATION_ABORTED;
85     grpc_core::ExecCtx::Run(DEBUG_LOCATION, winsocket->read_info.closure,
86                             absl::OkStatus());
87     // Note that while the read_info.closure closure is run, it is not set to
88     // NULL here. This ensures that the socket cannot get deleted yet until any
89     // pending I/O operations are flushed by the thread executing
90     // grpc_iocp_work. We set read_info.closure_already_executed_at_shutdown to
91     // true so that when the pending read I/O operations are flushed, the
92     // associated closure is not executed in the grpc_socket_became_ready
93     // function.
94     winsocket->read_info.closure_already_executed_at_shutdown = true;
95     register_shutdown = true;
96   }
97 
98   // If there is already a scheduled write closure, run it immediately. This
99   // follows the same semantics applied to posix endpoint which also runs any
100   // already registered closure immediately in the event of a shutdown.
101   if (winsocket->write_info.closure &&
102       !winsocket->write_info.has_pending_iocp) {
103     winsocket->write_info.bytes_transferred = 0;
104     winsocket->write_info.wsa_error = WSA_OPERATION_ABORTED;
105     grpc_core::ExecCtx::Run(DEBUG_LOCATION, winsocket->write_info.closure,
106                             absl::OkStatus());
107     // Note that while the write_info.closure closure is run, it is not set to
108     // NULL here. This ensures that the socket cannot get deleted yet until any
109     // pending I/O operations are flushed by the thread executing
110     // grpc_iocp_work. We set
111     // write_info.closure.closure_already_executed_at_shutdown to true so that
112     // when the pending write I/O operations are flushed, the associated closure
113     // is not executed in the grpc_socket_became_ready function.
114     winsocket->write_info.closure_already_executed_at_shutdown = true;
115     register_shutdown = true;
116   }
117 
118   if (register_shutdown) {
119     // Instruct gRPC to avoid completing any shutdowns until this socket is
120     // cleaned up.
121     grpc_iocp_register_socket_shutdown_socket_locked(winsocket);
122   }
123   gpr_mu_unlock(&winsocket->state_mu);
124 
125   status = WSAIoctl(winsocket->socket, SIO_GET_EXTENSION_FUNCTION_POINTER,
126                     &guid, sizeof(guid), &DisconnectEx, sizeof(DisconnectEx),
127                     &ioctl_num_bytes, NULL, NULL);
128 
129   if (status == 0) {
130     DisconnectEx(winsocket->socket, NULL, 0, 0);
131   } else {
132     char* utf8_message = gpr_format_message(WSAGetLastError());
133     VLOG(2) << "Unable to retrieve DisconnectEx pointer : " << utf8_message;
134     gpr_free(utf8_message);
135   }
136   // Calling closesocket triggers invocation of any pending I/O operations with
137   // ABORTED status.
138   closesocket(winsocket->socket);
139 }
140 
destroy(grpc_winsocket * winsocket)141 static void destroy(grpc_winsocket* winsocket) {
142   grpc_iomgr_unregister_object(&winsocket->iomgr_object);
143   gpr_mu_destroy(&winsocket->state_mu);
144   gpr_free(winsocket);
145 }
146 
check_destroyable(grpc_winsocket * winsocket)147 static bool check_destroyable(grpc_winsocket* winsocket) {
148   return winsocket->destroy_called == true &&
149          winsocket->write_info.closure == NULL &&
150          winsocket->read_info.closure == NULL;
151 }
152 
grpc_winsocket_finish(grpc_winsocket * winsocket)153 void grpc_winsocket_finish(grpc_winsocket* winsocket) {
154   grpc_iocp_finish_socket_shutdown(winsocket);
155   destroy(winsocket);
156 }
157 
grpc_winsocket_destroy(grpc_winsocket * winsocket)158 void grpc_winsocket_destroy(grpc_winsocket* winsocket) {
159   gpr_mu_lock(&winsocket->state_mu);
160   CHECK(!winsocket->destroy_called);
161   winsocket->destroy_called = true;
162   bool should_destroy = check_destroyable(winsocket);
163   gpr_mu_unlock(&winsocket->state_mu);
164   if (should_destroy) {
165     grpc_winsocket_finish(winsocket);
166   }
167 }
168 
169 // Calling notify_on_read or write means either of two things:
170 //-) The IOCP already completed in the background, and we need to call
171 // the callback now.
172 //-) The IOCP hasn't completed yet, and we're queuing it for later.
socket_notify_on_iocp(grpc_winsocket * socket,grpc_closure * closure,grpc_winsocket_callback_info * info)173 static void socket_notify_on_iocp(grpc_winsocket* socket, grpc_closure* closure,
174                                   grpc_winsocket_callback_info* info) {
175   CHECK(info->closure == NULL);
176   gpr_mu_lock(&socket->state_mu);
177   if (info->has_pending_iocp) {
178     info->has_pending_iocp = 0;
179     grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, absl::OkStatus());
180   } else {
181     info->closure = closure;
182   }
183   gpr_mu_unlock(&socket->state_mu);
184 }
185 
grpc_socket_notify_on_write(grpc_winsocket * socket,grpc_closure * closure)186 void grpc_socket_notify_on_write(grpc_winsocket* socket,
187                                  grpc_closure* closure) {
188   socket_notify_on_iocp(socket, closure, &socket->write_info);
189 }
190 
grpc_socket_notify_on_read(grpc_winsocket * socket,grpc_closure * closure)191 void grpc_socket_notify_on_read(grpc_winsocket* socket, grpc_closure* closure) {
192   socket_notify_on_iocp(socket, closure, &socket->read_info);
193 }
194 
grpc_socket_become_ready(grpc_winsocket * socket,grpc_winsocket_callback_info * info)195 bool grpc_socket_become_ready(grpc_winsocket* socket,
196                               grpc_winsocket_callback_info* info) {
197   CHECK(!info->has_pending_iocp);
198   if (info->closure) {
199     // Only run the closure once at shutdown.
200     if (!info->closure_already_executed_at_shutdown) {
201       grpc_core::ExecCtx::Run(DEBUG_LOCATION, info->closure, absl::OkStatus());
202     }
203     info->closure = NULL;
204   } else {
205     info->has_pending_iocp = 1;
206   }
207   return check_destroyable(socket);
208 }
209 
210 static gpr_once g_probe_ipv6_once = GPR_ONCE_INIT;
211 static bool g_ipv6_loopback_available = false;
212 
probe_ipv6_once(void)213 static void probe_ipv6_once(void) {
214   SOCKET s = socket(AF_INET6, SOCK_STREAM, 0);
215   g_ipv6_loopback_available = 0;
216   if (s == INVALID_SOCKET) {
217     VLOG(2) << "Disabling AF_INET6 sockets because socket() failed.";
218   } else {
219     grpc_sockaddr_in6 addr;
220     memset(&addr, 0, sizeof(addr));
221     addr.sin6_family = AF_INET6;
222     addr.sin6_addr.s6_addr[15] = 1;  // [::1]:0
223     if (bind(s, reinterpret_cast<grpc_sockaddr*>(&addr), sizeof(addr)) == 0) {
224       g_ipv6_loopback_available = 1;
225     } else {
226       VLOG(2) << "Disabling AF_INET6 sockets because ::1 is not available.";
227     }
228     closesocket(s);
229   }
230 }
231 
grpc_ipv6_loopback_available(void)232 int grpc_ipv6_loopback_available(void) {
233   gpr_once_init(&g_probe_ipv6_once, probe_ipv6_once);
234   return g_ipv6_loopback_available;
235 }
236 
grpc_get_default_wsa_socket_flags()237 DWORD grpc_get_default_wsa_socket_flags() { return s_wsa_socket_flags; }
238 
grpc_wsa_socket_flags_init()239 void grpc_wsa_socket_flags_init() {
240   s_wsa_socket_flags = WSA_FLAG_OVERLAPPED;
241   // WSA_FLAG_NO_HANDLE_INHERIT may be not supported on the older Windows
242   // versions, see
243   // https://msdn.microsoft.com/en-us/library/windows/desktop/ms742212(v=vs.85).aspx
244   // for details.
245   SOCKET sock = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
246                           s_wsa_socket_flags | WSA_FLAG_NO_HANDLE_INHERIT);
247   if (sock != INVALID_SOCKET) {
248     // Windows 7, Windows 2008 R2 with SP1 or later
249     s_wsa_socket_flags |= WSA_FLAG_NO_HANDLE_INHERIT;
250     closesocket(sock);
251   }
252 }
253 
254 #endif  // GRPC_WINSOCK_SOCKET
255