Skip to content

[poc] socket: mimic MSG_PEEK by read for windows#18305

Closed
soulxu wants to merge 3 commits intoenvoyproxy:mainfrom
soulxu:peek_on_windows
Closed

[poc] socket: mimic MSG_PEEK by read for windows#18305
soulxu wants to merge 3 commits intoenvoyproxy:mainfrom
soulxu:peek_on_windows

Conversation

@soulxu
Copy link
Copy Markdown
Member

@soulxu soulxu commented Sep 29, 2021

Commit Message: socket: mimic MSG_PEEK by read for windows
Additional Description:
Windows only support level triggered event. The edge triggered event was emulated by the Envoy. But it depends on the envoy code read all the data out of socket buffer, then the Read event won't be triggered again. But for listener filter, we need to peek the data, the peek won't consume the data from the socket buffer, the Read event will be triggered again, and becomes a busy loop.
This was found in the PR below:

#17395 (comment)

This PR is trying to mimic the MSG_PEEK by a normal read.

Risk Level: high
Testing:
Docs Changes:
Release Notes:

Signed-off-by: He Jie Xu <hejie.xu@intel.com>
Signed-off-by: He Jie Xu <hejie.xu@intel.com>
@repokitteh-read-only
Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #18305 was opened by soulxu.

see: more, trace.

Signed-off-by: He Jie Xu <hejie.xu@intel.com>
Copy link
Copy Markdown
Member

@davinci26 davinci26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some questions I have.

length = length - copy_size;
} else {
if (file_event_) {
file_event_->registerEventIfEmulatedEdge(Event::FileReadyType::Read);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general it is assumed that it is a responsibility of the caller to re-active the event if they expect more data unless the error is WOULD_BLOCK

In the case of tls/proxy inspectors they should be able to determine that the data they currently have are not available to do their work and they should re-enable read. This is something that connection_impl does well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, same as the previous one, whether we should emulate all the cases. or we can just add assert here, assume people always read the data until WOULD_BLOCK return.

}

Api::IoCallUint64Result IoSocketHandleImpl::recv(void* buffer, size_t length, int flags) {
if constexpr (Event::PlatformDefaultTriggerType == Event::FileTriggerType::EmulatedEdge) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if we should have a dedicated peek function for this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, agree with you.

if (buffer_->length() > 0) {
auto move_length = std::min(buffer_->length(), max_length);
buffer.move(*buffer_);
if (file_event_) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is needed. The caller should be reading until EAGAIN is returned

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emm...good point, should we emulate the case if the caller provides a small max_length?

@soulxu
Copy link
Copy Markdown
Member Author

soulxu commented Oct 6, 2021

@davinci26 thanks for the review!

sorry for reply late, this week is holiday in China, I will back to work next week.

@davinci26
Copy link
Copy Markdown
Member

@soulxu if you are busy let me know and I can take over this PR

@soulxu
Copy link
Copy Markdown
Member Author

soulxu commented Oct 18, 2021

@soulxu if you are busy let me know and I can take over this PR

thanks! I almost done a version, I should be able to send out today. I should ask your help in the beginning, my windows dev env is on my laptop, and my laptop is too slow~, anyway let me push out this version. if there are more things to do, I will appreciate you can help it.

@davinci26
Copy link
Copy Markdown
Member

Awesome no pressure, I just wanted to make sure this does not go stale because it is not high enough priority for you. Anytime, please involve me!

ggreenway pushed a commit that referenced this pull request Nov 2, 2021
In preparation for #18305, I split the implementation of io_socket_handle on different platforms to make the code more readable and separated. The new Windows class leverages the common implementation and now is responsible for registering the events.

This will allow us to cleanly implement MSG_PEEK by read in a consequent PR in a self-contained way.

Signed-off-by: Sotiris Nanopoulos <sonanopo@microsoft.com>
@github-actions
Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions bot added the stale stalebot believes this issue/PR has not been touched recently label Nov 18, 2021
@soulxu soulxu closed this Nov 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale stalebot believes this issue/PR has not been touched recently

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants