This is an automated email from the ASF dual-hosted git repository.
jvanderzee pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new 204ddea04c Remove `SocketManager` (#11639)
204ddea04c is described below
commit 204ddea04c9e5b388059004671b9c4c6c3110a48
Author: JosiahWI <[email protected]>
AuthorDate: Mon Aug 12 21:06:33 2024 -0500
Remove `SocketManager` (#11639)
* Replace use of `SocketManager` with `UnixSocket`
I carefully double checked all uses of `UnixSocket::is_ok()` as it is very
easy to add or forget a negation.
* Remove `SocketManager`
This removes SocketManager.h, P_UnixSocketManager.cc, and SocketManager.cc.
* Add assertion for `UnixSocket` invariant
---
include/iocore/eventsystem/EventSystem.h | 2 +-
include/iocore/eventsystem/SocketManager.h | 103 -----------
include/iocore/eventsystem/UnixSocket.h | 11 +-
src/iocore/eventsystem/CMakeLists.txt | 1 -
src/iocore/eventsystem/P_EventSystem.h | 1 -
src/iocore/eventsystem/P_UnixSocketManager.h | 198 ---------------------
src/iocore/eventsystem/SocketManager.cc | 61 -------
.../eventsystem/unit_tests/test_EventSystem.cc | 4 +-
src/iocore/hostdb/P_RefCountCache.h | 7 +-
src/iocore/net/BIO_fastopen.cc | 20 ++-
src/iocore/net/Connection.cc | 88 +++++----
src/iocore/net/NetAcceptEventIO.cc | 2 +-
src/iocore/net/P_Connection.h | 4 +-
src/iocore/net/P_UDPConnection.h | 9 +-
src/iocore/net/P_UnixNetVConnection.h | 10 +-
src/iocore/net/P_UnixUDPConnection.h | 4 +-
src/iocore/net/QUICNetProcessor.cc | 6 +-
src/iocore/net/SSLNetVConnection.cc | 12 +-
src/iocore/net/UDPEventIO.cc | 2 +-
src/iocore/net/UnixConnection.cc | 46 +++--
src/iocore/net/UnixNetAccept.cc | 29 +--
src/iocore/net/UnixNetProcessor.cc | 4 +-
src/iocore/net/UnixNetVConnection.cc | 42 +++--
src/iocore/net/UnixUDPConnection.cc | 5 +-
src/iocore/net/UnixUDPNet.cc | 118 ++++++------
src/proxy/http/HttpSessionManager.cc | 2 +-
26 files changed, 227 insertions(+), 564 deletions(-)
diff --git a/include/iocore/eventsystem/EventSystem.h
b/include/iocore/eventsystem/EventSystem.h
index 9e80c8ee49..dc80b025a1 100644
--- a/include/iocore/eventsystem/EventSystem.h
+++ b/include/iocore/eventsystem/EventSystem.h
@@ -41,10 +41,10 @@
#include "iocore/eventsystem/Processor.h"
#include "iocore/eventsystem/ProtectedQueue.h"
#include "iocore/eventsystem/Thread.h"
+#include "iocore/eventsystem/UnixSocket.h"
#include "iocore/eventsystem/VIO.h"
#include "iocore/eventsystem/VConnection.h"
#include "records/RecProcess.h"
-#include "iocore/eventsystem/SocketManager.h"
#include "iocore/eventsystem/RecProcess.h"
static constexpr ts::ModuleVersion EVENT_SYSTEM_MODULE_PUBLIC_VERSION(1, 0,
ts::ModuleVersion::PUBLIC);
diff --git a/include/iocore/eventsystem/SocketManager.h
b/include/iocore/eventsystem/SocketManager.h
deleted file mode 100644
index 369e354edc..0000000000
--- a/include/iocore/eventsystem/SocketManager.h
+++ /dev/null
@@ -1,103 +0,0 @@
-/** @file
-
- A brief file description
-
- @section license License
-
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
-
-/****************************************************************************
-
- SocketManager.h
-
- Handle the allocation of the socket descriptor (fd) resource.
-
-
- ****************************************************************************/
-
-#pragma once
-
-#include "tscore/ink_platform.h"
-
-#ifndef SOCK_NONBLOCK
-#define SOCK_NONBLOCK O_NONBLOCK
-#endif
-
-#ifndef SOCK_CLOEXEC
-#define SOCK_CLOEXEC O_CLOEXEC
-#endif
-
-#ifndef MSG_FASTOPEN
-#if defined(__linux__)
-#define MSG_FASTOPEN 0x20000000
-#else
-#define MSG_FASTOPEN 0
-#endif
-#endif
-
-#define SOCKET int
-
-/** Utility namespace for socket operations.
- */
-namespace SocketManager
-{
-// Test whether TCP Fast Open is supported on this host.
-bool fastopen_supported();
-
-// result is the socket or -errno
-SOCKET socket(int domain = AF_INET, int type = SOCK_STREAM, int protocol = 0);
-
-// result is the number of bytes or -errno
-int64_t read(int fd, void *buf, int len, void *pOLP = nullptr);
-
-int recv(int s, void *buf, int len, int flags);
-int recvfrom(int fd, void *buf, int size, int flags, struct sockaddr *addr,
socklen_t *addrlen);
-int recvmsg(int fd, struct msghdr *m, int flags, void *pOLP = nullptr);
-
-int64_t write(int fd, void *buf, int len, void *pOLP = nullptr);
-int64_t pwrite(int fd, void *buf, int len, off_t offset, char *tag = nullptr);
-
-int send(int fd, void *buf, int len, int flags);
-int sendto(int fd, void *buf, int len, int flags, struct sockaddr const *to,
int tolen);
-int sendmsg(int fd, struct msghdr *m, int flags, void *pOLP = nullptr);
-#ifdef HAVE_RECVMMSG
-int recvmmsg(int fd, struct mmsghdr *msgvec, int vlen, int flags, struct
timespec *timeout, void *pOLP = nullptr);
-#endif
-int64_t lseek(int fd, off_t offset, int whence);
-int fsync(int fildes);
-int poll(struct pollfd *fds, unsigned long nfds, int timeout);
-
-int shutdown(int s, int how);
-
-// result is the fd or -errno
-int accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags);
-
-// manipulate socket buffers
-int get_sndbuf_size(int s);
-int get_rcvbuf_size(int s);
-int set_sndbuf_size(int s, int size);
-int set_rcvbuf_size(int s, int size);
-
-int getsockname(int s, struct sockaddr *, socklen_t *);
-
-/** Close the socket.
- @return 0 if successful, -errno on error.
- */
-int close(int sock);
-int ink_bind(int s, struct sockaddr const *name, int namelen, short protocol =
0);
-} // namespace SocketManager
diff --git a/include/iocore/eventsystem/UnixSocket.h
b/include/iocore/eventsystem/UnixSocket.h
index 67c70ff282..a770e5d8cb 100644
--- a/include/iocore/eventsystem/UnixSocket.h
+++ b/include/iocore/eventsystem/UnixSocket.h
@@ -24,12 +24,14 @@
#pragma once
#include "tscore/ink_apidefs.h"
+#include "tscore/ink_assert.h"
#include "tscore/ink_platform.h"
#include "tscore/ink_sock.h"
#include <cstdint>
#define NO_SOCK -1
+#define SOCKET int
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
@@ -108,7 +110,14 @@ private:
int fd{NO_SOCK};
};
-inline UnixSocket::UnixSocket(int fd) : fd{fd} {}
+inline UnixSocket::UnixSocket(int fd) : fd{fd}
+{
+ // A value of -1 means no socket, and a positive value means a valid
+ // file descriptor (unless it's higher than the max file descriptor, but
+ // we don't check this). This is intended to catch if we try to use
+ // something like `-errno` to initialize the file descriptor.
+ ink_assert(fd >= -1);
+}
inline UnixSocket::UnixSocket(int domain, int type, int protocol)
{
diff --git a/src/iocore/eventsystem/CMakeLists.txt
b/src/iocore/eventsystem/CMakeLists.txt
index 63131bcf54..702d5f0690 100644
--- a/src/iocore/eventsystem/CMakeLists.txt
+++ b/src/iocore/eventsystem/CMakeLists.txt
@@ -26,7 +26,6 @@ add_library(
Processor.cc
ProtectedQueue.cc
ProxyAllocator.cc
- SocketManager.cc
Tasks.cc
Thread.cc
UnixEThread.cc
diff --git a/src/iocore/eventsystem/P_EventSystem.h
b/src/iocore/eventsystem/P_EventSystem.h
index f17686579f..53b4b463f7 100644
--- a/src/iocore/eventsystem/P_EventSystem.h
+++ b/src/iocore/eventsystem/P_EventSystem.h
@@ -44,7 +44,6 @@
#include "P_UnixEThread.h"
#include "P_ProtectedQueue.h"
#include "P_UnixEventProcessor.h"
-#include "P_UnixSocketManager.h"
static constexpr ts::ModuleVersion
EVENT_SYSTEM_MODULE_INTERNAL_VERSION{EVENT_SYSTEM_MODULE_PUBLIC_VERSION,
ts::ModuleVersion::PRIVATE};
diff --git a/src/iocore/eventsystem/P_UnixSocketManager.h
b/src/iocore/eventsystem/P_UnixSocketManager.h
deleted file mode 100644
index 7aec736fb4..0000000000
--- a/src/iocore/eventsystem/P_UnixSocketManager.h
+++ /dev/null
@@ -1,198 +0,0 @@
-/** @file
-
- A brief file description
-
- @section license License
-
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
-
-/****************************************************************************
-
- UnixSocketManager.h
-
- Handle the allocation of the socket descriptor (fd) resource.
-
-
-****************************************************************************/
-#pragma once
-
-#include "tscore/ink_platform.h"
-#include "tscore/ink_sock.h"
-#include "iocore/eventsystem/SocketManager.h"
-#include "iocore/eventsystem/UnixSocket.h"
-
-//
-// These limits are currently disabled
-//
-// 1024 - stdin, stderr, stdout
-#define EPOLL_MAX_DESCRIPTOR_SIZE 32768
-
-TS_INLINE int64_t
-SocketManager::read(int fd, void *buf, int size, void * /* pOLP ATS_UNUSED */)
-{
- UnixSocket sock{fd};
- return sock.read(buf, size);
-}
-
-TS_INLINE int
-SocketManager::recv(int fd, void *buf, int size, int flags)
-{
- UnixSocket sock{fd};
- return sock.recv(buf, size, flags);
-}
-
-TS_INLINE int
-SocketManager::recvfrom(int fd, void *buf, int size, int flags, struct
sockaddr *addr, socklen_t *addrlen)
-{
- UnixSocket sock{fd};
- return sock.recvfrom(buf, size, flags, addr, addrlen);
-}
-
-TS_INLINE int
-SocketManager::recvmsg(int fd, struct msghdr *m, int flags, void * /* pOLP
ATS_UNUSED */)
-{
- UnixSocket sock{fd};
- return sock.recvmsg(m, flags);
-}
-
-#ifdef HAVE_RECVMMSG
-TS_INLINE int
-SocketManager::recvmmsg(int fd, struct mmsghdr *msgvec, int vlen, int flags,
struct timespec *timeout, void * /* pOLP ATS_UNUSED */)
-{
- UnixSocket sock{fd};
- return sock.recvmmsg(msgvec, vlen, flags, timeout);
-}
-#endif
-
-TS_INLINE int64_t
-SocketManager::write(int fd, void *buf, int size, void * /* pOLP ATS_UNUSED */)
-{
- UnixSocket sock{fd};
- return sock.write(buf, size);
-}
-
-TS_INLINE int64_t
-SocketManager::pwrite(int fd, void *buf, int size, off_t offset, char * /* tag
ATS_UNUSED */)
-{
- int64_t r;
- do {
- if (unlikely((r = ::pwrite(fd, buf, size, offset)) < 0)) {
- r = -errno;
- }
- } while (r == -EINTR);
- return r;
-}
-
-TS_INLINE int
-SocketManager::send(int fd, void *buf, int size, int flags)
-{
- UnixSocket sock{fd};
- return sock.send(buf, size, flags);
-}
-
-TS_INLINE int
-SocketManager::sendto(int fd, void *buf, int len, int flags, struct sockaddr
const *to, int tolen)
-{
- UnixSocket sock{fd};
- return sock.sendto(buf, len, flags, to, tolen);
-}
-
-TS_INLINE int
-SocketManager::sendmsg(int fd, struct msghdr *m, int flags, void * /* pOLP
ATS_UNUSED */)
-{
- UnixSocket sock{fd};
- return sock.sendmsg(m, flags);
-}
-
-TS_INLINE int64_t
-SocketManager::lseek(int fd, off_t offset, int whence)
-{
- int64_t r;
- do {
- if ((r = ::lseek(fd, offset, whence)) < 0) {
- r = -errno;
- }
- } while (r == -EINTR);
- return r;
-}
-
-TS_INLINE int
-SocketManager::fsync(int fildes)
-{
- int r;
- do {
- if ((r = ::fsync(fildes)) < 0) {
- r = -errno;
- }
- } while (r == -EINTR);
- return r;
-}
-
-TS_INLINE int
-SocketManager::poll(struct pollfd *fds, unsigned long nfds, int timeout)
-{
- return UnixSocket::poll(fds, nfds, timeout);
-}
-
-TS_INLINE int
-SocketManager::get_sndbuf_size(int s)
-{
- UnixSocket sock{s};
- return sock.get_sndbuf_size();
-}
-
-TS_INLINE int
-SocketManager::get_rcvbuf_size(int s)
-{
- UnixSocket sock{s};
- return sock.get_rcvbuf_size();
-}
-
-TS_INLINE int
-SocketManager::set_sndbuf_size(int s, int bsz)
-{
- UnixSocket sock{s};
- return sock.set_sndbuf_size(bsz);
-}
-
-TS_INLINE int
-SocketManager::set_rcvbuf_size(int s, int bsz)
-{
- UnixSocket sock{s};
- return sock.set_rcvbuf_size(bsz);
-}
-
-TS_INLINE int
-SocketManager::getsockname(int s, struct sockaddr *sa, socklen_t *sz)
-{
- UnixSocket sock{s};
- return sock.getsockname(sa, sz);
-}
-
-TS_INLINE int
-SocketManager::socket(int domain, int type, int protocol)
-{
- return ::socket(domain, type, protocol);
-}
-
-TS_INLINE int
-SocketManager::shutdown(int s, int how)
-{
- UnixSocket sock{s};
- return sock.shutdown(how);
-}
diff --git a/src/iocore/eventsystem/SocketManager.cc
b/src/iocore/eventsystem/SocketManager.cc
deleted file mode 100644
index fc3c9834d3..0000000000
--- a/src/iocore/eventsystem/SocketManager.cc
+++ /dev/null
@@ -1,61 +0,0 @@
-/** @file
-
- A brief file description
-
- @section license License
-
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
-
-/****************************************************************************
-
- SocketManager.cc
- ****************************************************************************/
-
-#include "iocore/eventsystem/UnixSocket.h"
-
-#include "tscore/ink_platform.h"
-#include "P_EventSystem.h"
-
-#include "tscore/TextBuffer.h"
-
-int
-SocketManager::accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int
flags)
-{
- UnixSocket sock{s};
- return sock.accept4(addr, addrlen, flags);
-}
-
-int
-SocketManager::ink_bind(int s, struct sockaddr const *name, int namelen, short
/* Proto ATS_UNUSED */)
-{
- UnixSocket sock{s};
- return sock.bind(name, namelen);
-}
-
-int
-SocketManager::close(int s)
-{
- UnixSocket sock{s};
- return sock.close();
-}
-
-bool
-SocketManager::fastopen_supported()
-{
- return UnixSocket::client_fastopen_supported();
-}
diff --git a/src/iocore/eventsystem/unit_tests/test_EventSystem.cc
b/src/iocore/eventsystem/unit_tests/test_EventSystem.cc
index 228307cb47..8070673935 100644
--- a/src/iocore/eventsystem/unit_tests/test_EventSystem.cc
+++ b/src/iocore/eventsystem/unit_tests/test_EventSystem.cc
@@ -101,9 +101,9 @@ struct EventProcessorListener :
Catch::TestEventListenerBase {
CATCH_REGISTER_LISTENER(EventProcessorListener);
-TEST_CASE("EventSystemSocketManager", "[iocore][sock_mgr]")
+TEST_CASE("EventSystemUnixSocket", "[iocore][sock]")
{
- if (SocketManager::fastopen_supported()) {
+ if (UnixSocket::client_fastopen_supported()) {
if (MSG_FASTOPEN == 0) {
std::printf("TCP Fast Open is supported, MSG_FASTOPEN must not be 0\n");
CHECK(false);
diff --git a/src/iocore/hostdb/P_RefCountCache.h
b/src/iocore/hostdb/P_RefCountCache.h
index 3267dfcb5e..d66d748c91 100644
--- a/src/iocore/hostdb/P_RefCountCache.h
+++ b/src/iocore/hostdb/P_RefCountCache.h
@@ -24,6 +24,7 @@
#include "iocore/eventsystem/EventSystem.h"
#include "../eventsystem/P_EventSystem.h" // TODO: less? just need ET_TASK
+#include "iocore/eventsystem/UnixSocket.h"
#include "swoc/IntrusiveHashMap.h"
@@ -564,12 +565,12 @@ LoadRefCountCacheFromPath(RefCountCache<CacheEntryType>
&cache, const std::strin
RefCountCacheHeader tmpHeader = RefCountCacheHeader();
int read_ret = read(fd, (char *)&tmpHeader,
sizeof(RefCountCacheHeader));
if (read_ret != sizeof(RefCountCacheHeader)) {
- SocketManager::close(fd);
+ UnixSocket{fd}.close();
Warning("Error reading cache header from disk (expected %ld): %d",
sizeof(RefCountCacheHeader), read_ret);
return -1;
}
if (!cache.get_header().compatible(&tmpHeader)) {
- SocketManager::close(fd);
+ UnixSocket{fd}.close();
Warning("Incompatible cache at %s, not loading.", filepath.c_str());
return -1; // TODO: specific code for incompatible
}
@@ -593,6 +594,6 @@ LoadRefCountCacheFromPath(RefCountCache<CacheEntryType>
&cache, const std::strin
}
};
- SocketManager::close(fd);
+ UnixSocket{fd}.close();
return 0;
}
diff --git a/src/iocore/net/BIO_fastopen.cc b/src/iocore/net/BIO_fastopen.cc
index 4c83844aa4..80d648189a 100644
--- a/src/iocore/net/BIO_fastopen.cc
+++ b/src/iocore/net/BIO_fastopen.cc
@@ -24,7 +24,9 @@
#include <openssl/opensslv.h>
#include "P_Net.h"
-#include "iocore/eventsystem/SocketManager.h"
+
+#include "iocore/eventsystem/UnixSocket.h"
+
#include "tscore/ink_assert.h"
#include "tscore/ink_config.h"
@@ -206,8 +208,9 @@ fastopen_bwrite(BIO *bio, const char *in, int insz)
errno = 0;
BIO_clear_retry_flags(bio);
- int fd = BIO_get_fd(bio, nullptr);
- ink_assert(fd != NO_FD);
+ int fd = BIO_get_fd(bio, nullptr);
+ UnixSocket sock{fd};
+ ink_assert(sock.is_ok());
void *dst_void = get_dest_addr_for_bio(bio);
if (dst_void) {
@@ -217,14 +220,14 @@ fastopen_bwrite(BIO *bio, const char *in, int insz)
// RFC 7413. If we get EINPROGRESS it means that the SYN has been
// sent without data and we should retry.
Metrics::Counter::increment(net_rsb.fastopen_attempts);
- err = SocketManager::sendto(fd, (void *)in, insz, MSG_FASTOPEN, dst,
ats_ip_size(dst));
+ err = sock.sendto((void *)in, insz, MSG_FASTOPEN, dst, ats_ip_size(dst));
if (err >= 0) {
Metrics::Counter::increment(net_rsb.fastopen_successes);
}
set_dest_addr_for_bio(bio, nullptr);
} else {
- err = SocketManager::write(fd, (void *)in, insz);
+ err = sock.write((void *)in, insz);
}
if (err < 0) {
@@ -244,12 +247,13 @@ fastopen_bread(BIO *bio, char *out, int outsz)
errno = 0;
BIO_clear_retry_flags(bio);
- int fd = BIO_get_fd(bio, nullptr);
- ink_assert(fd != NO_FD);
+ int fd = BIO_get_fd(bio, nullptr);
+ UnixSocket sock{fd};
+ ink_assert(sock.is_ok());
// TODO: If we haven't done the fastopen, ink_abort().
- err = SocketManager::read(fd, out, outsz);
+ err = sock.read(out, outsz);
if (err < 0) {
errno = -err;
if (BIO_sock_non_fatal_error(errno)) {
diff --git a/src/iocore/net/Connection.cc b/src/iocore/net/Connection.cc
index 0d72e13397..e78a1a7f38 100644
--- a/src/iocore/net/Connection.cc
+++ b/src/iocore/net/Connection.cc
@@ -71,7 +71,7 @@ NetVCOptions::toString(addr_bind_style s)
return ANY_ADDR == s ? "any" : INTF_ADDR == s ? "interface" : "foreign";
}
-Connection::Connection() : fd(NO_FD)
+Connection::Connection()
{
memset(&addr, 0, sizeof(addr));
}
@@ -87,11 +87,11 @@ Server::accept(Connection *c)
int res = 0;
socklen_t sz = sizeof(c->addr);
- res = SocketManager::accept4(fd, &c->addr.sa, &sz, SOCK_NONBLOCK |
SOCK_CLOEXEC);
+ res = sock.accept4(&c->addr.sa, &sz, SOCK_NONBLOCK | SOCK_CLOEXEC);
if (res < 0) {
return res;
}
- c->fd = res;
+ c->sock = UnixSocket{res};
if (dbg_ctl_iocore_net_server.on()) {
ip_port_text_buffer ipb1, ipb2;
DbgPrint(dbg_ctl_iocore_net_server, "Connection accepted [Server]. %s ->
%s", ats_ip_nptop(&c->addr, ipb2, sizeof(ipb2)),
@@ -107,12 +107,10 @@ Connection::close()
is_connected = false;
is_bound = false;
// don't close any of the standards
- if (fd >= 2) {
- int fd_save = fd;
- fd = NO_FD;
- return SocketManager::close(fd_save);
+ if (sock.get_fd() >= 2) {
+ return sock.close();
} else {
- fd = NO_FD;
+ sock = UnixSocket{NO_FD};
return -EBADF;
}
}
@@ -126,15 +124,15 @@ Connection::move(Connection &orig)
{
this->is_connected = orig.is_connected;
this->is_bound = orig.is_bound;
- this->fd = orig.fd;
+ this->sock = orig.sock;
// The target has taken ownership of the file descriptor
- orig.fd = NO_FD;
+ orig.sock = UnixSocket{NO_FD};
this->addr = orig.addr;
this->sock_type = orig.sock_type;
}
static int
-add_http_filter(int fd ATS_UNUSED)
+add_http_filter([[maybe_unused]] int fd)
{
int err = -1;
#if defined(SOL_FILTER) && defined(FIL_ATTACH)
@@ -149,19 +147,19 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
int res = 0;
int listen_per_thread = 0;
- ink_assert(fd != NO_FD);
+ ink_assert(sock.is_ok());
if (opt.defer_accept > 0) {
http_accept_filter = true;
- add_http_filter(fd);
+ add_http_filter(sock.get_fd());
}
if (opt.recv_bufsize) {
- if (SocketManager::set_rcvbuf_size(fd, opt.recv_bufsize)) {
+ if (sock.set_rcvbuf_size(opt.recv_bufsize)) {
// Round down until success
int rbufsz = ROUNDUP(opt.recv_bufsize, 1024);
while (rbufsz) {
- if (SocketManager::set_rcvbuf_size(fd, rbufsz)) {
+ if (sock.set_rcvbuf_size(rbufsz)) {
rbufsz -= 1024;
} else {
break;
@@ -171,11 +169,11 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
}
if (opt.send_bufsize) {
- if (SocketManager::set_sndbuf_size(fd, opt.send_bufsize)) {
+ if (sock.set_sndbuf_size(opt.send_bufsize)) {
// Round down until success
int sbufsz = ROUNDUP(opt.send_bufsize, 1024);
while (sbufsz) {
- if (SocketManager::set_sndbuf_size(fd, sbufsz)) {
+ if (sock.set_sndbuf_size(sbufsz)) {
sbufsz -= 1024;
} else {
break;
@@ -184,7 +182,7 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
}
}
- if (safe_fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
+ if (safe_fcntl(sock.get_fd(), F_SETFD, FD_CLOEXEC) < 0) {
goto Lerror;
}
@@ -193,25 +191,25 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
l.l_onoff = 0;
l.l_linger = 0;
if ((opt.sockopt_flags & NetVCOptions::SOCK_OPT_LINGER_ON) &&
- safe_setsockopt(fd, SOL_SOCKET, SO_LINGER, reinterpret_cast<char
*>(&l), sizeof(l)) < 0) {
+ safe_setsockopt(sock.get_fd(), SOL_SOCKET, SO_LINGER,
reinterpret_cast<char *>(&l), sizeof(l)) < 0) {
goto Lerror;
}
}
- if (ats_is_ip6(&addr) && setsockopt_on(fd, IPPROTO_IPV6, IPV6_V6ONLY) < 0) {
+ if (ats_is_ip6(&addr) && sock.enable_option(IPPROTO_IPV6, IPV6_V6ONLY) < 0) {
goto Lerror;
}
- if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEADDR) < 0) {
+ if (sock.enable_option(SOL_SOCKET, SO_REUSEADDR) < 0) {
goto Lerror;
}
REC_ReadConfigInteger(listen_per_thread, "proxy.config.exec_thread.listen");
if (listen_per_thread == 1) {
- if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEPORT) < 0) {
+ if (sock.enable_option(SOL_SOCKET, SO_REUSEPORT) < 0) {
goto Lerror;
}
#ifdef SO_REUSEPORT_LB
- if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEPORT_LB) < 0) {
+ if (sock.enable_option(SOL_SOCKET, SO_REUSEPORT_LB) < 0) {
goto Lerror;
}
#endif
@@ -227,24 +225,24 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
#else
cpu = ethread->id;
#endif
- if (safe_setsockopt(fd, SOL_SOCKET, SO_INCOMING_CPU, &cpu, sizeof(cpu)) <
0) {
+ if (safe_setsockopt(sock.get_fd(), SOL_SOCKET, SO_INCOMING_CPU, &cpu,
sizeof(cpu)) < 0) {
goto Lerror;
}
- Dbg(dbg_ctl_iocore_thread, "SO_INCOMING_CPU - fd=%d cpu=%d", fd, cpu);
+ Dbg(dbg_ctl_iocore_thread, "SO_INCOMING_CPU - fd=%d cpu=%d",
sock.get_fd(), cpu);
}
#endif
- if ((opt.sockopt_flags & NetVCOptions::SOCK_OPT_NO_DELAY) &&
setsockopt_on(fd, IPPROTO_TCP, TCP_NODELAY) < 0) {
+ if ((opt.sockopt_flags & NetVCOptions::SOCK_OPT_NO_DELAY) &&
sock.enable_option(IPPROTO_TCP, TCP_NODELAY) < 0) {
goto Lerror;
}
// enables 2 hour inactivity probes, also may fix IRIX FIN_WAIT_2 leak
- if ((opt.sockopt_flags & NetVCOptions::SOCK_OPT_KEEP_ALIVE) &&
setsockopt_on(fd, SOL_SOCKET, SO_KEEPALIVE) < 0) {
+ if ((opt.sockopt_flags & NetVCOptions::SOCK_OPT_KEEP_ALIVE) &&
sock.enable_option(SOL_SOCKET, SO_KEEPALIVE) < 0) {
goto Lerror;
}
#ifdef TCP_FASTOPEN
if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_TCP_FAST_OPEN) {
- if (safe_setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, (char
*)&opt.tfo_queue_length, sizeof(int))) {
+ if (safe_setsockopt(sock.get_fd(), IPPROTO_TCP, TCP_FASTOPEN, (char
*)&opt.tfo_queue_length, sizeof(int))) {
// EOPNOTSUPP also checked for general safeguarding of unsupported
operations of socket functions
if (opt.f_mptcp && (errno == ENOPROTOOPT || errno == EOPNOTSUPP)) {
Warning("[Server::listen] TCP_FASTOPEN socket option not valid on
MPTCP socket level");
@@ -258,7 +256,7 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
if (opt.f_inbound_transparent) {
#if TS_USE_TPROXY
Dbg(dbg_ctl_http_tproxy, "Listen port inbound transparency enabled.");
- if (setsockopt_on(fd, SOL_IP, TS_IP_TRANSPARENT) < 0) {
+ if (sock.enable_option(SOL_IP, TS_IP_TRANSPARENT) < 0) {
Fatal("[Server::listen] Unable to set transparent socket option [%d]
%s\n", errno, strerror(errno));
}
#else
@@ -274,7 +272,8 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
if (NetProcessor::accept_mss > 0) {
if (opt.f_mptcp) {
Warning("[Server::listen] TCP_MAXSEG socket option not valid on MPTCP
socket level");
- } else if (safe_setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
reinterpret_cast<char *>(&NetProcessor::accept_mss), sizeof(int)) < 0) {
+ } else if (safe_setsockopt(sock.get_fd(), IPPROTO_TCP, TCP_MAXSEG,
reinterpret_cast<char *>(&NetProcessor::accept_mss),
+ sizeof(int)) < 0) {
goto Lerror;
}
}
@@ -283,7 +282,7 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
#ifdef TCP_DEFER_ACCEPT
// set tcp defer accept timeout if it is configured, this will not trigger
an accept until there is
// data on the socket ready to be read
- if (opt.defer_accept > 0 && setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT,
&opt.defer_accept, sizeof(int)) < 0) {
+ if (opt.defer_accept > 0 && setsockopt(sock.get_fd(), IPPROTO_TCP,
TCP_DEFER_ACCEPT, &opt.defer_accept, sizeof(int)) < 0) {
// FIXME: should we go to the error
// goto error;
Error("[Server::listen] Defer accept is configured but set failed: %d",
errno);
@@ -291,7 +290,7 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
#endif
if (non_blocking) {
- if (safe_nonblocking(fd) < 0) {
+ if (sock.set_nonblocking() < 0) {
goto Lerror;
}
}
@@ -302,7 +301,7 @@ Lerror:
res = -errno;
// coverity[check_after_sink]
- if (fd != NO_FD) {
+ if (sock.is_ok()) {
close();
}
@@ -328,7 +327,7 @@ Server::setup_fd_after_listen([[maybe_unused]] const
NetProcessor::AcceptOptions
bzero(&afa, sizeof(afa));
strcpy(afa.af_name, "dataready");
- if (setsockopt(this->fd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa))
< 0) {
+ if (setsockopt(this->sock.get_fd(), SOL_SOCKET, SO_ACCEPTFILTER, &afa,
sizeof(afa)) < 0) {
Error("[Server::listen] Defer accept is configured but set failed:
%d", errno);
return -errno;
}
@@ -342,10 +341,10 @@ Server::setup_fd_after_listen([[maybe_unused]] const
NetProcessor::AcceptOptions
int
Server::listen(bool non_blocking, const NetProcessor::AcceptOptions &opt)
{
- ink_assert(fd == NO_FD);
- int res = 0;
- int namelen;
- int prot = IPPROTO_TCP;
+ ink_assert(!sock.is_ok());
+ int res = 0;
+ socklen_t namelen;
+ int prot = IPPROTO_TCP;
if (!ats_is_ip(&accept_addr)) {
ats_ip4_set(&addr, INADDR_ANY, 0);
@@ -358,8 +357,8 @@ Server::listen(bool non_blocking, const
NetProcessor::AcceptOptions &opt)
prot = IPPROTO_MPTCP;
}
- fd = res = SocketManager::socket(addr.sa.sa_family, SOCK_STREAM, prot);
- if (res < 0) {
+ sock = UnixSocket{addr.sa.sa_family, SOCK_STREAM, prot};
+ if (!sock.is_ok()) {
goto Lerror;
}
@@ -368,11 +367,11 @@ Server::listen(bool non_blocking, const
NetProcessor::AcceptOptions &opt)
goto Lerror;
}
- if ((res = SocketManager::ink_bind(fd, &addr.sa, ats_ip_size(&addr.sa),
prot)) < 0) {
+ if ((res = sock.bind(&addr.sa, ats_ip_size(&addr.sa))) < 0) {
goto Lerror;
}
- if ((res = safe_listen(fd, get_listen_backlog())) < 0) {
+ if ((res = safe_listen(sock.get_fd(), get_listen_backlog())) < 0) {
goto Lerror;
}
@@ -383,16 +382,15 @@ Server::listen(bool non_blocking, const
NetProcessor::AcceptOptions &opt)
// Original just did this on port == 0.
namelen = sizeof(addr);
- if ((res = safe_getsockname(fd, &addr.sa, &namelen))) {
+ if ((res = sock.getsockname(&addr.sa, &namelen))) {
goto Lerror;
}
return 0;
Lerror:
- if (fd != NO_FD) {
+ if (sock.is_ok()) {
close();
- fd = NO_FD;
}
Fatal("Could not bind or listen to port %d, mptcp enabled: %d (error: %d) %s
%d", ats_ip_port_host_order(&addr),
diff --git a/src/iocore/net/NetAcceptEventIO.cc
b/src/iocore/net/NetAcceptEventIO.cc
index 74a4c4718a..876c663ebf 100644
--- a/src/iocore/net/NetAcceptEventIO.cc
+++ b/src/iocore/net/NetAcceptEventIO.cc
@@ -28,7 +28,7 @@ int
NetAcceptEventIO::start(EventLoop l, NetAccept *na, int events)
{
_na = na;
- return start_common(l, _na->server.fd, events);
+ return start_common(l, _na->server.sock.get_fd(), events);
}
void
NetAcceptEventIO::process_event(int /* flags ATS_UNUSED */)
diff --git a/src/iocore/net/P_Connection.h b/src/iocore/net/P_Connection.h
index 8ca38dbfaa..0ec192b7b3 100644
--- a/src/iocore/net/P_Connection.h
+++ b/src/iocore/net/P_Connection.h
@@ -52,6 +52,8 @@
#include "tscore/ink_platform.h"
#include "iocore/net/NetProcessor.h"
+#include "iocore/eventsystem/UnixSocket.h"
+
struct NetVCOptions;
///////////////////////////////////////////////////////////////////////
@@ -60,7 +62,7 @@ struct NetVCOptions;
//
///////////////////////////////////////////////////////////////////////
struct Connection {
- SOCKET fd; ///< Socket for connection.
+ UnixSocket sock{NO_FD};
IpEndpoint addr; ///< Associated address.
bool is_bound = false; ///< Flag for already bound to a local
address.
bool is_connected = false; ///< Flag for already connected.
diff --git a/src/iocore/net/P_UDPConnection.h b/src/iocore/net/P_UDPConnection.h
index 4669284ffd..6979750af6 100644
--- a/src/iocore/net/P_UDPConnection.h
+++ b/src/iocore/net/P_UDPConnection.h
@@ -30,9 +30,12 @@
****************************************************************************/
#pragma once
-#include "tscore/ink_atomic.h"
#include "iocore/net/UDPNet.h"
+#include "iocore/eventsystem/UnixSocket.h"
+
+#include "tscore/ink_atomic.h"
+
class UDPConnectionInternal : public UDPConnection
{
public:
@@ -42,7 +45,7 @@ public:
Continuation *continuation = nullptr;
int refcount = 0; // public for assertion
- SOCKET fd = -1;
+ UnixSocket sock{-1};
IpEndpoint binding{};
bool binding_valid = false;
int tobedestroyed = 0;
@@ -59,7 +62,7 @@ UDPConnectionInternal::~UDPConnectionInternal()
TS_INLINE SOCKET
UDPConnection::getFd()
{
- return static_cast<UDPConnectionInternal *>(this)->fd;
+ return static_cast<UDPConnectionInternal *>(this)->sock.get_fd();
}
TS_INLINE void
diff --git a/src/iocore/net/P_UnixNetVConnection.h
b/src/iocore/net/P_UnixNetVConnection.h
index ea8dec572b..f685528d65 100644
--- a/src/iocore/net/P_UnixNetVConnection.h
+++ b/src/iocore/net/P_UnixNetVConnection.h
@@ -167,7 +167,7 @@ public:
virtual int
get_fd() override
{
- return this->con.fd;
+ return this->con.sock.get_fd();
}
virtual EThread *
@@ -298,7 +298,7 @@ UnixNetVConnection::set_local_addr()
// This call will fail if fd is closed already. That is ok, because the
// `local_addr` is checked within get_local_addr() and the `got_local_addr`
// is set only with a valid `local_addr`.
- ATS_UNUSED_RETURN(safe_getsockname(con.fd, &local_addr.sa, &local_sa_size));
+ ATS_UNUSED_RETURN(safe_getsockname(get_fd(), &local_addr.sa,
&local_sa_size));
}
// Update the internal VC state variable for MPTCP
@@ -310,7 +310,7 @@ UnixNetVConnection::set_mptcp_state()
int minfo_len = sizeof(minfo);
Dbg(_dbg_ctl_socket_mptcp, "MPTCP_INFO and struct mptcp_info defined");
- if (0 == safe_getsockopt(con.fd, SOL_MPTCP, MPTCP_INFO, &minfo, &minfo_len))
{
+ if (0 == safe_getsockopt(get_fd(), SOL_MPTCP, MPTCP_INFO, &minfo,
&minfo_len)) {
if (minfo_len > 0) {
Dbg(_dbg_ctl_socket_mptcp, "MPTCP socket state (remote key received):
%d",
(minfo.mptcpi_flags & MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED));
@@ -319,7 +319,7 @@ UnixNetVConnection::set_mptcp_state()
}
} else {
mptcp_state = 0;
- Dbg(_dbg_ctl_socket_mptcp, "MPTCP failed getsockopt(%d, MPTCP_INFO): %s",
con.fd, strerror(errno));
+ Dbg(_dbg_ctl_socket_mptcp, "MPTCP failed getsockopt(%d, MPTCP_INFO): %s",
get_fd(), strerror(errno));
}
#endif
}
@@ -368,7 +368,7 @@ inline UnixNetVConnection::~UnixNetVConnection()
inline SOCKET
UnixNetVConnection::get_socket()
{
- return con.fd;
+ return get_fd();
}
inline void
diff --git a/src/iocore/net/P_UnixUDPConnection.h
b/src/iocore/net/P_UnixUDPConnection.h
index 3627b0fc65..e3cf65517a 100644
--- a/src/iocore/net/P_UnixUDPConnection.h
+++ b/src/iocore/net/P_UnixUDPConnection.h
@@ -62,14 +62,14 @@ private:
TS_INLINE
UnixUDPConnection::UnixUDPConnection(int the_fd)
{
- fd = the_fd;
+ sock = UnixSocket{the_fd};
SET_HANDLER(&UnixUDPConnection::callbackHandler);
}
TS_INLINE void
UnixUDPConnection::init(int the_fd)
{
- fd = the_fd;
+ sock = UnixSocket{the_fd};
onCallbackQueue = 0;
callbackAction = nullptr;
ethread = nullptr;
diff --git a/src/iocore/net/QUICNetProcessor.cc
b/src/iocore/net/QUICNetProcessor.cc
index 0c36425c37..b5ab10c90f 100644
--- a/src/iocore/net/QUICNetProcessor.cc
+++ b/src/iocore/net/QUICNetProcessor.cc
@@ -34,6 +34,8 @@
#include "iocore/net/quic/QUICConfig.h"
#include "iocore/net/QUICMultiCertConfigLoader.h"
+#include "iocore/eventsystem/UnixSocket.h"
+
//
// Global Data
//
@@ -246,8 +248,8 @@ QUICNetProcessor::main_accept(Continuation *cont, SOCKET
fd, AcceptOptions const
ink_assert(0 < opt.local_port && opt.local_port < 65536);
accept_ip.network_order_port() = htons(opt.local_port);
- na->accept_fn = net_accept;
- na->server.fd = fd;
+ na->accept_fn = net_accept;
+ na->server.sock = UnixSocket{fd};
ats_ip_copy(&na->server.accept_addr, &accept_ip);
na->action_ = new NetAcceptAction();
diff --git a/src/iocore/net/SSLNetVConnection.cc
b/src/iocore/net/SSLNetVConnection.cc
index 323cb16b06..d75c97b42b 100644
--- a/src/iocore/net/SSLNetVConnection.cc
+++ b/src/iocore/net/SSLNetVConnection.cc
@@ -420,12 +420,12 @@ SSLNetVConnection::read_raw_data()
buf_len = rattempted;
b = b->next.get();
- r = SocketManager::read(this->con.fd, buffer, buf_len);
+ r = this->con.sock.read(buffer, buf_len);
Metrics::Counter::increment(net_rsb.calls_to_read);
total_read += rattempted;
Dbg(dbg_ctl_ssl, "read_raw_data r=%" PRId64 " rattempted=%" PRId64 "
total_read=%" PRId64 " fd=%d", r, rattempted, total_read,
- con.fd);
+ con.sock.get_fd());
// last read failed or was incomplete
if (r != rattempted || !b) {
break;
@@ -963,7 +963,7 @@ SSLNetVConnection::do_io_close(int lerrno)
// at the same time we send the close-notify. If so, the client will
likely
// send RST anyway
char c;
- ssize_t x = recv(this->con.fd, &c, 1, MSG_PEEK);
+ ssize_t x = this->con.sock.recv(&c, 1, MSG_PEEK);
// x < 0 means error. x == 0 means fin sent
bool do_shutdown = (x > 0);
if (x < 0) {
@@ -1031,7 +1031,7 @@ SSLNetVConnection::free_thread(EThread *t)
ink_release_assert(t == this_ethread());
// close socket fd
- if (con.fd != NO_FD) {
+ if (con.sock.is_ok()) {
release_inbound_connection_tracking();
Metrics::Gauge::decrement(net_rsb.connections_currently_open);
}
@@ -1075,13 +1075,13 @@ SSLNetVConnection::free_thread(EThread *t)
clear();
SET_CONTINUATION_HANDLER(this, &SSLNetVConnection::startEvent);
- ink_assert(con.fd == NO_FD);
+ ink_assert(!con.sock.is_ok());
ink_assert(t == this_ethread());
if (from_accept_thread) {
sslNetVCAllocator.free(this);
} else {
- ink_assert(con.fd == NO_FD);
+ ink_assert(!con.sock.is_ok());
THREAD_FREE(this, sslNetVCAllocator, t);
}
}
diff --git a/src/iocore/net/UDPEventIO.cc b/src/iocore/net/UDPEventIO.cc
index 0823df64ed..a0224c0f2a 100644
--- a/src/iocore/net/UDPEventIO.cc
+++ b/src/iocore/net/UDPEventIO.cc
@@ -35,7 +35,7 @@ UDPEventIO::start(EventLoop l, UnixUDPConnection *uc,
UDPNetHandler *uh, int eve
{
_uc = uc;
_uh = uh;
- return start_common(l, uc->fd, events);
+ return start_common(l, uc->getFd(), events);
}
void
diff --git a/src/iocore/net/UnixConnection.cc b/src/iocore/net/UnixConnection.cc
index 1febf0d6ad..6b3342f81f 100644
--- a/src/iocore/net/UnixConnection.cc
+++ b/src/iocore/net/UnixConnection.cc
@@ -110,9 +110,8 @@ NetVCOptions const Connection::DEFAULT_OPTIONS;
int
Connection::open(NetVCOptions const &opt)
{
- ink_assert(fd == NO_FD);
+ ink_assert(!sock.is_ok());
- int res = 0; // temp result
IpEndpoint local_addr;
sock_type = NetVCOptions::USE_UDP == opt.ip_proto ? SOCK_DGRAM : SOCK_STREAM;
int family;
@@ -137,25 +136,24 @@ Connection::open(NetVCOptions const &opt)
local_addr.network_order_port() = htons(opt.local_port);
}
- res = SocketManager::socket(family, sock_type, 0);
- if (-1 == res) {
+ sock = UnixSocket{family, sock_type, 0};
+ if (!sock.is_ok()) {
return -errno;
}
- fd = res;
// mark fd for close until we succeed.
cleaner<Connection> cleanup(this, &Connection::_cleanup);
// Try setting the various socket options, if requested.
- if (-1 == setsockopt_on(fd, SOL_SOCKET, SO_REUSEADDR)) {
+ if (-1 == sock.enable_option(SOL_SOCKET, SO_REUSEADDR)) {
return -errno;
}
if (NetVCOptions::FOREIGN_ADDR == opt.addr_binding) {
static char const *const DEBUG_TEXT = "::open setsockopt() IP_TRANSPARENT";
#if TS_USE_TPROXY
- if (-1 == setsockopt_on(fd, SOL_IP, TS_IP_TRANSPARENT)) {
+ if (-1 == sock.enable_option(SOL_IP, TS_IP_TRANSPARENT)) {
Dbg(dbg_ctl_socket, "%s - fail %d:%s", DEBUG_TEXT, errno,
strerror(errno));
return -errno;
} else {
@@ -166,25 +164,25 @@ Connection::open(NetVCOptions const &opt)
#endif
}
- if (!opt.f_blocking_connect && -1 == safe_nonblocking(fd)) {
+ if (!opt.f_blocking_connect && -1 == sock.set_nonblocking()) {
return -errno;
}
if (opt.socket_recv_bufsize > 0) {
- if (SocketManager::set_rcvbuf_size(fd, opt.socket_recv_bufsize)) {
+ if (sock.set_rcvbuf_size(opt.socket_recv_bufsize)) {
// Round down until success
int rbufsz = ROUNDUP(opt.socket_recv_bufsize, 1024);
- while (rbufsz && !SocketManager::set_rcvbuf_size(fd, rbufsz)) {
+ while (rbufsz && !sock.set_rcvbuf_size(rbufsz)) {
rbufsz -= 1024;
}
Dbg(dbg_ctl_socket, "::open: recv_bufsize = %d of %d", rbufsz,
opt.socket_recv_bufsize);
}
}
if (opt.socket_send_bufsize > 0) {
- if (SocketManager::set_sndbuf_size(fd, opt.socket_send_bufsize)) {
+ if (sock.set_sndbuf_size(opt.socket_send_bufsize)) {
// Round down until success
int sbufsz = ROUNDUP(opt.socket_send_bufsize, 1024);
- while (sbufsz && !SocketManager::set_sndbuf_size(fd, sbufsz)) {
+ while (sbufsz && !sock.set_sndbuf_size(sbufsz)) {
sbufsz -= 1024;
}
Dbg(dbg_ctl_socket, "::open: send_bufsize = %d of %d", sbufsz,
opt.socket_send_bufsize);
@@ -195,7 +193,7 @@ Connection::open(NetVCOptions const &opt)
apply_options(opt);
if (local_addr.network_order_port() || !is_any_address) {
- if (-1 == SocketManager::ink_bind(fd, &local_addr.sa,
ats_ip_size(&local_addr.sa))) {
+ if (-1 == sock.bind(&local_addr.sa, ats_ip_size(&local_addr.sa))) {
return -errno;
}
}
@@ -208,7 +206,7 @@ Connection::open(NetVCOptions const &opt)
int
Connection::connect(sockaddr const *target, NetVCOptions const &opt)
{
- ink_assert(fd != NO_FD);
+ ink_assert(sock.is_ok());
ink_assert(is_bound);
ink_assert(!is_connected);
@@ -229,7 +227,7 @@ Connection::connect(sockaddr const *target, NetVCOptions
const &opt)
errno = EINPROGRESS;
res = -1;
} else {
- res = ::connect(fd, &this->addr.sa, ats_ip_size(&this->addr.sa));
+ res = sock.connect(&this->addr.sa, ats_ip_size(&this->addr.sa));
}
// It's only really an error if either the connect was blocking
@@ -240,11 +238,11 @@ Connection::connect(sockaddr const *target, NetVCOptions
const &opt)
if (-1 == res && (opt.f_blocking_connect || !(EINPROGRESS == errno ||
EWOULDBLOCK == errno))) {
return -errno;
} else if (opt.f_blocking_connect && !opt.f_blocking) {
- if (-1 == safe_nonblocking(fd)) {
+ if (-1 == sock.set_nonblocking()) {
return -errno;
}
} else if (!opt.f_blocking_connect && opt.f_blocking) {
- if (-1 == safe_blocking(fd)) {
+ if (-1 == safe_blocking(sock.get_fd())) {
return -errno;
}
}
@@ -270,24 +268,24 @@ Connection::apply_options(NetVCOptions const &opt)
// ignore other changes
if (SOCK_STREAM == sock_type) {
if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_NO_DELAY) {
- setsockopt_on(fd, IPPROTO_TCP, TCP_NODELAY);
+ sock.enable_option(IPPROTO_TCP, TCP_NODELAY);
Dbg(dbg_ctl_socket, "::open: setsockopt() TCP_NODELAY on socket");
}
if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_KEEP_ALIVE) {
- setsockopt_on(fd, SOL_SOCKET, SO_KEEPALIVE);
+ sock.enable_option(SOL_SOCKET, SO_KEEPALIVE);
Dbg(dbg_ctl_socket, "::open: setsockopt() SO_KEEPALIVE on socket");
}
if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_LINGER_ON) {
struct linger l;
l.l_onoff = 1;
l.l_linger = 0;
- safe_setsockopt(fd, SOL_SOCKET, SO_LINGER, reinterpret_cast<char *>(&l),
sizeof(l));
+ safe_setsockopt(sock.get_fd(), SOL_SOCKET, SO_LINGER,
reinterpret_cast<char *>(&l), sizeof(l));
Dbg(dbg_ctl_socket, "::open:: setsockopt() turn on SO_LINGER on socket");
}
#ifdef TCP_NOTSENT_LOWAT
if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_TCP_NOTSENT_LOWAT) {
uint32_t lowat = opt.packet_notsent_lowat;
- safe_setsockopt(fd, IPPROTO_TCP, TCP_NOTSENT_LOWAT,
reinterpret_cast<char *>(&lowat), sizeof(lowat));
+ safe_setsockopt(sock.get_fd(), IPPROTO_TCP, TCP_NOTSENT_LOWAT,
reinterpret_cast<char *>(&lowat), sizeof(lowat));
Dbg(dbg_ctl_socket, "::open:: setsockopt() set TCP_NOTSENT_LOWAT to %d",
lowat);
}
#endif
@@ -296,7 +294,7 @@ Connection::apply_options(NetVCOptions const &opt)
#if TS_HAS_SO_MARK
if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_PACKET_MARK) {
uint32_t mark = opt.packet_mark;
- safe_setsockopt(fd, SOL_SOCKET, SO_MARK, reinterpret_cast<char *>(&mark),
sizeof(uint32_t));
+ safe_setsockopt(sock.get_fd(), SOL_SOCKET, SO_MARK, reinterpret_cast<char
*>(&mark), sizeof(uint32_t));
}
#endif
@@ -304,9 +302,9 @@ Connection::apply_options(NetVCOptions const &opt)
if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_PACKET_TOS) {
uint32_t tos = opt.packet_tos;
if (addr.isIp4()) {
- safe_setsockopt(fd, IPPROTO_IP, IP_TOS, reinterpret_cast<char *>(&tos),
sizeof(uint32_t));
+ safe_setsockopt(sock.get_fd(), IPPROTO_IP, IP_TOS, reinterpret_cast<char
*>(&tos), sizeof(uint32_t));
} else if (addr.isIp6()) {
- safe_setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, reinterpret_cast<char
*>(&tos), sizeof(uint32_t));
+ safe_setsockopt(sock.get_fd(), IPPROTO_IPV6, IPV6_TCLASS,
reinterpret_cast<char *>(&tos), sizeof(uint32_t));
}
}
#endif
diff --git a/src/iocore/net/UnixNetAccept.cc b/src/iocore/net/UnixNetAccept.cc
index 487d0d5663..b8c91d3c1c 100644
--- a/src/iocore/net/UnixNetAccept.cc
+++ b/src/iocore/net/UnixNetAccept.cc
@@ -72,7 +72,7 @@ handle_max_client_connections(IpEndpoint const &addr,
std::shared_ptr<Connection
static void
safe_delay(int msec)
{
- SocketManager::poll(nullptr, 0, msec);
+ UnixSocket::poll(nullptr, 0, msec);
}
//
@@ -102,7 +102,7 @@ net_accept(NetAccept *na, void *ep, bool blockable)
if (res == -EAGAIN || res == -ECONNABORTED || res == -EPIPE) {
goto Ldone;
}
- if (na->server.fd != NO_FD && !na->action_->cancelled) {
+ if (na->server.sock.is_ok() && !na->action_->cancelled) {
if (!blockable) {
na->action_->continuation->handleEvent(EVENT_ERROR, (void
*)static_cast<intptr_t>(res));
} else {
@@ -330,7 +330,7 @@ NetAccept::do_listen_impl(bool non_blocking)
{
int res = 0;
- if (server.fd != NO_FD) {
+ if (server.sock.is_ok()) {
if ((res = server.setup_fd_for_listen(non_blocking, opt))) {
Warning("unable to listen on main accept port %d: errno = %d, %s",
server.accept_addr.host_order_port(), errno,
strerror(errno));
@@ -508,12 +508,15 @@ NetAccept::acceptFastEvent(int event, void *ep)
int additional_accepts =
NetHandler::get_additional_accepts();
do {
- socklen_t sz = sizeof(con.addr);
- int fd = SocketManager::accept4(server.fd, &con.addr.sa, &sz,
SOCK_NONBLOCK | SOCK_CLOEXEC);
- con.fd = fd;
+ socklen_t sz = sizeof(con.addr);
+ UnixSocket sock{-1};
+ if (int res{server.sock.accept4(&con.addr.sa, &sz, SOCK_NONBLOCK |
SOCK_CLOEXEC)}; res >= 0) {
+ sock = UnixSocket{res};
+ }
+ con.sock = sock;
std::shared_ptr<ConnectionTracker::Group> conn_track_group;
- if (likely(fd >= 0)) {
+ if (likely(sock.is_ok())) {
// check for throttle
if (check_net_throttle(ACCEPT)) {
// close the connection as we are in throttle state
@@ -525,13 +528,13 @@ NetAccept::acceptFastEvent(int event, void *ep)
con.close();
continue;
}
- Dbg(dbg_ctl_iocore_net, "accepted a new socket: %d", fd);
+ Dbg(dbg_ctl_iocore_net, "accepted a new socket: %d", sock.get_fd());
Metrics::Counter::increment(net_rsb.tcp_accept);
if (opt.send_bufsize > 0) {
- if (unlikely(SocketManager::set_sndbuf_size(fd, opt.send_bufsize))) {
+ if (unlikely(sock.set_sndbuf_size(opt.send_bufsize))) {
bufsz = ROUNDUP(opt.send_bufsize, 1024);
while (bufsz > 0) {
- if (!SocketManager::set_sndbuf_size(fd, bufsz)) {
+ if (!sock.set_sndbuf_size(bufsz)) {
break;
}
bufsz -= 1024;
@@ -539,10 +542,10 @@ NetAccept::acceptFastEvent(int event, void *ep)
}
}
if (opt.recv_bufsize > 0) {
- if (unlikely(SocketManager::set_rcvbuf_size(fd, opt.recv_bufsize))) {
+ if (unlikely(sock.set_rcvbuf_size(opt.recv_bufsize))) {
bufsz = ROUNDUP(opt.recv_bufsize, 1024);
while (bufsz > 0) {
- if (!SocketManager::set_rcvbuf_size(fd, bufsz)) {
+ if (!sock.set_rcvbuf_size(bufsz)) {
break;
}
bufsz -= 1024;
@@ -550,7 +553,7 @@ NetAccept::acceptFastEvent(int event, void *ep)
}
}
} else {
- res = fd;
+ res = sock.get_fd();
}
// check return value from accept()
if (res < 0) {
diff --git a/src/iocore/net/UnixNetProcessor.cc
b/src/iocore/net/UnixNetProcessor.cc
index a6519b9a61..c81cc1aa4c 100644
--- a/src/iocore/net/UnixNetProcessor.cc
+++ b/src/iocore/net/UnixNetProcessor.cc
@@ -110,8 +110,8 @@ UnixNetProcessor::accept_internal(Continuation *cont, int
fd, AcceptOptions cons
ink_assert(0 < opt.local_port && opt.local_port < 65536);
accept_ip.network_order_port() = htons(opt.local_port);
- na->accept_fn = net_accept; // All callers used this.
- na->server.fd = fd;
+ na->accept_fn = net_accept; // All callers used this.
+ na->server.sock = UnixSocket{fd};
ats_ip_copy(&na->server.accept_addr, &accept_ip);
if (opt.f_inbound_transparent) {
diff --git a/src/iocore/net/UnixNetVConnection.cc
b/src/iocore/net/UnixNetVConnection.cc
index c8bc8c2866..46b568999b 100644
--- a/src/iocore/net/UnixNetVConnection.cc
+++ b/src/iocore/net/UnixNetVConnection.cc
@@ -25,6 +25,9 @@
#include "P_NetAccept.h"
#include "iocore/net/ConnectionTracker.h"
+
+#include "iocore/eventsystem/UnixSocket.h"
+
#include "tscore/ink_platform.h"
#include "tscore/InkErrno.h"
@@ -273,7 +276,7 @@ read_from_net(NetHandler *nh, UnixNetVConnection *vc,
EThread *thread)
msg.msg_namelen = ats_ip_size(vc->get_remote_addr());
msg.msg_iov = &tiovec[0];
msg.msg_iovlen = niov;
- r = SocketManager::recvmsg(vc->con.fd, &msg, 0);
+ r = vc->con.sock.recvmsg(&msg, 0);
Metrics::Counter::increment(net_rsb.calls_to_read);
@@ -703,7 +706,7 @@ UnixNetVConnection::do_io_shutdown(ShutdownHowTo_t howto)
{
switch (howto) {
case IO_SHUTDOWN_READ:
- SocketManager::shutdown((this)->con.fd, 0);
+ this->con.sock.shutdown(0);
read.enabled = 0;
read.vio.buffer.clear();
read.vio.nbytes = 0;
@@ -711,7 +714,7 @@ UnixNetVConnection::do_io_shutdown(ShutdownHowTo_t howto)
f.shutdown |= NetEvent::SHUTDOWN_READ;
break;
case IO_SHUTDOWN_WRITE:
- SocketManager::shutdown((this)->con.fd, 1);
+ this->con.sock.shutdown(1);
write.enabled = 0;
write.vio.buffer.clear();
write.vio.nbytes = 0;
@@ -719,7 +722,7 @@ UnixNetVConnection::do_io_shutdown(ShutdownHowTo_t howto)
f.shutdown |= NetEvent::SHUTDOWN_WRITE;
break;
case IO_SHUTDOWN_READWRITE:
- SocketManager::shutdown((this)->con.fd, 2);
+ this->con.sock.shutdown(2);
read.enabled = 0;
write.enabled = 0;
read.vio.buffer.clear();
@@ -935,7 +938,7 @@ UnixNetVConnection::load_buffer_and_write(int64_t towrite,
MIOBufferAccessor &bu
Metrics::Counter::increment(net_rsb.fastopen_attempts);
flags = MSG_FASTOPEN;
}
- r = SocketManager::sendmsg(con.fd, &msg, flags);
+ r = con.sock.sendmsg(&msg, flags);
if (!this->con.is_connected && this->options.f_tcp_fastopen) {
if (r < 0) {
if (r == -EINPROGRESS || r == -EWOULDBLOCK) {
@@ -1161,7 +1164,7 @@ UnixNetVConnection::populate(Connection &con_in,
Continuation *c, void * /* arg
ink_assert(this->nh != nullptr);
SET_HANDLER(&UnixNetVConnection::mainEvent);
this->nh->startCop(this);
- ink_assert(this->con.fd != NO_FD);
+ ink_assert(this->con.sock.is_ok());
return EVENT_DONE;
}
@@ -1169,7 +1172,8 @@ int
UnixNetVConnection::connectUp(EThread *t, int fd)
{
ink_assert(get_NetHandler(t)->mutex->thread_holding == this_ethread());
- int res;
+ int res;
+ UnixSocket sock{fd};
thread = t;
if (check_net_throttle(CONNECT)) {
@@ -1194,7 +1198,7 @@ UnixNetVConnection::connectUp(EThread *t, int fd)
// If this is getting called from the TS API, then we are wiring up a file
descriptor
// provided by the caller. In that case, we know that the socket is already
connected.
- if (fd == NO_FD) {
+ if (!sock.is_ok()) {
// Due to multi-threads system, the fd returned from con.open() may exceed
the limitation of check_net_throttle().
res = con.open(options);
if (res != 0) {
@@ -1207,8 +1211,8 @@ UnixNetVConnection::connectUp(EThread *t, int fd)
// eventfd or a regular file fd. That is ok, because sock_type
// is only used when setting up the socket.
safe_getsockopt(fd, SOL_SOCKET, SO_TYPE, reinterpret_cast<char
*>(&con.sock_type), &len);
- safe_nonblocking(fd);
- con.fd = fd;
+ sock.set_nonblocking();
+ con.sock = sock;
con.is_connected = true;
con.is_bound = true;
}
@@ -1219,7 +1223,7 @@ UnixNetVConnection::connectUp(EThread *t, int fd)
goto fail;
}
- if (fd == NO_FD) {
+ if (!sock.is_ok()) {
res = con.connect(nullptr, options);
if (res != 0) {
// fast stopIO
@@ -1229,7 +1233,7 @@ UnixNetVConnection::connectUp(EThread *t, int fd)
// Did not fail, increment connection count
Metrics::Gauge::increment(net_rsb.connections_currently_open);
- ink_release_assert(con.fd != NO_FD);
+ ink_release_assert(con.sock.is_ok());
// Setup a timeout callback handler.
SET_HANDLER(&UnixNetVConnection::mainEvent);
@@ -1245,8 +1249,8 @@ UnixNetVConnection::connectUp(EThread *t, int fd)
fail:
lerrno = -res;
action_.continuation->handleEvent(NET_EVENT_OPEN_FAILED, (void
*)static_cast<intptr_t>(res));
- if (fd != NO_FD) {
- con.fd = NO_FD;
+ if (con.sock.is_ok()) {
+ con.sock = UnixSocket{NO_FD};
}
if (nullptr != nh) {
nh->free_netevent(this);
@@ -1305,7 +1309,7 @@ UnixNetVConnection::free_thread(EThread *t)
ink_release_assert(t == this_ethread());
// close socket fd
- if (con.fd != NO_FD) {
+ if (con.sock.is_ok()) {
release_inbound_connection_tracking();
Metrics::Gauge::decrement(net_rsb.connections_currently_open);
}
@@ -1328,7 +1332,7 @@ UnixNetVConnection::free_thread(EThread *t)
clear();
SET_CONTINUATION_HANDLER(this, &UnixNetVConnection::startEvent);
- ink_assert(con.fd == NO_FD);
+ ink_assert(!con.sock.is_ok());
ink_assert(t == this_ethread());
if (from_accept_thread) {
@@ -1526,13 +1530,13 @@
UnixNetVConnection::set_tcp_congestion_control([[maybe_unused]] int side)
}
if (!ccp.empty()) {
- int rv = setsockopt(con.fd, IPPROTO_TCP, TCP_CONGESTION,
reinterpret_cast<const void *>(ccp.data()), ccp.size());
+ int rv = setsockopt(con.sock.get_fd(), IPPROTO_TCP, TCP_CONGESTION,
static_cast<const void *>(ccp.data()), ccp.size());
if (rv < 0) {
- Error("Unable to set TCP congestion control on socket %d to \"%s\",
errno=%d (%s)", con.fd, ccp.data(), errno,
+ Error("Unable to set TCP congestion control on socket %d to \"%s\",
errno=%d (%s)", con.sock.get_fd(), ccp.data(), errno,
strerror(errno));
} else {
- Dbg(dbg_ctl_socket, "Setting TCP congestion control on socket [%d] to
\"%s\" -> %d", con.fd, ccp.data(), rv);
+ Dbg(dbg_ctl_socket, "Setting TCP congestion control on socket [%d] to
\"%s\" -> %d", con.sock.get_fd(), ccp.data(), rv);
}
return 0;
}
diff --git a/src/iocore/net/UnixUDPConnection.cc
b/src/iocore/net/UnixUDPConnection.cc
index 736b2ca6b1..a8427b2ad7 100644
--- a/src/iocore/net/UnixUDPConnection.cc
+++ b/src/iocore/net/UnixUDPConnection.cc
@@ -57,10 +57,9 @@ UnixUDPConnection::~UnixUDPConnection()
callbackAction = nullptr;
}
Dbg(dbg_ctl_udpnet, "Destroying udp port = %d", getPortNum());
- if (fd != NO_FD) {
- SocketManager::close(fd);
+ if (sock.is_ok()) {
+ sock.close();
}
- fd = NO_FD;
}
// called with continuation lock taken out
diff --git a/src/iocore/net/UnixUDPNet.cc b/src/iocore/net/UnixUDPNet.cc
index 4aa8138836..dd9ed940dd 100644
--- a/src/iocore/net/UnixUDPNet.cc
+++ b/src/iocore/net/UnixUDPNet.cc
@@ -41,6 +41,7 @@
#endif
#include "P_Net.h"
#include "P_UDPNet.h"
+#include "iocore/eventsystem/UnixSocket.h"
#include "tscore/ink_inet.h"
#include "tscore/ink_sock.h"
#include <netinet/udp.h>
@@ -430,7 +431,7 @@
UDPNetProcessorInternal::read_single_message_from_net(UDPNetHandler *nh, UDPConn
msg.msg_controllen = cmsg_size;
// receive data by recvmsg
- r = SocketManager::recvmsg(uc->getFd(), &msg, 0);
+ r = uc->sock.recvmsg(&msg, 0);
if (r <= 0) {
// error
break;
@@ -563,7 +564,7 @@
UDPNetProcessorInternal::read_multiple_messages_from_net(UDPNetHandler *nh, UDPC
mmsg[msg_num].msg_hdr.msg_controllen = cmsg_size;
}
- const int return_val = SocketManager::recvmmsg(uc->getFd(), mmsg,
MAX_RECEIVE_MSG_PER_CALL, MSG_WAITFORONE, nullptr);
+ const int return_val = uc->sock.recvmmsg(mmsg, MAX_RECEIVE_MSG_PER_CALL,
MSG_WAITFORONE, nullptr);
if (return_val <= 0) {
Dbg(dbg_ctl_udp_read, "Done. recvmmsg() ret is %d, errno %s", return_val,
strerror(errno));
@@ -865,8 +866,9 @@ UDPReadContinuation::readPollEvent(int event_, Event *e)
c = completionUtil::getContinuation(event);
// do read
- socklen_t tmp_fromlen = *fromaddrlen;
- int rlen = SocketManager::recvfrom(fd, readbuf->end(), readlen,
0, ats_ip_sa_cast(fromaddr), &tmp_fromlen);
+ socklen_t tmp_fromlen = *fromaddrlen;
+ UnixSocket sock{fd};
+ int rlen = sock.recvfrom(readbuf->end(), readlen, 0,
ats_ip_sa_cast(fromaddr), &tmp_fromlen);
completionUtil::setThread(event, e->ethread);
// call back user with their event
@@ -934,11 +936,12 @@ UDPNetProcessor::recvfrom_re(Continuation *cont, void
*token, int fd, struct soc
completionUtil::setContinuation(event, cont);
completionUtil::setHandle(event, token);
- actual = SocketManager::recvfrom(fd, buf->end(), len, 0, fromaddr,
fromaddrlen);
+ UnixSocket sock{fd};
+ actual = sock.recvfrom(buf->end(), len, 0, fromaddr, fromaddrlen);
if (actual > 0) {
completionUtil::setThread(event, this_ethread());
- completionUtil::setInfo(event, fd, make_ptr(buf), actual, errno);
+ completionUtil::setInfo(event, sock.get_fd(), make_ptr(buf), actual,
errno);
buf->fill(actual);
cont->handleEvent(NET_EVENT_DATAGRAM_READ_COMPLETE, event);
completionUtil::destroy(event);
@@ -946,14 +949,14 @@ UDPNetProcessor::recvfrom_re(Continuation *cont, void
*token, int fd, struct soc
} else if (actual == 0 || actual == -EAGAIN) {
UDPReadContinuation *c = udpReadContAllocator.alloc();
c->init_token(event);
- c->init_read(fd, buf, len, fromaddr, fromaddrlen);
+ c->init_read(sock.get_fd(), buf, len, fromaddr, fromaddrlen);
if (timeout) {
c->set_timer(timeout);
}
return event;
} else {
completionUtil::setThread(event, this_ethread());
- completionUtil::setInfo(event, fd, make_ptr(buf), actual, errno);
+ completionUtil::setInfo(event, sock.get_fd(), make_ptr(buf), actual,
errno);
cont->handleEvent(NET_EVENT_DATAGRAM_READ_ERROR, event);
completionUtil::destroy(event);
return ACTION_IO_ERROR;
@@ -977,16 +980,17 @@ UDPNetProcessor::sendmsg_re(Continuation *cont, void
*token, int fd, struct msgh
completionUtil::setContinuation(event, cont);
completionUtil::setHandle(event, token);
- actual = SocketManager::sendmsg(fd, msg, 0);
+ UnixSocket sock{fd};
+ actual = sock.sendmsg(msg, 0);
if (actual >= 0) {
completionUtil::setThread(event, this_ethread());
- completionUtil::setInfo(event, fd, msg, actual, errno);
+ completionUtil::setInfo(event, sock.get_fd(), msg, actual, errno);
cont->handleEvent(NET_EVENT_DATAGRAM_WRITE_COMPLETE, event);
completionUtil::destroy(event);
return ACTION_RESULT_DONE;
} else {
completionUtil::setThread(event, this_ethread());
- completionUtil::setInfo(event, fd, msg, actual, errno);
+ completionUtil::setInfo(event, sock.get_fd(), msg, actual, errno);
cont->handleEvent(NET_EVENT_DATAGRAM_WRITE_ERROR, event);
completionUtil::destroy(event);
return ACTION_IO_ERROR;
@@ -1010,7 +1014,8 @@ UDPNetProcessor::sendto_re(Continuation *cont, void
*token, int fd, struct socka
{
(void)token;
ink_assert(buf->read_avail() >= len);
- int nbytes_sent = SocketManager::sendto(fd, buf->start(), len, 0, toaddr,
toaddrlen);
+ UnixSocket sock{fd};
+ int nbytes_sent = sock.sendto(buf->start(), len, 0, toaddr,
toaddrlen);
if (nbytes_sent >= 0) {
ink_assert(nbytes_sent == len);
@@ -1026,9 +1031,8 @@ UDPNetProcessor::sendto_re(Continuation *cont, void
*token, int fd, struct socka
bool
UDPNetProcessor::CreateUDPSocket(int *resfd, sockaddr const *remote_addr,
Action **status, NetVCOptions const &opt)
{
- int res = 0, fd = -1;
IpEndpoint local_addr;
- int local_addr_len = sizeof(local_addr.sa);
+ socklen_t local_addr_len = sizeof(local_addr.sa);
// Need to do address calculations first, so we can determine the
// address family for socket creation.
@@ -1051,22 +1055,22 @@ UDPNetProcessor::CreateUDPSocket(int *resfd, sockaddr
const *remote_addr, Action
}
*resfd = -1;
- if ((res = SocketManager::socket(remote_addr->sa_family, SOCK_DGRAM, 0)) <
0) {
+ UnixSocket sock{remote_addr->sa_family, SOCK_DGRAM, 0};
+ if (!sock.is_ok()) {
goto HardError;
}
- fd = res;
- if (safe_fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
+ if (safe_fcntl(sock.get_fd(), F_SETFL, O_NONBLOCK) < 0) {
goto HardError;
}
if (opt.socket_recv_bufsize > 0) {
- if (unlikely(SocketManager::set_rcvbuf_size(fd, opt.socket_recv_bufsize)))
{
+ if (unlikely(sock.set_rcvbuf_size(opt.socket_recv_bufsize))) {
Dbg(dbg_ctl_udpnet, "set_dnsbuf_size(%d) failed",
opt.socket_recv_bufsize);
}
}
if (opt.socket_send_bufsize > 0) {
- if (unlikely(SocketManager::set_sndbuf_size(fd, opt.socket_send_bufsize)))
{
+ if (unlikely(sock.set_sndbuf_size(opt.socket_send_bufsize))) {
Dbg(dbg_ctl_udpnet, "set_dnsbuf_size(%d) failed",
opt.socket_send_bufsize);
}
}
@@ -1074,12 +1078,12 @@ UDPNetProcessor::CreateUDPSocket(int *resfd, sockaddr
const *remote_addr, Action
if (opt.ip_family == AF_INET) {
bool succeeded = false;
#ifdef IP_PKTINFO
- if (setsockopt_on(fd, IPPROTO_IP, IP_PKTINFO) == 0) {
+ if (sock.enable_option(IPPROTO_IP, IP_PKTINFO) == 0) {
succeeded = true;
}
#endif
#ifdef IP_RECVDSTADDR
- if (setsockopt_on(fd, IPPROTO_IP, IP_RECVDSTADDR) == 0) {
+ if (sock.enable_option(IPPROTO_IP, IP_RECVDSTADDR) == 0) {
succeeded = true;
}
#endif
@@ -1090,12 +1094,12 @@ UDPNetProcessor::CreateUDPSocket(int *resfd, sockaddr
const *remote_addr, Action
} else if (opt.ip_family == AF_INET6) {
bool succeeded = false;
#ifdef IPV6_PKTINFO
- if (setsockopt_on(fd, IPPROTO_IPV6, IPV6_PKTINFO) == 0) {
+ if (sock.enable_option(IPPROTO_IPV6, IPV6_PKTINFO) == 0) {
succeeded = true;
}
#endif
#ifdef IPV6_RECVPKTINFO
- if (setsockopt_on(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO) == 0) {
+ if (sock.enable_option(IPPROTO_IPV6, IPV6_RECVPKTINFO) == 0) {
succeeded = true;
}
#endif
@@ -1106,35 +1110,35 @@ UDPNetProcessor::CreateUDPSocket(int *resfd, sockaddr
const *remote_addr, Action
}
if (local_addr.network_order_port() || !is_any_address) {
- if (-1 == SocketManager::ink_bind(fd, &local_addr.sa,
ats_ip_size(&local_addr.sa))) {
+ if (-1 == sock.bind(&local_addr.sa, ats_ip_size(&local_addr.sa))) {
char buff[INET6_ADDRPORTSTRLEN];
Dbg(dbg_ctl_udpnet, "ink bind failed on %s", ats_ip_nptop(local_addr,
buff, sizeof(buff)));
goto SoftError;
}
- if (safe_getsockname(fd, &local_addr.sa, &local_addr_len) < 0) {
+ if (sock.getsockname(&local_addr.sa, &local_addr_len) < 0) {
Dbg(dbg_ctl_udpnet, "CreateUdpsocket: getsockname didn't work");
goto HardError;
}
}
- *resfd = fd;
+ *resfd = sock.get_fd();
*status = nullptr;
Dbg(dbg_ctl_udpnet, "creating a udp socket port = %d, %d---success",
ats_ip_port_host_order(remote_addr),
ats_ip_port_host_order(local_addr));
return true;
SoftError:
Dbg(dbg_ctl_udpnet, "creating a udp socket port = %d---soft failure",
ats_ip_port_host_order(local_addr));
- if (fd != -1) {
- SocketManager::close(fd);
+ if (sock.is_ok()) {
+ sock.close();
}
*resfd = -1;
*status = nullptr;
return false;
HardError:
Dbg(dbg_ctl_udpnet, "creating a udp socket port = %d---hard failure",
ats_ip_port_host_order(local_addr));
- if (fd != -1) {
- SocketManager::close(fd);
+ if (sock.is_ok()) {
+ sock.close();
}
*resfd = -1;
*status = ACTION_IO_ERROR;
@@ -1144,35 +1148,35 @@ HardError:
Action *
UDPNetProcessor::UDPBind(Continuation *cont, sockaddr const *addr, int fd, int
send_bufsize, int recv_bufsize)
{
- int res = 0;
- UnixUDPConnection *n = nullptr;
+ UnixUDPConnection *n = nullptr;
IpEndpoint myaddr;
- int myaddr_len = sizeof(myaddr);
+ socklen_t myaddr_len = sizeof(myaddr);
PollCont *pc = nullptr;
PollDescriptor *pd = nullptr;
bool need_bind = true;
+ UnixSocket sock{fd};
- if (fd == -1) {
- if ((res = SocketManager::socket(addr->sa_family, SOCK_DGRAM, 0)) < 0) {
+ if (!sock.is_ok()) {
+ sock = UnixSocket{addr->sa_family, SOCK_DGRAM, 0};
+ if (!sock.is_ok()) {
goto Lerror;
}
- fd = res;
} else {
need_bind = false;
}
- if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
+ if (fcntl(sock.get_fd(), F_SETFL, O_NONBLOCK) < 0) {
goto Lerror;
}
if (addr->sa_family == AF_INET) {
bool succeeded = false;
#ifdef IP_PKTINFO
- if (setsockopt_on(fd, IPPROTO_IP, IP_PKTINFO) == 0) {
+ if (sock.enable_option(IPPROTO_IP, IP_PKTINFO) == 0) {
succeeded = true;
}
#endif
#ifdef IP_RECVDSTADDR
- if (setsockopt_on(fd, IPPROTO_IP, IP_RECVDSTADDR) == 0) {
+ if (sock.enable_option(IPPROTO_IP, IP_RECVDSTADDR) == 0) {
succeeded = true;
}
#endif
@@ -1182,19 +1186,19 @@ UDPNetProcessor::UDPBind(Continuation *cont, sockaddr
const *addr, int fd, int s
}
#ifdef IP_MTU_DISCOVER
int probe = IP_PMTUDISC_PROBE; // Set DF but ignore Path MTU
- if (safe_setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &probe,
sizeof(probe)) == -1) {
+ if (safe_setsockopt(sock.get_fd(), IPPROTO_IP, IP_MTU_DISCOVER, &probe,
sizeof(probe)) == -1) {
Dbg(dbg_ctl_udpnet, "setsockeopt for IP_MTU_DISCOVER failed");
}
#endif
} else if (addr->sa_family == AF_INET6) {
bool succeeded = false;
#ifdef IPV6_PKTINFO
- if (setsockopt_on(fd, IPPROTO_IPV6, IPV6_PKTINFO) == 0) {
+ if (sock.enable_option(IPPROTO_IPV6, IPV6_PKTINFO) == 0) {
succeeded = true;
}
#endif
#ifdef IPV6_RECVPKTINFO
- if (setsockopt_on(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO) == 0) {
+ if (sock.enable_option(IPPROTO_IPV6, IPV6_RECVPKTINFO) == 0) {
succeeded = true;
}
#endif
@@ -1204,7 +1208,7 @@ UDPNetProcessor::UDPBind(Continuation *cont, sockaddr
const *addr, int fd, int s
}
#ifdef IPV6_MTU_DISCOVER
int probe = IPV6_PMTUDISC_PROBE; // Set DF but ignore Path MTU
- if (safe_setsockopt(fd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &probe,
sizeof(probe)) == -1) {
+ if (safe_setsockopt(sock.get_fd(), IPPROTO_IPV6, IPV6_MTU_DISCOVER,
&probe, sizeof(probe)) == -1) {
Dbg(dbg_ctl_udpnet, "setsockeopt for IPV6_MTU_DISCOVER failed");
}
#endif
@@ -1213,7 +1217,7 @@ UDPNetProcessor::UDPBind(Continuation *cont, sockaddr
const *addr, int fd, int s
#ifdef SOL_UDP
if (G_udp_config.enable_gro) {
int gro = 1;
- if (safe_setsockopt(fd, IPPROTO_UDP, UDP_GRO, (char *)&gro, sizeof(gro))
== -1) {
+ if (safe_setsockopt(sock.get_fd(), IPPROTO_UDP, UDP_GRO, (char *)&gro,
sizeof(gro)) == -1) {
Dbg(dbg_ctl_udpnet, "setsockopt UDP_GRO. errno=%d", errno);
}
}
@@ -1224,55 +1228,55 @@ UDPNetProcessor::UDPBind(Continuation *cont, sockaddr
const *addr, int fd, int s
sk_txtime.clockid = CLOCK_MONOTONIC;
sk_txtime.flags = 0;
- if (setsockopt(fd, SOL_SOCKET, SO_TXTIME, &sk_txtime, sizeof(sk_txtime)) ==
-1) {
+ if (setsockopt(sock.get_fd(), SOL_SOCKET, SO_TXTIME, &sk_txtime,
sizeof(sk_txtime)) == -1) {
Dbg(dbg_ctl_udpnet, "Failed to setsockopt SO_TXTIME. errno=%d", errno);
}
#endif
// If this is a class D address (i.e. multicast address), use REUSEADDR.
if (ats_is_ip_multicast(addr)) {
- if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEADDR) < 0) {
+ if (sock.enable_option(SOL_SOCKET, SO_REUSEADDR) < 0) {
goto Lerror;
}
}
- if (need_bind && ats_is_ip6(addr) && setsockopt_on(fd, IPPROTO_IPV6,
IPV6_V6ONLY) < 0) {
+ if (need_bind && ats_is_ip6(addr) && sock.enable_option(IPPROTO_IPV6,
IPV6_V6ONLY) < 0) {
goto Lerror;
}
- if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEPORT) < 0) {
+ if (sock.enable_option(SOL_SOCKET, SO_REUSEPORT) < 0) {
Dbg(dbg_ctl_udpnet, "setsockopt for SO_REUSEPORT failed");
goto Lerror;
}
#ifdef SO_REUSEPORT_LB
- if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEPORT_LB) < 0) {
+ if (sock.enable_option(SOL_SOCKET, SO_REUSEPORT_LB) < 0) {
Dbg(dbg_ctl_udpnet, "setsockopt for SO_REUSEPORT_LB failed");
goto Lerror;
}
#endif
- if (need_bind && (SocketManager::ink_bind(fd, addr, ats_ip_size(addr)) < 0))
{
+ if (need_bind && (sock.bind(addr, ats_ip_size(addr)) < 0)) {
Dbg(dbg_ctl_udpnet, "ink_bind failed");
goto Lerror;
}
// check this for GRO
if (recv_bufsize) {
- if (unlikely(SocketManager::set_rcvbuf_size(fd, recv_bufsize))) {
+ if (unlikely(sock.set_rcvbuf_size(recv_bufsize))) {
Dbg(dbg_ctl_udpnet, "set_dnsbuf_size(%d) failed", recv_bufsize);
}
}
if (send_bufsize) {
- if (unlikely(SocketManager::set_sndbuf_size(fd, send_bufsize))) {
+ if (unlikely(sock.set_sndbuf_size(send_bufsize))) {
Dbg(dbg_ctl_udpnet, "set_dnsbuf_size(%d) failed", send_bufsize);
}
}
- if (safe_getsockname(fd, &myaddr.sa, &myaddr_len) < 0) {
+ if (sock.getsockname(&myaddr.sa, &myaddr_len) < 0) {
goto Lerror;
}
- n = new UnixUDPConnection(fd);
+ n = new UnixUDPConnection(sock.get_fd());
- Dbg(dbg_ctl_udpnet, "UDPNetProcessor::UDPBind: %p fd=%d", n, fd);
+ Dbg(dbg_ctl_udpnet, "UDPNetProcessor::UDPBind: %p fd=%d", n, sock.get_fd());
n->setBinding(&myaddr.sa);
n->bindToThread(cont, cont->getThreadAffinity());
@@ -1284,8 +1288,8 @@ UDPNetProcessor::UDPBind(Continuation *cont, sockaddr
const *addr, int fd, int s
cont->handleEvent(NET_EVENT_DATAGRAM_OPEN, n);
return ACTION_RESULT_DONE;
Lerror:
- if (fd != NO_FD) {
- SocketManager::close(fd);
+ if (sock.is_ok()) {
+ sock.close();
}
Dbg(dbg_ctl_udpnet, "Error: %s (%d)", strerror(errno), errno);
diff --git a/src/proxy/http/HttpSessionManager.cc
b/src/proxy/http/HttpSessionManager.cc
index fb6625f7ec..db0a8a074c 100644
--- a/src/proxy/http/HttpSessionManager.cc
+++ b/src/proxy/http/HttpSessionManager.cc
@@ -312,7 +312,7 @@ ServerSessionPool::eventHandler(int event, void *data)
char peer_ip[INET6_ADDRPORTSTRLEN];
ats_ip_nptop(unix_net_vc->get_remote_addr(), peer_ip, sizeof(peer_ip));
- Warning("Connection leak from http keep-alive system fd=%d closed=%d
peer_ip_port=%s", unix_net_vc->con.fd,
+ Warning("Connection leak from http keep-alive system fd=%d closed=%d
peer_ip_port=%s", unix_net_vc->get_fd(),
unix_net_vc->closed, peer_ip);
}
ink_assert(0);