Skip to content

vhost-device-can: use the same socket for send/recv#937

Open
MatiasVara wants to merge 5 commits intorust-vmm:mainfrom
MatiasVara:fix-for-925
Open

vhost-device-can: use the same socket for send/recv#937
MatiasVara wants to merge 5 commits intorust-vmm:mainfrom
MatiasVara:fix-for-925

Conversation

@MatiasVara
Copy link
Contributor

@MatiasVara MatiasVara commented Feb 23, 2026

Summary of the PR

Use the same socket for send/receive in the backend to prevent guest to receiving two copies of the message when it sends to the local CAN bus.

Fixes #925

Reported-by: Francesco Valla francesco@valla.it

Requirements

Before submitting your PR, please make sure you addressed the following
requirements:

  • All commits in this PR have Signed-Off-By trailers (with
    git commit -s), and the commit message has max 60 characters for the
    summary and max 75 characters for each description line.
  • All added/changed functionality has a corresponding unit/integration
    test.
  • All added/changed public-facing functionality has entries in the "Upcoming
    Release" section of CHANGELOG.md (if no such section exists, please create one).
  • Any newly added unsafe code is properly documented.

@MatiasVara
Copy link
Contributor Author

When fixing this, some tests started to fail since now the opening of the socket is happening in new(). Before, those tests did not require to open a socket to work. I fixed those tests by adding a few commits together with the fix. I wonder if these tests should be fixed here or in another PR.

@MatiasVara
Copy link
Contributor Author

To run the tests, we require a virtual can interface in the host named can0.

@stefano-garzarella
Copy link
Member

stefano-garzarella commented Mar 4, 2026

To run the tests, we require a virtual can interface in the host named can0.

Is this something new (i.e. added here) or was there before this PR?
If it's something new, I think we need to update the rust-vmm-container image to provide that device.

@MatiasVara
Copy link
Contributor Author

To run the tests, we require a virtual can interface in the host named can0.

Is this something new (i.e. added here) or was there before this PR? If it's something new, I think we need to update the rust-vmm-container image to provide that device.

It was in the code before but it was not being tested. The tests were done to not use a can interface. I am not sure if that was done on purpose.

@epilys
Copy link
Member

epilys commented Mar 4, 2026

I think we should wrap the CanFdSocket field into an enum, and add a #[cfg(test)] variant that is a mock socket.

This way we can add support in the future for classic Can sockets (i.e. not FD) which is something I wanted to do anyway.

@stefano-garzarella
Copy link
Member

To run the tests, we require a virtual can interface in the host named can0.

Is this something new (i.e. added here) or was there before this PR? If it's something new, I think we need to update the rust-vmm-container image to provide that device.

It was in the code before but it was not being tested. The tests were done to not use a can interface. I am not sure if that was done on purpose.

Maybe to avoid updating the container image xD (I have no idea).
So can we enable some can0 stub/looback interface in the container?

@epilys
Copy link
Member

epilys commented Mar 4, 2026

To run the tests, we require a virtual can interface in the host named can0.

Is this something new (i.e. added here) or was there before this PR? If it's something new, I think we need to update the rust-vmm-container image to provide that device.

It was in the code before but it was not being tested. The tests were done to not use a can interface. I am not sure if that was done on purpose.

Maybe to avoid updating the container image xD (I have no idea). So can we enable some can0 stub/looback interface in the container?

If the linux images contain the vcan kernel driver yes:

sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set vcan0 up

@MatiasVara
Copy link
Contributor Author

I think we should wrap the CanFdSocket field into an enum, and add a #[cfg(test)] variant that is a mock socket.

Thanks! I'll try that. I did not get it completely yet though.

Use the same socket for send/receive in the backend to prevent the guest
from receiving two copies of the message when it sends to the local CAN
bus. Fix tests and remove test_can_read_socket_fail() test since
read_can_socket() does not open the socket anymore.

Fixes rust-vmm#925

Reported-by: Francesco Valla <francesco@valla.it>
Signed-off-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
Most of the tests were using `can0` as interface but some used `can`.
Use `can0` interface for all tests and `canx` for those that expect an
interface down.

Signed-off-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
To support 64 bytes packet, set CAN_FRMF_TYPE_FD flag in
test_can_controller_operation test.

Signed-off-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
Rename this test that previously had to fail since the interface was
down. The previous test was testing the NoOutputInterface path which
does not exist anymore. Make the test pass when transmission succeeds.

Signed-off-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
Add a mock socket for tests so that tests no longer require a CAN
interface on the host.

Signed-off-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
@MatiasVara
Copy link
Contributor Author

MatiasVara commented Mar 11, 2026

The PR has now a wider scope :(. I am OK to split if it is required. Bear in mind that I used Claude for the last commit. Claude proposed a single builder for both socket types but I ignored it for the moment.

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.

vhost-device-can: messages sent from the guest are always looped back

3 participants