Skip to content

Commit b6e0242

Browse files
committed
WIP: drivers/net/dummy_rs
Signed-off-by: Finn Behrens <[email protected]>
1 parent 8b993a1 commit b6e0242

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

Diff for: drivers/net/Kconfig

+16
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ config DUMMY
7272
To compile this driver as a module, choose M here: the module
7373
will be called dummy.
7474

75+
config DUMMY_RS
76+
tristate "Dummy net driver support"
77+
depends on HAS_RUST
78+
help
79+
This is essentially a bit-bucket device (i.e. traffic you send to
80+
this device is consigned into oblivion) with a configurable IP
81+
address. It is most commonly used in order to make your currently
82+
inactive SLIP address seem like a real address for local programs.
83+
If you use SLIP or PPP, you might want to say Y here. It won't
84+
enlarge your kernel. What a deal. Read about it in the Network
85+
Administrator's Guide, available from
86+
<http://www.tldp.org/docs.html#guide>.
87+
88+
To compile this driver as a module, choose M here: the module
89+
will be called dummy_rs.
90+
7591
config WIREGUARD
7692
tristate "WireGuard secure network tunnel"
7793
depends on NET && INET

Diff for: drivers/net/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ obj-$(CONFIG_BONDING) += bonding/
1010
obj-$(CONFIG_IPVLAN) += ipvlan/
1111
obj-$(CONFIG_IPVTAP) += ipvlan/
1212
obj-$(CONFIG_DUMMY) += dummy.o
13+
obj-$(CONFIG_DUMMY_RS) += dummy_rs.o
1314
obj-$(CONFIG_WIREGUARD) += wireguard/
1415
obj-$(CONFIG_EQUALIZER) += eql.o
1516
obj-$(CONFIG_IFB) += ifb.o

Diff for: drivers/net/dummy_rs.rs

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust dummy network driver
4+
5+
#![no_std]
6+
#![feature(allocator_api, global_asm)]
7+
8+
use kernel::prelude::*;
9+
use kernel::net::prelude::*;
10+
use kernel::net::device;
11+
12+
module! {
13+
type: RustNetDummy,
14+
name: b"dummy_rs",
15+
author: b"Rust for Linux Contributors",
16+
description: b"Rust dummy network driver",
17+
license: b"GPL v2",
18+
params: {
19+
numdummies: usize {
20+
default: 0,
21+
permissions: 0o644,
22+
description: b"Number of dummy_rs pseudo devices",
23+
},
24+
},
25+
}
26+
27+
fn setup(dev: &mut NetDevice<DummyRsDev>) {
28+
dev.ether_setup();
29+
30+
// Fill in device structure with ethernet-generic values.
31+
dev.add_flag(device::Iff::NOARP);
32+
dev.remove_flag(device::Iff::MULTICAST);
33+
34+
dev.add_private_flag(device::IffPriv::LIVE_ADDR_CHANGE);
35+
dev.add_private_flag(device::IffPriv::IFF_NO_QUEUE);
36+
37+
dev.add_feature(device::Feature::NETIF_F_SG);
38+
dev.add_feature(device::Feature::NETIF_F_FRAGLIST_BIT);
39+
dev.add_feature(device::Feature::NETIF_F_GSO_SOFTWARE);
40+
}
41+
42+
rtnl_link_ops! {
43+
kind: b"dummy_rs",
44+
type: DummyRsDev,
45+
setup: setup,
46+
maxtype: 20,
47+
}
48+
49+
struct RustNetDummy {
50+
dev: NetDevice<DummyRsDev>,
51+
}
52+
53+
54+
impl KernelModule for RustNetDummy {
55+
fn init() -> KernelResult<Self> {
56+
{
57+
let lock =THIS_MODULE.kernel_param_lock();
58+
pr_info!("Rust Network Dummy with {} pseudo devices\n", numdummies.read(&lock));
59+
}
60+
61+
let dev = NetDevice::new(DummyRsDev, kernel::cstr!("dummyrs%d"), kernel::net::device::NetNameAssingType::Enum, 1, 1)?;
62+
63+
if let Err(e) = dev.register() {
64+
pr_warn!("could not register: {}", e.to_kernel_errno());
65+
}
66+
67+
Ok(RustNetDummy {
68+
dev,
69+
})
70+
}
71+
}
72+
73+
impl Drop for RustNetDummy {
74+
fn drop(&mut self) {
75+
pr_info!("remove rust net dummy");
76+
// TODO
77+
}
78+
}
79+
80+
81+
struct DummyRsDev;
82+
83+
impl NetDeviceOps<Self> for DummyRsDev {
84+
kernel::declare_net_device_ops!();
85+
86+
fn init(dev: &NetDevice<Self>) -> KernelResult<()> {
87+
Ok(())
88+
}
89+
90+
fn uninit(dev: &NetDevice<Self>) {
91+
92+
}
93+
}
94+
95+
impl NetDeviceAdapter for DummyRsDev {
96+
type Inner = Self;
97+
98+
type Ops = Self;
99+
100+
type EthOps = Self;
101+
102+
fn setup(dev: &mut NetDevice<Self>) {
103+
pr_info!("called netdev_setup");
104+
//dev.rtnl_link_ops = unsafe { &dummy_rs_rtnl_link_ops as *const kernel::bindings::rtnl_link_ops as *mut _ };
105+
dev.set_rtnl_ops( unsafe { &dummy_rs_rtnl_link_ops });
106+
107+
}
108+
}
109+
110+
impl EthToolOps<Self> for DummyRsDev {
111+
kernel::declare_eth_tool_ops!();
112+
}

0 commit comments

Comments
 (0)