Skip to content

net: refactor to simplify lifetimes/generics. #3329

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

Merged
merged 2 commits into from
Sep 16, 2024
Merged

net: refactor to simplify lifetimes/generics. #3329

merged 2 commits into from
Sep 16, 2024

Conversation

Dirbaio
Copy link
Member

@Dirbaio Dirbaio commented Sep 11, 2024

Refactor embassy-net api. Advantages:

  • Now there's a separate Runner struct. run() requires &mut self, this prevents the user from doing weird stuff like calling run() in two tasks concurrently.
  • Stack is now Copy, so you can pass it by value everywhere. This means you need only one StaticCell (for the resources) instead of two (resources, stack).
  • Stack is no longer generic over the driver, only the runner is. This makes code using embassy-net agnostic to what driver is in use, only the code creating the stack has to care.
  • DnsSocket is not generic over the driver anymore.
  • No forced 'static litetime anymore. This means you can now dynamically create/teardown the network stack as many times as you want. If you run everything in a single task with join you don't need StaticCell at all.
  • Stack and the sockets are all covariant over the lifetime now. This helps a bit with avoiding hairy lifetime errors.

Dirbaio added a commit to smoltcp-rs/smoltcp that referenced this pull request Sep 11, 2024
Instead of eagerly sending the join/leave packet when the user calls join/leave,
we update internal state and send the packet when the interface is polled.

Advantages:
- If the device is exhausted, the packet gets sent later instead of just failing and returning an error to the user.
- Makes the API consistent with everything else in smoltcp: operations only update internal state, poll is what sends/receives packets.
- Enables wrappers to offer simpler APIs with less generics. See embassy-rs/embassy#3329 for an example, which is my original motivation.
Dirbaio added a commit to smoltcp-rs/smoltcp that referenced this pull request Sep 12, 2024
Instead of eagerly sending the join/leave packet when the user calls join/leave,
we update internal state and send the packet when the interface is polled.

Advantages:
- If the device is exhausted, the packet gets sent later instead of just failing and returning an error to the user.
- Makes the API consistent with everything else in smoltcp: operations only update internal state, poll is what sends/receives packets.
- Enables wrappers to offer simpler APIs with less generics. See embassy-rs/embassy#3329 for an example, which is my original motivation.
@Dirbaio Dirbaio force-pushed the net-deinit branch 3 times, most recently from 8f0bb04 to 115afdc Compare September 16, 2024 19:14
@Dirbaio
Copy link
Member Author

Dirbaio commented Sep 16, 2024

seems it improves perf a bit, too:

embassy-net [stm32 eth perf test](https://github.com/embassy-rs/embassy/blob/main/tests/stm32/src/bin/eth.rs), stm32h755zi, 64k tcp buffer:

smoltcp 0.11.0:
    download:        6273 kB/s  6264 kB/s
    upload:          9619 kB/s  9619 kB/s
    upload+download: 1297 kB/s  1575 kB/s

smoltcp git main 469dccd
    download:        5357 kB/s  5354 kB/s
    upload:          9678 kB/s  9588 kB/s
    upload+download: 1098 kB/s  1122 kB/s

smoltcp https://github.com/smoltcp-rs/smoltcp/pull/991
    download:        5826 kB/s  5822 kB/s
    upload:          9142 kB/s  9145 kB/s
    upload+download: 3043 kB/s  2729 kB/s

smoltcp https://github.com/smoltcp-rs/smoltcp/pull/991 + embassy-net refactor (this PR)
    download:        6148 kB/s  6147 kB/s
    upload:          9678 kB/s  9676 kB/s
    upload+download: 4691 kB/s  4752 kB/s

@Dirbaio Dirbaio marked this pull request as ready for review September 16, 2024 19:33
@Dirbaio
Copy link
Member Author

Dirbaio commented Sep 16, 2024

bender run

Copy link
Member

@lulf lulf left a comment

Choose a reason for hiding this comment

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

Changes look great!

@Dirbaio
Copy link
Member Author

Dirbaio commented Sep 16, 2024

bender run

@Dirbaio Dirbaio added this pull request to the merge queue Sep 16, 2024
Merged via the queue into main with commit e90b3bc Sep 16, 2024
6 checks passed
@Dirbaio Dirbaio deleted the net-deinit branch September 16, 2024 19:47
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.

2 participants