| 1 |
// Copyright 2022 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
#include <grpc/support/port_platform.h> |
| 15 |
|
| 16 |
#ifdef GPR_WINDOWS |
| 17 |
#include <grpc/support/alloc.h> |
| 18 |
#include <grpc/support/log_windows.h> |
| 19 |
|
| 20 |
#include "src/core/lib/event_engine/executor/executor.h" |
| 21 |
#include "src/core/lib/event_engine/trace.h" |
| 22 |
#include "src/core/lib/event_engine/windows/win_socket.h" |
| 23 |
#include "src/core/lib/gprpp/sync.h" |
| 24 |
#include "src/core/lib/iomgr/error.h" |
| 25 |
|
| 26 |
#if defined(__MSYS__) && defined(GPR_ARCH_64) |
| 27 |
/* Nasty workaround for nasty bug when using the 64 bits msys compiler |
| 28 |
in conjunction with Microsoft Windows headers. */ |
| 29 |
#define GRPC_FIONBIO _IOW('f', 126, uint32_t) |
| 30 |
#else |
| 31 |
#define GRPC_FIONBIO FIONBIO |
| 32 |
#endif |
| 33 |
|
| 34 |
namespace grpc_event_engine { |
| 35 |
namespace experimental { |
| 36 |
|
| 37 |
WinSocket::WinSocket(SOCKET socket, Executor* executor) noexcept |
| 38 |
: socket_(socket), |
| 39 |
executor_(executor), |
| 40 |
read_info_(OpState(this)), |
| 41 |
write_info_(OpState(this)) {} |
| 42 |
|
| 43 |
WinSocket::~WinSocket() { GPR_ASSERT(is_shutdown_.load()); } |
| 44 |
|
| 45 |
SOCKET WinSocket::socket() { return socket_; } |
| 46 |
|
| 47 |
void WinSocket::MaybeShutdown(absl::Status why) { |
| 48 |
// if already shutdown, return early. Otherwise, set the shutdown flag. |
| 49 |
if (is_shutdown_.exchange(true)) { |
| 50 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_event_engine_trace)) { |
| 51 |
gpr_log(GPR_DEBUG, "WinSocket::%p already shutting down", this); |
| 52 |
} |
| 53 |
return; |
| 54 |
} |
| 55 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_event_engine_trace)) { |
| 56 |
gpr_log(GPR_DEBUG, "WinSocket::%p shutting down now. Reason: %s", this, |
| 57 |
why.ToString().c_str()); |
| 58 |
} |
| 59 |
// Grab the function pointer for DisconnectEx for that specific socket. |
| 60 |
// It may change depending on the interface. |
| 61 |
GUID guid = WSAID_DISCONNECTEX; |
| 62 |
LPFN_DISCONNECTEX DisconnectEx; |
| 63 |
DWORD ioctl_num_bytes; |
| 64 |
int status = WSAIoctl(socket_, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, |
| 65 |
sizeof(guid), &DisconnectEx, sizeof(DisconnectEx), |
| 66 |
&ioctl_num_bytes, NULL, NULL); |
| 67 |
|
| 68 |
if (status == 0) { |
| 69 |
DisconnectEx(socket_, NULL, 0, 0); |
| 70 |
} else { |
| 71 |
char* utf8_message = gpr_format_message(WSAGetLastError()); |
| 72 |
gpr_log(GPR_INFO, "Unable to retrieve DisconnectEx pointer : %s", |
| 73 |
utf8_message); |
| 74 |
gpr_free(utf8_message); |
| 75 |
} |
| 76 |
closesocket(socket_); |
| 77 |
} |
| 78 |
|
| 79 |
void WinSocket::NotifyOnReady(OpState& info, EventEngine::Closure* closure) { |
| 80 |
if (IsShutdown()) { |
| 81 |
info.SetError(WSAESHUTDOWN); |
| 82 |
executor_->Run(closure); |
| 83 |
return; |
| 84 |
}; |
| 85 |
if (std::exchange(info.has_pending_iocp_, false)) { |
| 86 |
executor_->Run(closure); |
| 87 |
} else { |
| 88 |
info.closure_ = closure; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
void WinSocket::NotifyOnRead(EventEngine::Closure* on_read) { |
| 93 |
NotifyOnReady(read_info_, on_read); |
| 94 |
} |
| 95 |
|
| 96 |
void WinSocket::NotifyOnWrite(EventEngine::Closure* on_write) { |
| 97 |
NotifyOnReady(write_info_, on_write); |
| 98 |
} |
| 99 |
|
| 100 |
WinSocket::OpState::OpState(WinSocket* win_socket) noexcept |
| 101 |
: win_socket_(win_socket), closure_(nullptr) {} |
| 102 |
|
| 103 |
void WinSocket::OpState::SetReady() { |
| 104 |
GPR_ASSERT(!has_pending_iocp_); |
| 105 |
if (closure_) { |
| 106 |
win_socket_->executor_->Run(closure_); |
| 107 |
} else { |
| 108 |
has_pending_iocp_ = true; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void WinSocket::OpState::SetError(int wsa_error) { |
| 113 |
bytes_transferred_ = 0; |
| 114 |
wsa_error_ = wsa_error; |
| 115 |
} |
| 116 |
|
| 117 |
void WinSocket::OpState::GetOverlappedResult() { |
| 118 |
DWORD flags = 0; |
| 119 |
DWORD bytes; |
| 120 |
BOOL success = WSAGetOverlappedResult(win_socket_->socket(), &overlapped_, |
| 121 |
&bytes, FALSE, &flags); |
| 122 |
bytes_transferred_ = bytes; |
| 123 |
wsa_error_ = success ? 0 : WSAGetLastError(); |
| 124 |
} |
| 125 |
|
| 126 |
void WinSocket::SetReadable() { read_info_.SetReady(); } |
| 127 |
|
| 128 |
void WinSocket::SetWritable() { write_info_.SetReady(); } |
| 129 |
|
| 130 |
bool WinSocket::IsShutdown() { return is_shutdown_.load(); } |
| 131 |
|
| 132 |
WinSocket::OpState* WinSocket::GetOpInfoForOverlapped(OVERLAPPED* overlapped) { |
| 133 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_event_engine_trace)) { |
| 134 |
gpr_log(GPR_DEBUG, |
| 135 |
"WinSocket::%p looking for matching OVERLAPPED::%p. " |
| 136 |
"read(%p) write(%p)", |
| 137 |
this, overlapped, &read_info_.overlapped_, |
| 138 |
&write_info_.overlapped_); |
| 139 |
} |
| 140 |
if (overlapped == &read_info_.overlapped_) return &read_info_; |
| 141 |
if (overlapped == &write_info_.overlapped_) return &write_info_; |
| 142 |
return nullptr; |
| 143 |
} |
| 144 |
|
| 145 |
namespace { |
| 146 |
|
| 147 |
grpc_error_handle grpc_tcp_set_non_block(SOCKET sock) { |
| 148 |
int status; |
| 149 |
uint32_t param = 1; |
| 150 |
DWORD ret; |
| 151 |
status = WSAIoctl(sock, GRPC_FIONBIO, ¶m, sizeof(param), NULL, 0, &ret, |
| 152 |
NULL, NULL); |
| 153 |
return status == 0 |
| 154 |
? absl::OkStatus() |
| 155 |
: GRPC_WSA_ERROR(WSAGetLastError(), "WSAIoctl(GRPC_FIONBIO)"); |
| 156 |
} |
| 157 |
|
| 158 |
static grpc_error_handle set_dualstack(SOCKET sock) { |
| 159 |
int status; |
| 160 |
DWORD param = 0; |
| 161 |
status = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)¶m, |
| 162 |
sizeof(param)); |
| 163 |
return status == 0 |
| 164 |
? absl::OkStatus() |
| 165 |
: GRPC_WSA_ERROR(WSAGetLastError(), "setsockopt(IPV6_V6ONLY)"); |
| 166 |
} |
| 167 |
|
| 168 |
static grpc_error_handle enable_socket_low_latency(SOCKET sock) { |
| 169 |
int status; |
| 170 |
BOOL param = TRUE; |
| 171 |
status = ::setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, |
| 172 |
reinterpret_cast<char*>(¶m), sizeof(param)); |
| 173 |
if (status == SOCKET_ERROR) { |
| 174 |
status = WSAGetLastError(); |
| 175 |
} |
| 176 |
return status == 0 ? absl::OkStatus() |
| 177 |
: GRPC_WSA_ERROR(status, "setsockopt(TCP_NODELAY)"); |
| 178 |
} |
| 179 |
|
| 180 |
} // namespace |
| 181 |
|
| 182 |
absl::Status PrepareSocket(SOCKET sock) { |
| 183 |
absl::Status err; |
| 184 |
err = grpc_tcp_set_non_block(sock); |
| 185 |
if (!err.ok()) return err; |
| 186 |
err = enable_socket_low_latency(sock); |
| 187 |
if (!err.ok()) return err; |
| 188 |
err = set_dualstack(sock); |
| 189 |
if (!err.ok()) return err; |
| 190 |
return absl::OkStatus(); |
| 191 |
} |
| 192 |
|
| 193 |
} // namespace experimental |
| 194 |
} // namespace grpc_event_engine |
| 195 |
|
| 196 |
#endif // GPR_WINDOWS |