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