[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230710073703.147351-5-fujita.tomonori@gmail.com>
Date: Mon, 10 Jul 2023 16:37:02 +0900
From: FUJITA Tomonori <fujita.tomonori@...il.com>
To: rust-for-linux@...r.kernel.org,
netdev@...r.kernel.org
Cc: kuba@...nel.org,
andrew@...n.ch,
aliceryhl@...gle.com,
miguel.ojeda.sandonis@...il.com,
benno.lossin@...ton.me
Subject: [PATCH v2 4/5] samples: rust: add dummy network driver
This is a simpler version of drivers/net/dummy.c.
This demonstrates the usage of abstractions for network device drivers.
Allows allocator_api feature for Box::try_new();
Signed-off-by: FUJITA Tomonori <fujita.tomonori@...il.com>
---
samples/rust/Kconfig | 13 ++++++
samples/rust/Makefile | 1 +
samples/rust/rust_net_dummy.rs | 75 ++++++++++++++++++++++++++++++++++
scripts/Makefile.build | 2 +-
4 files changed, 90 insertions(+), 1 deletion(-)
create mode 100644 samples/rust/rust_net_dummy.rs
diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index b0f74a81c8f9..02bda7bdf722 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -30,6 +30,19 @@ config SAMPLE_RUST_PRINT
If unsure, say N.
+config SAMPLE_RUST_NET_DUMMY
+ tristate "Dummy network driver"
+ depends on NET
+ help
+ This is the simpler version of drivers/net/dummy.c.
+ No intention to replace it. This provides educational information
+ for Rust abstractions for network device drivers.
+
+ To compile this as a module, choose M here:
+ the module will be called rust_net_dummy.
+
+ If unsure, say N.
+
config SAMPLE_RUST_HOSTPROGS
bool "Host programs"
help
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index 03086dabbea4..440dee2971ba 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -2,5 +2,6 @@
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
+obj-$(CONFIG_SAMPLE_RUST_NET_DUMMY) += rust_net_dummy.o
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs
diff --git a/samples/rust/rust_net_dummy.rs b/samples/rust/rust_net_dummy.rs
new file mode 100644
index 000000000000..78f8f3321fd2
--- /dev/null
+++ b/samples/rust/rust_net_dummy.rs
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+//! Rust dummy netdev.
+
+//use kernel::types::ForeignOwnable;
+use kernel::{
+ c_str,
+ net::dev::{
+ flags, priv_flags, Device, DeviceOperations, EtherOperations, EthtoolTsInfo, Registration,
+ SkBuff, TxCode,
+ },
+ prelude::*,
+};
+
+module! {
+ type: DummyNetdev,
+ name: "rust_net_dummy",
+ author: "Rust for Linux Contributors",
+ description: "Rust dummy netdev",
+ license: "GPL v2",
+}
+
+#[vtable]
+impl DeviceOperations for Box<DriverData> {
+ fn init(dev: Device<Box<DriverData>>) -> Result {
+ // how to access to the driver private data.
+ let _ = dev.drv_priv_data();
+ Ok(())
+ }
+
+ fn start_xmit(_dev: Device<Box<DriverData>>, mut skb: SkBuff) -> TxCode {
+ skb.tx_timestamp();
+ skb.consume();
+ TxCode::Ok
+ }
+}
+
+/// For device driver specific information.
+struct DriverData {}
+
+struct DummyNetdev {
+ _r: Registration<Box<DriverData>>,
+}
+
+#[vtable]
+impl EtherOperations for Box<DriverData> {
+ fn get_ts_info(dev: Device<Box<DriverData>>, mut info: EthtoolTsInfo) -> Result {
+ EthtoolTsInfo::ethtool_op_get_ts_info(&dev, &mut info)
+ }
+}
+
+impl kernel::Module for DummyNetdev {
+ fn init(_module: &'static ThisModule) -> Result<Self> {
+ let data = Box::try_new(DriverData {})?;
+ let mut r = Registration::<Box<DriverData>>::try_new_ether(1, 1, data)?;
+ r.set_ether_operations::<Box<DriverData>>()?;
+
+ let netdev = r.dev_get();
+ netdev.set_name(c_str!("dummy%d"))?;
+
+ netdev.set_flags(netdev.get_flags() | flags::IFF_NOARP & !flags::IFF_MULTICAST);
+ netdev.set_priv_flags(
+ netdev.get_priv_flags() | priv_flags::IFF_LIVE_ADDR_CHANGE | priv_flags::IFF_NO_QUEUE,
+ );
+ netdev.set_random_eth_hw_addr();
+ netdev.set_min_mtu(0);
+ netdev.set_max_mtu(0);
+
+ r.register()?;
+
+ // TODO: Replaces pr_info with the wrapper of netdev_info().
+ pr_info!("Hello Rust dummy netdev!");
+ Ok(DummyNetdev { _r: r })
+ }
+}
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 78175231c969..1404967e908e 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -277,7 +277,7 @@ $(obj)/%.lst: $(src)/%.c FORCE
# Compile Rust sources (.rs)
# ---------------------------------------------------------------------------
-rust_allowed_features := new_uninit
+rust_allowed_features := allocator_api,new_uninit
rust_common_cmd = \
RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
--
2.34.1
Powered by blists - more mailing lists