|
| 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