|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Rust dummy network driver |
| 4 | +//! |
| 5 | +//! This is a demonstration of what a small driver looks like in Rust, based on drivers/net/dummy.c. |
| 6 | +//! This code is provided as a demonstration only, not as a proposal to mass-rewrite existing drivers in Rust |
| 7 | +
|
| 8 | +// TODO: copyright (see ./dummy.c) |
| 9 | + |
| 10 | +#![no_std] |
| 11 | +#![feature(allocator_api, global_asm)] |
| 12 | + |
| 13 | +use core::ops::Deref; |
| 14 | + |
| 15 | +use kernel::net::device; |
| 16 | +use kernel::net::prelude::*; |
| 17 | +use kernel::net::rtnl; |
| 18 | +use kernel::Error; |
| 19 | +use kernel::{ |
| 20 | + net::netlink::{NlAttrVec, NlExtAck}, |
| 21 | + prelude::*, |
| 22 | +}; |
| 23 | + |
| 24 | +module! { |
| 25 | + type: RustNetDummy, |
| 26 | + name: b"dummy_rs", |
| 27 | + author: b"Rust for Linux Contributors", |
| 28 | + description: b"Rust dummy network driver", |
| 29 | + license: b"GPL v2", |
| 30 | + alias_rtnl_link: b"dummy_rs", |
| 31 | + params: { |
| 32 | + numdummies: usize { |
| 33 | + default: 0, |
| 34 | + permissions: 0, |
| 35 | + description: b"Number of dummy_rs pseudo devices", |
| 36 | + }, |
| 37 | + }, |
| 38 | +} |
| 39 | + |
| 40 | +fn setup(dev: &mut NetDevice<DummyRsDev>) { |
| 41 | + dev.ether_setup(); |
| 42 | + |
| 43 | + dev.set_ops(); |
| 44 | + |
| 45 | + // Fill in device structure with ethernet-generic values. |
| 46 | + dev.add_flag(device::Iff::NOARP); |
| 47 | + dev.remove_flag(device::Iff::MULTICAST); |
| 48 | + |
| 49 | + dev.add_private_flag(device::IffPriv::LIVE_ADDR_CHANGE); |
| 50 | + dev.add_private_flag(device::IffPriv::NO_QUEUE); |
| 51 | + |
| 52 | + let mut feature = device::feature::NetIF::new(); |
| 53 | + |
| 54 | + //feature.add(device::feature::NETIF_F_SG); |
| 55 | + //feature.add(device::feature::NETIF_F_FRAGLIST_BIT as u64); |
| 56 | + feature += device::feature::NETIF_F_SG; |
| 57 | + feature += device::feature::NETIF_F_FRAGLIST; |
| 58 | + feature += device::feature::NETIF_F_GSO_SOFTWARE; |
| 59 | + feature += device::feature::NETIF_F_HW_CSUM; |
| 60 | + feature += device::feature::NETIF_F_HIGHDMA; |
| 61 | + feature += device::feature::NETIF_F_LLTX; |
| 62 | + feature += device::feature::NETIF_F_GSO_ENCAP_ALL; |
| 63 | + |
| 64 | + dev.set_features(feature); |
| 65 | + dev.set_hw_features(feature); |
| 66 | + dev.set_hw_enc_features(feature); |
| 67 | + |
| 68 | + dev.hw_addr_random(); |
| 69 | + dev.set_mtu(0, 0); |
| 70 | +} |
| 71 | + |
| 72 | +fn validate(tb: &NlAttrVec, _data: &NlAttrVec, _ext_ack: &NlExtAck) -> KernelResult<()> { |
| 73 | + if let Some(addr) = tb.get(kernel::bindings::IFLA_ADDRESS) { |
| 74 | + if addr.nla_len() != kernel::net::netlink::ETH_ALEN { |
| 75 | + return Err(Error::EINVAL); |
| 76 | + } |
| 77 | + if !addr.is_valid_ether_addr() { |
| 78 | + return Err(Error::EADDRNOTAVAIL); |
| 79 | + } |
| 80 | + } |
| 81 | + Ok(()) |
| 82 | +} |
| 83 | + |
| 84 | +rtnl_link_ops! { |
| 85 | + kind: b"dummy_rs", |
| 86 | + type: DummyRsDev, |
| 87 | + setup: setup, |
| 88 | + validate: validate, |
| 89 | +} |
| 90 | + |
| 91 | +struct RustNetDummy { |
| 92 | + //dev: NetDevice<DummyRsDev>, |
| 93 | +} |
| 94 | + |
| 95 | +impl KernelModule for RustNetDummy { |
| 96 | + fn init() -> KernelResult<Self> { |
| 97 | + let num = *numdummies.read(); |
| 98 | + |
| 99 | + unsafe { dummy_rs_link_ops.register() }?; |
| 100 | + |
| 101 | + for _ in 0..(num) { |
| 102 | + let dev = NetDevice::new( |
| 103 | + DummyRsDev, |
| 104 | + kernel::cstr!("dummyrs%d"), |
| 105 | + kernel::net::device::NetNameAssingType::Enum, |
| 106 | + 1, |
| 107 | + 1, |
| 108 | + )?; |
| 109 | + dev.set_rtnl_ops(unsafe { &dummy_rs_link_ops }); |
| 110 | + |
| 111 | + if let Err(e) = dev.register() { |
| 112 | + pr_warn!("could not register: {}", e.to_kernel_errno()); |
| 113 | + return Err(e); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + Ok(RustNetDummy { |
| 118 | + //dev, |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl Drop for RustNetDummy { |
| 124 | + fn drop(&mut self) { |
| 125 | + // TODO: remove unsafe somehow |
| 126 | + unsafe { dummy_rs_link_ops.unregister() }; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +struct DummyRsDev; |
| 131 | + |
| 132 | +impl NetDeviceOps<Self> for DummyRsDev { |
| 133 | + kernel::declare_net_device_ops!( |
| 134 | + get_stats64, |
| 135 | + change_carrier, |
| 136 | + validate_addr, |
| 137 | + set_mac_addr, |
| 138 | + set_rx_mode |
| 139 | + ); |
| 140 | + |
| 141 | + fn init(dev: &mut NetDevice<Self>) -> KernelResult<()> { |
| 142 | + dev.set_new_pcpu_lstats()?; |
| 143 | + Ok(()) |
| 144 | + } |
| 145 | + |
| 146 | + fn uninit(dev: &mut NetDevice<Self>) { |
| 147 | + unsafe { dev.free_lstats() }; |
| 148 | + } |
| 149 | + |
| 150 | + fn start_xmit(skb: SkBuff, dev: &mut NetDevice<Self>) -> kernel::net::device::NetdevTX { |
| 151 | + let mut skb = skb; |
| 152 | + |
| 153 | + dev.lstats_add(skb.len()); |
| 154 | + |
| 155 | + skb.tx_timestamp(); |
| 156 | + drop(skb); |
| 157 | + |
| 158 | + kernel::net::device::NetdevTX::TX_OK |
| 159 | + } |
| 160 | + |
| 161 | + fn get_stats64(dev: &NetDevice<Self>, stats: &mut rtnl::RtnlLinkStats64) { |
| 162 | + stats.dev_read(dev); |
| 163 | + } |
| 164 | + |
| 165 | + fn change_carrier(dev: &mut NetDevice<Self>, new_carrier: bool) -> KernelResult<()> { |
| 166 | + dev.carrier_set(new_carrier); |
| 167 | + |
| 168 | + Ok(()) |
| 169 | + } |
| 170 | + |
| 171 | + fn validate_addr(dev: &NetDevice<Self>) -> KernelResult<()> { |
| 172 | + device::helpers::eth_validate_addr(dev) |
| 173 | + } |
| 174 | + |
| 175 | + fn set_mac_addr( |
| 176 | + dev: &mut NetDevice<Self>, |
| 177 | + p: *mut kernel::c_types::c_void, |
| 178 | + ) -> KernelResult<()> { |
| 179 | + device::helpers::eth_mac_addr(dev, p) |
| 180 | + } |
| 181 | + |
| 182 | + // [Someting about faking multicast](https://elixir.bootlin.com/linux/v5.12-rc4/source/drivers/net/dummy.c#L48). |
| 183 | + fn set_rx_mode(_dev: &mut NetDevice<Self>) {} |
| 184 | +} |
| 185 | + |
| 186 | +impl NetDeviceAdapter for DummyRsDev { |
| 187 | + type Inner = Self; |
| 188 | + |
| 189 | + type Ops = Self; |
| 190 | + |
| 191 | + type EthOps = Self; |
| 192 | + |
| 193 | + fn setup(dev: &mut NetDevice<Self>) { |
| 194 | + setup(dev); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +impl EthToolOps<Self> for DummyRsDev { |
| 199 | + kernel::declare_eth_tool_ops!(get_drvinfo, get_ts_info); |
| 200 | + |
| 201 | + fn get_drvinfo(_dev: &NetDevice<Self>, info: &mut ethtool::EthtoolDrvinfo) { |
| 202 | + // TODO: how to do this more efficient without unsafe? |
| 203 | + // FIXME: !! |
| 204 | + let info: &kernel::bindings::ethtool_drvinfo = info.deref(); |
| 205 | + unsafe { |
| 206 | + kernel::bindings::strlcpy( |
| 207 | + &(info.driver) as *const _ as *mut i8, |
| 208 | + b"dummy_rs\0" as *const _ as *mut i8, |
| 209 | + 32, |
| 210 | + ); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + fn get_ts_info(dev: &NetDevice<Self>, info: &mut ethtool::EthToolTsInfo) -> KernelResult<()> { |
| 215 | + kernel::net::ethtool::helpers::ethtool_op_get_ts_info(dev, info) |
| 216 | + } |
| 217 | +} |
0 commit comments