Skip to content

Commit 7b00809

Browse files
committed
Add rust net_device wrappers
Signed-off-by: Finn Behrens <me@kloenk.de>
1 parent e97ba67 commit 7b00809

File tree

15 files changed

+2276
-110
lines changed

15 files changed

+2276
-110
lines changed

rust/helpers.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include <linux/mutex.h>
1212
#include <linux/platform_device.h>
1313
#include <linux/security.h>
14+
#include <linux/netdevice.h>
15+
#include <linux/etherdevice.h>
16+
#include <linux/rtnetlink.h>
1417

1518
void rust_helper_BUG(void)
1619
{
@@ -212,6 +215,34 @@ int rust_helper_security_binder_transfer_file(struct task_struct *from,
212215
}
213216
EXPORT_SYMBOL_GPL(rust_helper_security_binder_transfer_file);
214217

218+
void *rust_helper_netdev_priv(struct net_device *dev)
219+
{
220+
return netdev_priv(dev);
221+
}
222+
EXPORT_SYMBOL_GPL(rust_helper_netdev_priv);
223+
224+
void rust_helper_eth_hw_addr_random(struct net_device *dev)
225+
{
226+
eth_hw_addr_random(dev);
227+
}
228+
EXPORT_SYMBOL_GPL(rust_helper_eth_hw_addr_random);
229+
230+
int rust_helper_net_device_set_new_lstats(struct net_device *dev)
231+
{
232+
dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
233+
if (!dev->lstats)
234+
return -ENOMEM;
235+
236+
return 0;
237+
}
238+
EXPORT_SYMBOL_GPL(rust_helper_net_device_set_new_lstats);
239+
240+
void rust_helper_dev_lstats_add(struct net_device *dev, unsigned int len)
241+
{
242+
dev_lstats_add(dev, len);
243+
}
244+
EXPORT_SYMBOL_GPL(rust_helper_dev_lstats_add);
245+
215246
/* We use bindgen's --size_t-is-usize option to bind the C size_t type
216247
* as the Rust usize type, so we can use it in contexts where Rust
217248
* expects a usize like slice (array) indices. usize is defined to be

rust/kernel/bindings_helper.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@
1818
#include <linux/platform_device.h>
1919
#include <linux/of_platform.h>
2020
#include <linux/security.h>
21+
#include <linux/netdevice.h>
22+
#include <linux/ethtool.h>
23+
#include <linux/etherdevice.h>
24+
#include <linux/netdev_features.h>
25+
#include <linux/rtnetlink.h>
26+
#include <net/rtnetlink.h>
2127

2228
// `bindgen` gets confused at certain things
2329
const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL;
2430
const gfp_t BINDINGS___GFP_ZERO = __GFP_ZERO;
31+
32+
const int BINDINGS_NLA_HDRLEN = NLA_HDRLEN;

rust/kernel/error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ impl Error {
129129
/// Restart the system call.
130130
pub const ERESTARTSYS: Self = Error(-(bindings::ERESTARTSYS as i32));
131131

132+
/// Cannot assign requested address
133+
pub const EADDRNOTAVAIL: Self = Error(-(bindings::EADDRNOTAVAIL as i32));
134+
132135
/// Creates an [`Error`] from a kernel error code.
133136
///
134137
/// It is a bug to pass an out-of-range `errno`. `EINVAL` would
@@ -276,7 +279,7 @@ where
276279
#[macro_export]
277280
macro_rules! from_kernel_result {
278281
($($tt:tt)*) => {{
279-
$crate::error::from_kernel_result_helper((|| {
282+
$crate::from_kernel_result_helper((|| {
280283
$($tt)*
281284
})())
282285
}};

rust/kernel/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
const_raw_ptr_deref,
2323
const_unreachable_unchecked,
2424
receiver_trait,
25-
try_reserve
25+
try_reserve,
26+
doc_cfg
2627
)]
2728

2829
// Ensure conditional compilation based on the kernel configuration works;
@@ -44,6 +45,11 @@ mod error;
4445
pub mod file;
4546
pub mod file_operations;
4647
pub mod miscdev;
48+
49+
#[cfg(any(CONFIG_NET, doc))]
50+
#[doc(cfg(CONFIG_NET))]
51+
pub mod net;
52+
4753
pub mod pages;
4854
pub mod security;
4955
pub mod str;
@@ -77,8 +83,11 @@ pub mod user_ptr;
7783
#[doc(hidden)]
7884
pub use build_error::build_error;
7985

86+
#[doc(hidden)]
87+
pub use crate::error::from_kernel_result_helper;
88+
8089
pub use crate::error::{Error, Result};
81-
pub use crate::types::{Mode, ScopeGuard};
90+
pub use crate::types::{Mode, SavedAsPointer, SavedAsPointerMut, ScopeGuard};
8291

8392
/// Page size defined in terms of the `PAGE_SHIFT` macro from C.
8493
///

0 commit comments

Comments
 (0)