Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

network: add io handle recv function for http inspector #12736

Merged
merged 1 commit into from
Aug 21, 2020
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
8 changes: 8 additions & 0 deletions include/envoy/network/io_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 @@ -43,6 +43,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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class HttpInspectorTest : public testing::Test {
EXPECT_CALL(socket_, detectedTransportProtocol()).WillRepeatedly(Return("raw_buffer"));
EXPECT_CALL(cb_, dispatcher()).WillRepeatedly(ReturnRef(dispatcher_));
EXPECT_CALL(testing::Const(socket_), ioHandle()).WillRepeatedly(ReturnRef(*io_handle_));
EXPECT_CALL(socket_, ioHandle()).WillRepeatedly(ReturnRef(*io_handle_));

if (include_inline_recv) {
EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
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 @@ -28,6 +28,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