Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/iocore/eventsystem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ add_library(
UnixEThread.cc
UnixEvent.cc
UnixEventProcessor.cc
UnixSocket.cc
ConfigProcessor.cc
RecRawStatsImpl.cc
RecProcess.cc
Expand Down
144 changes: 33 additions & 111 deletions src/iocore/eventsystem/P_UnixSocketManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
****************************************************************************/
#pragma once

#include "UnixSocket.h"

#include "tscore/ink_platform.h"
#include "tscore/ink_sock.h"
#include "iocore/eventsystem/SocketManager.h"
Expand All @@ -41,19 +43,6 @@
// 1024 - stdin, stderr, stdout
#define EPOLL_MAX_DESCRIPTOR_SIZE 32768

TS_INLINE bool
transient_error()
{
bool transient = (errno == EINTR);
#ifdef ENOMEM
transient = transient || (errno == ENOMEM);
#endif
#ifdef ENOBUFS
transient = transient || (errno == ENOBUFS);
#endif
return transient;
}

TS_INLINE int
SocketManager::open(const char *path, int oflag, mode_t mode)
{
Expand All @@ -71,80 +60,45 @@ SocketManager::open(const char *path, int oflag, mode_t mode)
TS_INLINE int64_t
SocketManager::read(int fd, void *buf, int size, void * /* pOLP ATS_UNUSED */)
{
int64_t r;
do {
r = ::read(fd, buf, size);
if (likely(r >= 0)) {
break;
}
r = -errno;
} while (r == -EINTR);
return r;
UnixSocket sock{fd};
return sock.read(buf, size);
}

TS_INLINE int
SocketManager::recv(int fd, void *buf, int size, int flags)
{
int r;
do {
if (unlikely((r = ::recv(fd, (char *)buf, size, flags)) < 0)) {
r = -errno;
}
} while (r == -EINTR);
return r;
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)
{
int r;
do {
r = ::recvfrom(fd, (char *)buf, size, flags, addr, addrlen);
if (unlikely(r < 0)) {
r = -errno;
}
} while (r == -EINTR);
return r;
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 */)
{
int r;
do {
if (unlikely((r = ::recvmsg(fd, m, flags)) < 0)) {
r = -errno;
}
} while (r == -EINTR);
return r;
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 */)
{
int r;
do {
if (unlikely((r = ::recvmmsg(fd, msgvec, vlen, flags, timeout)) < 0)) {
r = -errno;
// EINVAL can ocur if timeout is invalid.
}
} while (r == -EINTR);
return r;
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 */)
{
int64_t r;
do {
if (likely((r = ::write(fd, buf, size)) >= 0)) {
break;
}
r = -errno;
} while (r == -EINTR);
return r;
UnixSocket sock{fd};
return sock.write(buf, size);
}

TS_INLINE int64_t
Expand All @@ -162,37 +116,22 @@ SocketManager::pwrite(int fd, void *buf, int size, off_t offset, char * /* tag A
TS_INLINE int
SocketManager::send(int fd, void *buf, int size, int flags)
{
int r;
do {
if (unlikely((r = ::send(fd, (char *)buf, size, flags)) < 0)) {
r = -errno;
}
} while (r == -EINTR);
return r;
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)
{
int r;
do {
if (unlikely((r = ::sendto(fd, (char *)buf, len, flags, to, tolen)) < 0)) {
r = -errno;
}
} while (r == -EINTR);
return r;
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 */)
{
int r;
do {
if (unlikely((r = ::sendmsg(fd, m, flags)) < 0)) {
r = -errno;
}
} while (r == -EINTR);
return r;
UnixSocket sock{fd};
return sock.sendmsg(m, flags);
}

TS_INLINE int64_t
Expand Down Expand Up @@ -222,54 +161,42 @@ SocketManager::fsync(int fildes)
TS_INLINE int
SocketManager::poll(struct pollfd *fds, unsigned long nfds, int timeout)
{
int r;
do {
if ((r = ::poll(fds, nfds, timeout)) >= 0) {
break;
}
r = -errno;
} while (transient_error());
return r;
return UnixSocket::poll(fds, nfds, timeout);
}

TS_INLINE int
SocketManager::get_sndbuf_size(int s)
{
int bsz = 0;
int bszsz, r;

bszsz = sizeof(bsz);
r = safe_getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&bsz, &bszsz);
return (r == 0 ? bsz : r);
UnixSocket sock{s};
return sock.get_sndbuf_size();
}

TS_INLINE int
SocketManager::get_rcvbuf_size(int s)
{
int bsz = 0;
int bszsz, r;

bszsz = sizeof(bsz);
r = safe_getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&bsz, &bszsz);
return (r == 0 ? bsz : r);
UnixSocket sock{s};
return sock.get_rcvbuf_size();
}

TS_INLINE int
SocketManager::set_sndbuf_size(int s, int bsz)
{
return safe_setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&bsz, sizeof(bsz));
UnixSocket sock{s};
return sock.set_sndbuf_size(bsz);
}

TS_INLINE int
SocketManager::set_rcvbuf_size(int s, int bsz)
{
return safe_setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&bsz, sizeof(bsz));
UnixSocket sock{s};
return sock.set_rcvbuf_size(bsz);
}

TS_INLINE int
SocketManager::getsockname(int s, struct sockaddr *sa, socklen_t *sz)
{
return ::getsockname(s, sa, sz);
UnixSocket sock{s};
return sock.getsockname(sa, sz);
}

TS_INLINE int
Expand All @@ -281,11 +208,6 @@ SocketManager::socket(int domain, int type, int protocol)
TS_INLINE int
SocketManager::shutdown(int s, int how)
{
int res;
do {
if (unlikely((res = ::shutdown(s, how)) < 0)) {
res = -errno;
}
} while (res == -EINTR);
return res;
UnixSocket sock{s};
return sock.shutdown(how);
}
80 changes: 11 additions & 69 deletions src/iocore/eventsystem/SocketManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,95 +25,37 @@

SocketManager.cc
****************************************************************************/

#include "UnixSocket.h"

#include "tscore/ink_platform.h"
#include "P_EventSystem.h"

#include "tscore/TextBuffer.h"

#if !HAVE_ACCEPT4
static int
accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
{
int fd, err;

do {
fd = accept(sockfd, addr, addrlen);
if (likely(fd >= 0))
break;
} while (transient_error());

if ((fd >= 0) && (flags & SOCK_CLOEXEC) && (safe_fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)) {
err = errno;
close(fd);
errno = err;
return -1;
}

if ((fd >= 0) && (flags & SOCK_NONBLOCK) && (safe_nonblocking(fd) < 0)) {
err = errno;
close(fd);
errno = err;
return -1;
}

return fd;
}
#endif

int
SocketManager::accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
{
do {
int fd = ::accept4(s, addr, addrlen, flags);
if (likely(fd >= 0)) {
return fd;
}
} while (transient_error());

return -errno;
UnixSocket sock{s};
return sock.accept4(addr, addrlen, flags);
}

int
SocketManager::ink_bind(int s, struct sockaddr const *name, int namelen, short Proto)
SocketManager::ink_bind(int s, struct sockaddr const *name, int namelen, short /* Proto ATS_UNUSED */)
{
(void)Proto;
return safe_bind(s, name, namelen);
UnixSocket sock{s};
return sock.bind(name, namelen);
}

int
SocketManager::close(int s)
{
int res;

if (s == 0) {
return -EACCES;
} else if (s < 0) {
return -EINVAL;
}

do {
res = ::close(s);
if (res == -1) {
res = -errno;
}
} while (res == -EINTR);
return res;
UnixSocket sock{s};
return sock.close();
}

bool
SocketManager::fastopen_supported()
{
static const unsigned TFO_CLIENT_ENABLE = 1;

ats_scoped_fd fd(::open("/proc/sys/net/ipv4/tcp_fastopen", O_RDONLY));
int value = 0;

if (fd) {
TextBuffer buffer(16);

buffer.slurp(fd.get());
value = atoi(buffer.bufPtr());
}

return value & TFO_CLIENT_ENABLE;
return UnixSocket::client_fastopen_supported();
}
Loading