Line data Source code
1 : #pragma once
2 :
3 : #include <sys/stat.h>
4 :
5 : #include <chrono>
6 : #include <memory>
7 : #include <string>
8 : #include <vector>
9 :
10 : #include "envoy/api/os_sys_calls_common.h"
11 : #include "envoy/common/platform.h"
12 : #include "envoy/common/pure.h"
13 : #include "envoy/network/address.h"
14 :
15 : #include "absl/types/optional.h"
16 :
17 : namespace Envoy {
18 : namespace Api {
19 :
20 : struct EnvoyTcpInfo {
21 : std::chrono::microseconds tcpi_rtt;
22 : // Congestion window, in bytes. Note that posix's TCP_INFO socket option returns cwnd in packets,
23 : // we multiply it by MSS to get bytes.
24 : uint32_t tcpi_snd_cwnd = 0;
25 : };
26 :
27 : // Small struct to avoid exposing ifaddrs -- which is not defined in all platforms -- to the
28 : // codebase.
29 : struct InterfaceAddress {
30 : InterfaceAddress(absl::string_view interface_name, unsigned int interface_flags,
31 : Envoy::Network::Address::InstanceConstSharedPtr interface_addr)
32 : : interface_name_(interface_name), interface_flags_(interface_flags),
33 392 : interface_addr_(interface_addr) {}
34 :
35 : std::string interface_name_;
36 : unsigned int interface_flags_;
37 : Envoy::Network::Address::InstanceConstSharedPtr interface_addr_;
38 : };
39 :
40 : using InterfaceAddressVector = std::vector<InterfaceAddress>;
41 :
42 : class OsSysCalls {
43 : public:
44 91 : virtual ~OsSysCalls() = default;
45 :
46 : /**
47 : * @see bind (man 2 bind)
48 : */
49 : virtual SysCallIntResult bind(os_fd_t sockfd, const sockaddr* addr, socklen_t addrlen) PURE;
50 :
51 : /**
52 : * @see chmod (man 2 chmod)
53 : */
54 : virtual SysCallIntResult chmod(const std::string& path, mode_t mode) PURE;
55 :
56 : /**
57 : * This interface is based on Windows `WSAIoctl`. It becomes equivalent with the POSIX interface
58 : * with `in_buffer` as `argp` and the rest of the parameters ignored.
59 : * @see ioctl (man 2 ioctl)
60 : * @see WSAIoctl (MSDN)
61 : */
62 : virtual SysCallIntResult ioctl(os_fd_t sockfd, unsigned long control_code, void* in_buffer,
63 : unsigned long in_buffer_len, void* out_buffer,
64 : unsigned long out_buffer_len, unsigned long* bytes_returned) PURE;
65 :
66 : /**
67 : * @see writev (man 2 writev)
68 : */
69 : virtual SysCallSizeResult writev(os_fd_t fd, const iovec* iov, int num_iov) PURE;
70 :
71 : /**
72 : * @see readv (man 2 readv)
73 : */
74 : virtual SysCallSizeResult readv(os_fd_t fd, const iovec* iov, int num_iov) PURE;
75 :
76 : /**
77 : * @see man 2 pwrite
78 : */
79 : virtual SysCallSizeResult pwrite(os_fd_t fd, const void* buffer, size_t length,
80 : off_t offset) const PURE;
81 :
82 : /**
83 : * @see man 2 pread
84 : */
85 : virtual SysCallSizeResult pread(os_fd_t fd, void* buffer, size_t length, off_t offset) const PURE;
86 :
87 : /**
88 : * @see send (man 2 send)
89 : */
90 : virtual SysCallSizeResult send(os_fd_t socket, void* buffer, size_t length, int flags) PURE;
91 :
92 : /**
93 : * @see recv (man 2 recv)
94 : */
95 : virtual SysCallSizeResult recv(os_fd_t socket, void* buffer, size_t length, int flags) PURE;
96 :
97 : /**
98 : * @see recvmsg (man 2 recvmsg)
99 : */
100 : virtual SysCallSizeResult recvmsg(os_fd_t sockfd, msghdr* msg, int flags) PURE;
101 :
102 : /**
103 : * @see recvmmsg (man 2 recvmmsg)
104 : */
105 : virtual SysCallIntResult recvmmsg(os_fd_t sockfd, struct mmsghdr* msgvec, unsigned int vlen,
106 : int flags, struct timespec* timeout) PURE;
107 :
108 : /**
109 : * return true if the OS supports recvmmsg() and sendmmsg().
110 : */
111 : virtual bool supportsMmsg() const PURE;
112 :
113 : /**
114 : * return true if the OS supports UDP GRO.
115 : */
116 : virtual bool supportsUdpGro() const PURE;
117 :
118 : /**
119 : * return true if the OS supports UDP GSO
120 : */
121 : virtual bool supportsUdpGso() const PURE;
122 :
123 : /**
124 : * return true if the OS support IP_TRANSPARENT or IPV6_TRANSPARENT options by the ip version.
125 : */
126 : virtual bool supportsIpTransparent(Network::Address::IpVersion version) const PURE;
127 :
128 : /**
129 : * return true if the OS supports multi-path TCP
130 : */
131 : virtual bool supportsMptcp() const PURE;
132 :
133 : /**
134 : * Release all resources allocated for fd.
135 : * @return zero on success, -1 returned otherwise.
136 : */
137 : virtual SysCallIntResult close(os_fd_t fd) PURE;
138 :
139 : /**
140 : * @see man 2 ftruncate
141 : */
142 : virtual SysCallIntResult ftruncate(int fd, off_t length) PURE;
143 :
144 : /**
145 : * @see man 2 mmap
146 : */
147 : virtual SysCallPtrResult mmap(void* addr, size_t length, int prot, int flags, int fd,
148 : off_t offset) PURE;
149 :
150 : /**
151 : * @see man 2 stat
152 : */
153 : virtual SysCallIntResult stat(const char* pathname, struct stat* buf) PURE;
154 :
155 : /**
156 : * @see man 2 fstat
157 : */
158 : virtual SysCallIntResult fstat(os_fd_t fd, struct stat* buf) PURE;
159 :
160 : /**
161 : * @see man 2 setsockopt
162 : */
163 : virtual SysCallIntResult setsockopt(os_fd_t sockfd, int level, int optname, const void* optval,
164 : socklen_t optlen) PURE;
165 :
166 : /**
167 : * @see man 2 getsockopt
168 : */
169 : virtual SysCallIntResult getsockopt(os_fd_t sockfd, int level, int optname, void* optval,
170 : socklen_t* optlen) PURE;
171 :
172 : /**
173 : * @see man 2 socket
174 : */
175 : virtual SysCallSocketResult socket(int domain, int type, int protocol) PURE;
176 :
177 : /**
178 : * @see man 2 sendmsg
179 : */
180 : virtual SysCallSizeResult sendmsg(os_fd_t sockfd, const msghdr* message, int flags) PURE;
181 :
182 : /**
183 : * @see man 2 getsockname
184 : */
185 : virtual SysCallIntResult getsockname(os_fd_t sockfd, sockaddr* addr, socklen_t* addrlen) PURE;
186 :
187 : /**
188 : * @see man 2 gethostname
189 : */
190 : virtual SysCallIntResult gethostname(char* name, size_t length) PURE;
191 :
192 : /**
193 : * @see man 2 getpeername
194 : */
195 : virtual SysCallIntResult getpeername(os_fd_t sockfd, sockaddr* name, socklen_t* namelen) PURE;
196 :
197 : /**
198 : * Toggle the blocking state bit using fcntl
199 : */
200 : virtual SysCallIntResult setsocketblocking(os_fd_t sockfd, bool blocking) PURE;
201 :
202 : /**
203 : * @see man 2 connect
204 : */
205 : virtual SysCallIntResult connect(os_fd_t sockfd, const sockaddr* addr, socklen_t addrlen) PURE;
206 :
207 : /**
208 : * @see man 2 open
209 : */
210 : virtual SysCallIntResult open(const char* pathname, int flags) const PURE;
211 :
212 : /**
213 : * @see man 2 open
214 : */
215 : virtual SysCallIntResult open(const char* pathname, int flags, mode_t mode) const PURE;
216 :
217 : /**
218 : * @see man 2 unlink
219 : */
220 : virtual SysCallIntResult unlink(const char* pathname) const PURE;
221 :
222 : /**
223 : * @see man 2 unlink
224 : */
225 : virtual SysCallIntResult linkat(os_fd_t olddirfd, const char* oldpath, os_fd_t newdirfd,
226 : const char* newpath, int flags) const PURE;
227 :
228 : /**
229 : * @see man 2 mkstemp
230 : */
231 : virtual SysCallIntResult mkstemp(char* tmplate) const PURE;
232 :
233 : /**
234 : * Returns true if mkstemp, linkat, unlink, open, close, pread and pwrite are fully supported.
235 : */
236 : virtual bool supportsAllPosixFileOperations() const PURE;
237 :
238 : /**
239 : * @see man 2 shutdown
240 : */
241 : virtual SysCallIntResult shutdown(os_fd_t sockfd, int how) PURE;
242 :
243 : /**
244 : * @see man 2 socketpair
245 : */
246 : virtual SysCallIntResult socketpair(int domain, int type, int protocol, os_fd_t sv[2]) PURE;
247 :
248 : /**
249 : * @see man 2 listen
250 : */
251 : virtual SysCallIntResult listen(os_fd_t sockfd, int backlog) PURE;
252 :
253 : /**
254 : * @see man 2 write
255 : */
256 : virtual SysCallSizeResult write(os_fd_t socket, const void* buffer, size_t length) PURE;
257 :
258 : /**
259 : * @see man 2 accept. The fds returned are configured to be non-blocking.
260 : */
261 : virtual SysCallSocketResult accept(os_fd_t socket, sockaddr* addr, socklen_t* addrlen) PURE;
262 :
263 : /**
264 : * @see man 2 dup(2).
265 : */
266 : virtual SysCallSocketResult duplicate(os_fd_t oldfd) PURE;
267 :
268 : /**
269 : * @see man TCP_INFO. Get the tcp info for the socket.
270 : */
271 : virtual SysCallBoolResult socketTcpInfo(os_fd_t sockfd, EnvoyTcpInfo* tcp_info) PURE;
272 :
273 : /**
274 : * return true if the OS supports getifaddrs.
275 : */
276 : virtual bool supportsGetifaddrs() const PURE;
277 :
278 : /**
279 : * @see man getifaddrs
280 : */
281 : virtual SysCallIntResult getifaddrs(InterfaceAddressVector& interfaces) PURE;
282 :
283 : /**
284 : * @see man getaddrinfo
285 : */
286 : virtual SysCallIntResult getaddrinfo(const char* node, const char* service, const addrinfo* hints,
287 : addrinfo** res) PURE;
288 :
289 : /**
290 : * @see man freeaddrinfo
291 : */
292 : virtual void freeaddrinfo(addrinfo* res) PURE;
293 : };
294 :
295 : using OsSysCallsPtr = std::unique_ptr<OsSysCalls>;
296 :
297 : } // namespace Api
298 : } // namespace Envoy
|