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
15 #include <grpc/support/port_platform.h>
16
17 #include "src/core/lib/event_engine/posix_engine/internal_errqueue.h"
18
19 #include <string>
20
21 #include <grpc/support/log.h>
22
23 #include "src/core/lib/iomgr/port.h"
24
25 #ifdef GRPC_POSIX_SOCKET_TCP
26
27 #include <errno.h>
28 #include <netinet/in.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/utsname.h>
32
33 #include <cstddef>
34
35 #include "src/core/lib/gprpp/strerror.h"
36
37 namespace grpc_event_engine {
38 namespace experimental {
39
40 #ifdef GRPC_LINUX_ERRQUEUE
GetSocketTcpInfo(struct tcp_info * info,int fd)41 int GetSocketTcpInfo(struct tcp_info* info, int fd) {
42 memset(info, 0, sizeof(*info));
43 info->length = offsetof(tcp_info, length);
44 return getsockopt(fd, IPPROTO_TCP, TCP_INFO, info, &(info->length));
45 }
46 #endif
47
KernelSupportsErrqueue()48 bool KernelSupportsErrqueue() {
49 static const bool errqueue_supported = []() {
50 #ifdef GRPC_LINUX_ERRQUEUE
51 // Both-compile time and run-time linux kernel versions should be at
52 // least 4.0.0
53 struct utsname buffer;
54 if (uname(&buffer) != 0) {
55 gpr_log(GPR_ERROR, "uname: %s", grpc_core::StrError(errno).c_str());
56 return false;
57 }
58 char* release = buffer.release;
59 if (release == nullptr) {
60 return false;
61 }
62
63 if (strtol(release, nullptr, 10) >= 4) {
64 return true;
65 } else {
66 gpr_log(GPR_DEBUG, "ERRQUEUE support not enabled");
67 }
68 #endif // GRPC_LINUX_ERRQUEUE
69 return false;
70 }();
71 return errqueue_supported;
72 }
73
74 } // namespace experimental
75 } // namespace grpc_event_engine
76
77 #endif // GRPC_POSIX_SOCKET_TCP
78