| 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 |
|
| 18 |
#include <chrono> |
| 19 |
|
| 20 |
#include "absl/strings/str_format.h" |
| 21 |
|
| 22 |
#include <grpc/support/alloc.h> |
| 23 |
#include <grpc/support/log_windows.h> |
| 24 |
|
| 25 |
#include "src/core/lib/event_engine/time_util.h" |
| 26 |
#include "src/core/lib/event_engine/trace.h" |
| 27 |
#include "src/core/lib/event_engine/windows/iocp.h" |
| 28 |
#include "src/core/lib/event_engine/windows/win_socket.h" |
| 29 |
|
| 30 |
namespace grpc_event_engine { |
| 31 |
namespace experimental { |
| 32 |
|
| 33 |
IOCP::IOCP(Executor* executor) noexcept |
| 34 |
: executor_(executor), |
| 35 |
iocp_handle_(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, |
| 36 |
(ULONG_PTR)NULL, 0)) { |
| 37 |
GPR_ASSERT(iocp_handle_); |
| 38 |
WSASocketFlagsInit(); |
| 39 |
} |
| 40 |
|
| 41 |
// Shutdown must be called prior to deletion |
| 42 |
IOCP::~IOCP() {} |
| 43 |
|
| 44 |
WinSocket* IOCP::Watch(SOCKET socket) { |
| 45 |
WinSocket* wrapped_socket = new WinSocket(socket, executor_); |
| 46 |
HANDLE ret = |
| 47 |
CreateIoCompletionPort(reinterpret_cast<HANDLE>(socket), iocp_handle_, |
| 48 |
reinterpret_cast<uintptr_t>(wrapped_socket), 0); |
| 49 |
if (!ret) { |
| 50 |
char* utf8_message = gpr_format_message(WSAGetLastError()); |
| 51 |
gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message); |
| 52 |
gpr_free(utf8_message); |
| 53 |
__debugbreak(); |
| 54 |
abort(); |
| 55 |
} |
| 56 |
GPR_ASSERT(ret == iocp_handle_); |
| 57 |
return wrapped_socket; |
| 58 |
} |
| 59 |
|
| 60 |
void IOCP::Shutdown() { |
| 61 |
while (outstanding_kicks_.load() > 0) { |
| 62 |
Work(std::chrono::hours(42), []() {}); |
| 63 |
} |
| 64 |
GPR_ASSERT(CloseHandle(iocp_handle_)); |
| 65 |
} |
| 66 |
|
| 67 |
Poller::WorkResult IOCP::Work(EventEngine::Duration timeout, |
| 68 |
absl::FunctionRef<void()> schedule_poll_again) { |
| 69 |
static const absl::Status kDeadlineExceeded = absl::DeadlineExceededError( |
| 70 |
absl::StrFormat("IOCP::%p: Received no completions", this)); |
| 71 |
static const absl::Status kKicked = |
| 72 |
absl::AbortedError(absl::StrFormat("IOCP::%p: Awoken from a kick", this)); |
| 73 |
DWORD bytes = 0; |
| 74 |
ULONG_PTR completion_key; |
| 75 |
LPOVERLAPPED overlapped; |
| 76 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_event_engine_trace)) { |
| 77 |
gpr_log(GPR_DEBUG, "IOCP::%p doing work", this); |
| 78 |
} |
| 79 |
BOOL success = GetQueuedCompletionStatus( |
| 80 |
iocp_handle_, &bytes, &completion_key, &overlapped, |
| 81 |
static_cast<DWORD>(Milliseconds(timeout))); |
| 82 |
if (success == 0 && overlapped == NULL) { |
| 83 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_event_engine_trace)) { |
| 84 |
gpr_log(GPR_DEBUG, "IOCP::%p deadline exceeded", this); |
| 85 |
} |
| 86 |
return Poller::WorkResult::kDeadlineExceeded; |
| 87 |
} |
| 88 |
GPR_ASSERT(completion_key && overlapped); |
| 89 |
if (overlapped == &kick_overlap_) { |
| 90 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_event_engine_trace)) { |
| 91 |
gpr_log(GPR_DEBUG, "IOCP::%p kicked", this); |
| 92 |
} |
| 93 |
outstanding_kicks_.fetch_sub(1); |
| 94 |
if (completion_key == (ULONG_PTR)&kick_token_) { |
| 95 |
return Poller::WorkResult::kKicked; |
| 96 |
} |
| 97 |
gpr_log(GPR_ERROR, "Unknown custom completion key: %p", completion_key); |
| 98 |
abort(); |
| 99 |
} |
| 100 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_event_engine_trace)) { |
| 101 |
gpr_log(GPR_DEBUG, "IOCP::%p got event on OVERLAPPED::%p", this, |
| 102 |
overlapped); |
| 103 |
} |
| 104 |
WinSocket* socket = reinterpret_cast<WinSocket*>(completion_key); |
| 105 |
// TODO(hork): move the following logic into the WinSocket impl. |
| 106 |
WinSocket::OpState* info = socket->GetOpInfoForOverlapped(overlapped); |
| 107 |
GPR_ASSERT(info != nullptr); |
| 108 |
if (socket->IsShutdown()) { |
| 109 |
info->SetError(WSAESHUTDOWN); |
| 110 |
} else { |
| 111 |
info->GetOverlappedResult(); |
| 112 |
} |
| 113 |
if (info->closure() != nullptr) { |
| 114 |
schedule_poll_again(); |
| 115 |
executor_->Run(info->closure()); |
| 116 |
return Poller::WorkResult::kOk; |
| 117 |
} |
| 118 |
// No callback registered. Set ready and return an empty set |
| 119 |
info->SetReady(); |
| 120 |
schedule_poll_again(); |
| 121 |
return Poller::WorkResult::kOk; |
| 122 |
} |
| 123 |
|
| 124 |
void IOCP::Kick() { |
| 125 |
outstanding_kicks_.fetch_add(1); |
| 126 |
GPR_ASSERT(PostQueuedCompletionStatus( |
| 127 |
iocp_handle_, 0, reinterpret_cast<ULONG_PTR>(&kick_token_), |
| 128 |
&kick_overlap_)); |
| 129 |
} |
| 130 |
|
| 131 |
DWORD IOCP::GetDefaultSocketFlags() { |
| 132 |
static DWORD wsa_socket_flags = WSASocketFlagsInit(); |
| 133 |
return wsa_socket_flags; |
| 134 |
} |
| 135 |
|
| 136 |
DWORD IOCP::WSASocketFlagsInit() { |
| 137 |
DWORD wsa_socket_flags = WSA_FLAG_OVERLAPPED; |
| 138 |
/* WSA_FLAG_NO_HANDLE_INHERIT may be not supported on the older Windows |
| 139 |
versions, see |
| 140 |
https://msdn.microsoft.com/en-us/library/windows/desktop/ms742212(v=vs.85).aspx |
| 141 |
for details. */ |
| 142 |
SOCKET sock = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0, |
| 143 |
wsa_socket_flags | WSA_FLAG_NO_HANDLE_INHERIT); |
| 144 |
if (sock != INVALID_SOCKET) { |
| 145 |
/* Windows 7, Windows 2008 R2 with SP1 or later */ |
| 146 |
wsa_socket_flags |= WSA_FLAG_NO_HANDLE_INHERIT; |
| 147 |
closesocket(sock); |
| 148 |
} |
| 149 |
return wsa_socket_flags; |
| 150 |
} |
| 151 |
|
| 152 |
} // namespace experimental |
| 153 |
} // namespace grpc_event_engine |
| 154 |
|
| 155 |
#endif // GPR_WINDOWS |