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] Add Ethernet lengths and Header/Address types #1014

Draft
wants to merge 1 commit into
base: rust
Choose a base branch
from

Conversation

Lymkwi
Copy link

@Lymkwi Lymkwi commented Jun 13, 2023

(proper patch note below)

I know this repo is sort of meant to stay as an archive as development has moved to mainline. I'm only based on rust because I needed the fundamentals of net.

This PR is just a showcase of some of the most uncontroversial and menial work (the kind you script with sed eventually) I had to do in the course of a work project where I wrote the necessary abstractions to have a tunneling driver (including RTNL, NAPI, some Device stuff, some SkBuff stuff, UDP Sockets, etc) working in Rust for Linux. I know the infrastructure for net is slowly being added to rust-next by Tomonori FUJITA[1], and that base is needed for me to even begin to consider the idea of presenting any of the rest I have to that branch.

I'm just looking for feedback, like protocol names, conventions used for naming structs, methods and enums. Anything that can be improved.

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/T/#t


This patch adds a module to the net module of kernel that provides enumerations with constants for ethernet lengths (address length, type length, etc), as well as the enumeration of all protocols currently understood by the network stack. The latter are represented as 16-bit integers in memory to simplify conversion to and from the types read directly from Ethernet II frames. This patch also contains a conversion from/to u16.

The ethernet module also contains a Header abstraction that just wraps around struct ethhdr defined in the UAPI. There is a simple builder function implemented as well as getters for src/dst address, and protocol number.

rust/kernel/net/ethernet.rs Outdated Show resolved Hide resolved
rust/kernel/net/ethernet.rs Outdated Show resolved Hide resolved
rust/kernel/net/ethernet.rs Outdated Show resolved Hide resolved
@Lymkwi
Copy link
Author

Lymkwi commented Jun 13, 2023

Force-pushed with changes suggested by @bjorn3 .

EDIT: I missed the answer to whether or not to use usize for length constants, so I changed it again.

@ojeda
Copy link
Member

ojeda commented Jun 13, 2023

I know this repo is sort of meant to stay as an archive as development has moved to mainline.

Nit: to be clear, that applies to the rust branch, no the entire repo (we still use the issue tracker and have the other branches here).

@Lymkwi
Copy link
Author

Lymkwi commented Jun 13, 2023

I know this repo is sort of meant to stay as an archive as development has moved to mainline.

Nit: to be clear, that applies to the rust branch, no the entire repo (we still use the issue tracker and have the other branches here).

My bad, I forgot that rust-next and the issue tracker, among other things, are still hosted here.

/// Return the unspecified Ethernet address
pub const fn get_address_unspecified() -> Address {
Address([0x00; ALEN])
}
Copy link
Member

Choose a reason for hiding this comment

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

I was originally thinking about const ADDRESS_UNSPECIFIED: Address = Address([0x00; ALEN]); but this works too I guess. By the way does the C side have similar constants, or is it manually written everywhere?

Edit: Found eth_zero_addr() and eth_broadcast_addr().

Copy link
Author

Choose a reason for hiding this comment

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

Yep! Those are the functions I originally copied from C, which is why I thought about making them functions first.

I could make them into associated methods to Address though? That'd mirror the way Ipv4Addr works IIRC

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. And yeah, making them methods would be nice.

Copy link
Author

Choose a reason for hiding this comment

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

I moved them to Address::broadcast() and Address::unspecified().

This patch adds a module to the `net` module of `kernel` that provides
constants for ethernet lengths (address length, type length, etc), as
well as the enumeration of all protocols currently understood by the
network stack. The latter are represented as 16-bit integers in memory
to simplify conversion to and from the types read directly from Ethernet
II frames. This patch also contains a conversion from/to `u16`.

The `ethernet` module also contains a `Header` abstraction that just
wraps around `struct ethhdr` defined in the UAPI. There is a simple
builder function implemented as well as getters for src/dst address, and
protocol number.

Signed-off-by: Amélie Gonzalez <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants