From 28d4703338eb2764babbdeffedc311355902c319 Mon Sep 17 00:00:00 2001 From: Finn Behrens Date: Thu, 22 Apr 2021 22:42:54 +0200 Subject: [PATCH] WIP: drivers/net/dummy_rs Signed-off-by: Finn Behrens --- drivers/net/Kconfig | 16 ++++++ drivers/net/Makefile | 1 + drivers/net/dummy_rs.rs | 112 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 drivers/net/dummy_rs.rs diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig index bcd31f458d1acd..3e7fc2c55a7ab4 100644 --- a/drivers/net/Kconfig +++ b/drivers/net/Kconfig @@ -72,6 +72,22 @@ config DUMMY To compile this driver as a module, choose M here: the module will be called dummy. +config DUMMY_RS + tristate "Dummy net driver support" + depends on HAS_RUST + help + This is essentially a bit-bucket device (i.e. traffic you send to + this device is consigned into oblivion) with a configurable IP + address. It is most commonly used in order to make your currently + inactive SLIP address seem like a real address for local programs. + If you use SLIP or PPP, you might want to say Y here. It won't + enlarge your kernel. What a deal. Read about it in the Network + Administrator's Guide, available from + . + + To compile this driver as a module, choose M here: the module + will be called dummy_rs. + config WIREGUARD tristate "WireGuard secure network tunnel" depends on NET && INET diff --git a/drivers/net/Makefile b/drivers/net/Makefile index f4990ff32fa4cc..8c2f8e10e49617 100644 --- a/drivers/net/Makefile +++ b/drivers/net/Makefile @@ -10,6 +10,7 @@ obj-$(CONFIG_BONDING) += bonding/ obj-$(CONFIG_IPVLAN) += ipvlan/ obj-$(CONFIG_IPVTAP) += ipvlan/ obj-$(CONFIG_DUMMY) += dummy.o +obj-$(CONFIG_DUMMY_RS) += dummy_rs.o obj-$(CONFIG_WIREGUARD) += wireguard/ obj-$(CONFIG_EQUALIZER) += eql.o obj-$(CONFIG_IFB) += ifb.o diff --git a/drivers/net/dummy_rs.rs b/drivers/net/dummy_rs.rs new file mode 100644 index 00000000000000..eb40d1adfb5d22 --- /dev/null +++ b/drivers/net/dummy_rs.rs @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust dummy network driver + +#![no_std] +#![feature(allocator_api, global_asm)] + +use kernel::prelude::*; +use kernel::net::prelude::*; +use kernel::net::device; + +module! { + type: RustNetDummy, + name: b"dummy_rs", + author: b"Rust for Linux Contributors", + description: b"Rust dummy network driver", + license: b"GPL v2", + params: { + numdummies: usize { + default: 0, + permissions: 0o644, + description: b"Number of dummy_rs pseudo devices", + }, + }, +} + +fn setup(dev: &mut NetDevice) { + dev.ether_setup(); + + // Fill in device structure with ethernet-generic values. + dev.add_flag(device::Iff::NOARP); + dev.remove_flag(device::Iff::MULTICAST); + + dev.add_private_flag(device::IffPriv::LIVE_ADDR_CHANGE); + dev.add_private_flag(device::IffPriv::IFF_NO_QUEUE); + + dev.add_feature(device::Feature::NETIF_F_SG); + dev.add_feature(device::Feature::NETIF_F_FRAGLIST_BIT); + dev.add_feature(device::Feature::NETIF_F_GSO_SOFTWARE); +} + +rtnl_link_ops! { + kind: b"dummy_rs", + type: DummyRsDev, + setup: setup, + maxtype: 20, +} + +struct RustNetDummy { + dev: NetDevice, +} + + +impl KernelModule for RustNetDummy { + fn init() -> KernelResult { + { + let lock =THIS_MODULE.kernel_param_lock(); + pr_info!("Rust Network Dummy with {} pseudo devices\n", numdummies.read(&lock)); + } + + let dev = NetDevice::new(DummyRsDev, kernel::cstr!("dummyrs%d"), kernel::net::device::NetNameAssingType::Enum, 1, 1)?; + + if let Err(e) = dev.register() { + pr_warn!("could not register: {}", e.to_kernel_errno()); + } + + Ok(RustNetDummy { + dev, + }) + } +} + +impl Drop for RustNetDummy { + fn drop(&mut self) { + pr_info!("remove rust net dummy"); + // TODO + } +} + + +struct DummyRsDev; + +impl NetDeviceOps for DummyRsDev { + kernel::declare_net_device_ops!(); + + fn init(dev: &NetDevice) -> KernelResult<()> { + Ok(()) + } + + fn uninit(dev: &NetDevice) { + + } +} + +impl NetDeviceAdapter for DummyRsDev { + type Inner = Self; + + type Ops = Self; + + type EthOps = Self; + + fn setup(dev: &mut NetDevice) { + pr_info!("called netdev_setup"); + //dev.rtnl_link_ops = unsafe { &dummy_rs_rtnl_link_ops as *const kernel::bindings::rtnl_link_ops as *mut _ }; + dev.set_rtnl_ops( unsafe { &dummy_rs_rtnl_link_ops }); + + } +} + +impl EthToolOps for DummyRsDev { + kernel::declare_eth_tool_ops!(); +}