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

net/can: Add SO_RCVBUF option for can socket #12465

Merged
merged 1 commit into from
Jun 14, 2024

Conversation

HedongGao
Copy link
Contributor

@HedongGao HedongGao commented Jun 6, 2024

Summary

If the CAN stack receiving packets fast, but the application layer reading packets slow. Then conn->readahead will continue to grow, leading to memory leaks. Finally CAN stack potentially starve out all IOB buffers.

To prevent IOB buffers leaks, users can restrict CAN socket buffer length.

Impact

Rename mm/iob/iob_get_queue_size.c to mm/iob/iob_get_queue_info.c
Discard subsequent CAN frame, when the CAN socket is full.

Testing

CAN socket can cache x packets. x = (NET_MAX_RECV_BUFSIZE + IOB_BUF_SIZE - 1)/IOB_BUF_SIZE
Set NET_MAX_RECV_BUFSIZE = 1024 when make-menuconfig.
CAN Socket could cache 6 packets.

Create a demo program as follows:
`
int main(int argc, char **argv)
{
int fd;
int buf_size;
struct ifreq ifr_can;
struct sockaddr_can can_addr_can;

fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
strcpy(ifr_can.ifr_name, "can0");
ioctl(fd, SIOCGIFINDEX, &ifr_can);
can_addr_can[i].can_family = AF_CAN;
can_addr_can.can_ifindex = ifr_can.ifr_ifindex;
bind(fd, (struct sockaddr*)&can_addr_can, sizeof(can_addr_can));
//setsockopt(fd, SOL_CAN_RAW, SO_RCVBUF, &buf_size, sizeof(buf_size));

while(1){
    sleep(3);
    //Do not read CAN frame or reading speed slower than writing speed.
}
return 0;

}
`
CAN stack potentially starve out all IOB buffers if do not set SO_RCVBUF option.

Copy link
Contributor

@acassis acassis left a comment

Choose a reason for hiding this comment

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

@HedongGao please add that text from Summary into your commit log

If the CAN stack receiving packets fast, but the application layer reading packets slow. Then `conn->readahead` will continue to grow, leading to memory leaks. Finally CAN stack potentially starve out all IOB buffers. To prevent memory leaks, users can restrict can socket buffer length.

Signed-off-by: gaohedong <gaohedong@xiaomi.com>
@HedongGao
Copy link
Contributor Author

Add Summary-text into commit log!

@xiaoxiang781216 xiaoxiang781216 merged commit dc651e0 into apache:master Jun 14, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants