Skip to content

Commit

Permalink
network: add io handle recv function for http inspector (#12736)
Browse files Browse the repository at this point in the history
Risk Level: Low
Testing: unit tests
Docs Changes: n/a
Release Notes: n/a

Signed-off-by: Florin Coras <fcoras@cisco.com>
  • Loading branch information
florincoras authored Aug 21, 2020
1 parent be89c79 commit 9273e6f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
8 changes: 8 additions & 0 deletions include/envoy/network/io_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ class IoHandle {
virtual Api::IoCallUint64Result recvmmsg(RawSliceArrays& slices, uint32_t self_port,
RecvMsgOutput& output) PURE;

/**
* Read data into given buffer for connected handles
* @param buffer buffer to read the data into
* @param length buffer length
* @param flags flags to pass to the underlying recv function (see man 2 recv)
*/
virtual Api::IoCallUint64Result recv(void* buffer, size_t length, int flags) PURE;

/**
* return true if the platform supports recvmmsg() and sendmmsg().
*/
Expand Down
6 changes: 6 additions & 0 deletions source/common/network/io_socket_handle_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,12 @@ Api::IoCallUint64Result IoSocketHandleImpl::recvmmsg(RawSliceArrays& slices, uin
return sysCallResultToIoCallResult(result);
}

Api::IoCallUint64Result IoSocketHandleImpl::recv(void* buffer, size_t length, int flags) {
const Api::SysCallSizeResult result =
Api::OsSysCallsSingleton::get().recv(fd_, buffer, length, flags);
return sysCallResultToIoCallResult(result);
}

bool IoSocketHandleImpl::supportsMmsg() const {
return Api::OsSysCallsSingleton::get().supportsMmsg();
}
Expand Down
1 change: 1 addition & 0 deletions source/common/network/io_socket_handle_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class IoSocketHandleImpl : public IoHandle, protected Logger::Loggable<Logger::I

Api::IoCallUint64Result recvmmsg(RawSliceArrays& slices, uint32_t self_port,
RecvMsgOutput& output) override;
Api::IoCallUint64Result recv(void* buffer, size_t length, int flags) override;

bool supportsMmsg() const override;
bool supportsUdpGro() const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,12 @@ Network::FilterStatus Filter::onAccept(Network::ListenerFilterCallbacks& cb) {
}

ParseState Filter::onRead() {
auto& os_syscalls = Api::OsSysCallsSingleton::get();
const Network::ConnectionSocket& socket = cb_->socket();
const Api::SysCallSizeResult result =
os_syscalls.recv(socket.ioHandle().fd(), buf_, Config::MAX_INSPECT_SIZE, MSG_PEEK);
auto result = cb_->socket().ioHandle().recv(buf_, Config::MAX_INSPECT_SIZE, MSG_PEEK);
ENVOY_LOG(trace, "http inspector: recv: {}", result.rc_);
if (SOCKET_FAILURE(result.rc_) && result.errno_ == SOCKET_ERROR_AGAIN) {
return ParseState::Continue;
} else if (SOCKET_FAILURE(result.rc_)) {
if (!result.ok()) {
if (result.err_->getErrorCode() == Api::IoError::IoErrorCode::Again) {
return ParseState::Continue;
}
config_->stats().read_error_.inc();
return ParseState::Error;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ class QuicIoHandleWrapper : public Network::IoHandle {
}
return io_handle_.recvmmsg(slices, self_port, output);
}
Api::IoCallUint64Result recv(void* buffer, size_t length, int flags) override {
if (closed_) {
ASSERT(false, "recv called after close.");
return Api::IoCallUint64Result(0, Api::IoErrorPtr(new Network::IoSocketError(EBADF),
Network::IoSocketError::deleteIoError));
}
return io_handle_.recv(buffer, length, flags);
}
bool supportsMmsg() const override { return io_handle_.supportsMmsg(); }
bool supportsUdpGro() const override { return io_handle_.supportsUdpGro(); }
Api::SysCallIntResult bind(Network::Address::InstanceConstSharedPtr address) override {
Expand Down
1 change: 1 addition & 0 deletions test/mocks/network/io_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MockIoHandle : public IoHandle {
RecvMsgOutput& output));
MOCK_METHOD(Api::IoCallUint64Result, recvmmsg,
(RawSliceArrays & slices, uint32_t self_port, RecvMsgOutput& output));
MOCK_METHOD(Api::IoCallUint64Result, recv, (void* buffer, size_t length, int flags));
MOCK_METHOD(bool, supportsMmsg, (), (const));
MOCK_METHOD(bool, supportsUdpGro, (), (const));
MOCK_METHOD(Api::SysCallIntResult, bind, (Address::InstanceConstSharedPtr address));
Expand Down

0 comments on commit 9273e6f

Please sign in to comment.