-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
net: sockets: Update msg_controllen in recvmsg properly #77345
net: sockets: Update msg_controllen in recvmsg properly #77345
Conversation
The change from this PR is causing a regression is some socket tests. when running @jukkar @awojasinski what do you think? |
I tried to clear the buffer, and the test can pass. I push it so you can review. |
I tried to find answer when the
Also recvmsg(2) says
So if I am reading this correctly, if the call is successfull, we should not clear the value.
|
Indeed, that could also be a problem. |
Are you sure this patch satisfies the following statement:
I might be missing something, but I don't see where the sum of CMSG_SPACE is computed and set to msg_controllen. Maybe before returning from recvmsg, we should go through all the cmsg buffer and compute the sum of the different control data length. Am I missing something ? |
Yes, that is missing. The original code had only support for one ancillary message, and the caller should have been allocating space for it, but now there could be two so we should update the controllen correctly. |
I'll cook up something in my tomorrow, I got a rough patch which makes things pass and should correctly update the controllen, will keep you updated. |
01b8f8b
to
8be4ab7
Compare
Fix updated, I updated With this version of the fix, the unit test are passing on my side, will get the full confirmation with the PR checks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please fix compliance check errors
02a7a32
to
3acae5c
Compare
In last push I fixed the compliance checks and also pushed an update I forgot to add in the patch (the test would fail without it) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
subsys/net/lib/sockets/sockets.c
Outdated
@@ -1572,21 +1580,28 @@ static inline ssize_t zsock_recv_dgram(struct net_context *ctx, | |||
if (msg != NULL) { | |||
if (msg->msg_control != NULL) { | |||
if (msg->msg_controllen > 0) { | |||
bool clear_controllen = true; | |||
bool clear_controllen = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
after some more thought, I think this clear_controllen
logic is not working well.
If we have both TIMESTAMPING and RECV_PKTINFO enabled, we might end up in some corner cases when we clear msg_controllen
even if we shouldn't.
The only safe way to implement this seems to compute the control data length in this function
subsys/net/lib/sockets/sockets.c
Outdated
} | ||
} else { | ||
clear_controllen = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as an example for my previous comment, if we have both timestamping and pkt_info enabled, we can have a case where we add timestamping info, then there are no pkt_info to add so we go to this branch and we would clear the control data, including the timestamping.
3acae5c
to
3a6827d
Compare
In the last push, I reworked the whole fix and now I think it covers all cases.
To do so, I implemented I also implemented |
3a6827d
to
86716c0
Compare
According to recvmsg man page, msg_controllen should be set to the sum of the length of all control messages in the buffer. This is missing from the current recvmsg implementation. This commit aims to fix this by updating msg_controllen each time control data are added to the buffer. This commit also fixes cases where the msg_controllen is cleared incorrectly. Fixes zephyrproject-rtos#77303 Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
86716c0
to
7d3d37b
Compare
I don't think this is a valid use case, what I mean by that is that the control data is always tied to the data that we just received, so the user should read all the control data after each recvmsg because it would be useless in the next recvmsg call. |
Ok that makes sense. But who is responsible to clear the control data buffer ? The user or recvmsg ? Couldn't find the answer. |
I think it can be only the caller, it only knows what to do with the data. So if we assume that, then your code is working fine by not overriding the control data. |
Then I think we need to update the unit tests as the control data buffer is not always cleared between calls to recvmsg. |
Currently, the test doesn't clear the control data buffer before calling recvmsg. This leads to recvmsg being unable to add the new control data, which corresponds to the current received data. This commit aims to clear the control data buffer to match most use cases, when the control data buffer is empty before calling recvmsg. Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
If adding any control data like timestamping or pkt_info fails, the msg_controllen wasn't properly cleared.
This commit aims to fix this by clearing the msg_controllen only when adding control data is successful.
Fixes #77303
Author: @axelnxp
(submitted on behalf because of some nxp github policies delaying his ability to post a PR)