Skip to content

Commit

Permalink
[xdp] Feature: XDP Bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ppenna committed Jun 17, 2024
1 parent f3c8501 commit 19e2a24
Show file tree
Hide file tree
Showing 15 changed files with 392 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ windows = { version = "0.57.0", features = [
"Win32_System_Pipes",
"Win32_System_Threading",
] }
xdp-rs = { path = "xdp-rs", optional = true }
# for interacting with socket2.
windows-sys = { version = "0.52.0", features = ["Win32_Networking_WinSock"] }

Expand Down Expand Up @@ -154,6 +155,7 @@ catmem-libos = []
catnip-libos = ["libdpdk"]
catloop-libos = ["catmem-libos"]
libdpdk = ["dpdk-rs"]
libxdp = ["xdp-rs"]
mlx4 = ["dpdk-rs/mlx4"]
mlx5 = ["dpdk-rs/mlx5"]
profiler = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod rawsocket;
//======================================================================================================================

use crate::{
catpowder::runtime::rawsocket::{
catpowder::linux::rawsocket::{
RawSocket,
RawSocketAddr,
},
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//======================================================================================================================

use crate::{
catpowder::runtime::RawSocketAddr,
catpowder::linux::RawSocketAddr,
pal::data_structures::{
SockAddr,
SockAddrIn,
Expand Down
6 changes: 5 additions & 1 deletion src/rust/catpowder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

pub mod runtime;
#[cfg(target_os = "linux")]
mod linux;

#[cfg(target_os = "linux")]
pub use linux::LinuxRuntime as CatpowderRuntime;
10 changes: 5 additions & 5 deletions src/rust/demikernel/libos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use self::name::LibOSName;
#[cfg(feature = "catnip-libos")]
use crate::catnip::runtime::SharedDPDKRuntime;
#[cfg(feature = "catpowder-libos")]
use crate::catpowder::runtime::LinuxRuntime;
use crate::catpowder::CatpowderRuntime;
#[cfg(any(
feature = "catnap-libos",
feature = "catnip-libos",
Expand Down Expand Up @@ -134,12 +134,12 @@ impl LibOS {
#[cfg(feature = "catpowder-libos")]
LibOSName::Catpowder => {
// TODO: Remove some of these clones once we are done merging the libOSes.
let transport: LinuxRuntime = LinuxRuntime::new(&config)?;
let transport: CatpowderRuntime = CatpowderRuntime::new(&config)?;
// This is our transport for Catpowder.
let inetstack: SharedInetStack<LinuxRuntime> =
SharedInetStack::<LinuxRuntime>::new(&config, runtime.clone(), transport).unwrap();
let inetstack: SharedInetStack<CatpowderRuntime> =
SharedInetStack::<CatpowderRuntime>::new(&config, runtime.clone(), transport).unwrap();
Self::NetworkLibOS(NetworkLibOSWrapper::Catpowder(SharedNetworkLibOS::<
SharedInetStack<LinuxRuntime>,
SharedInetStack<CatpowderRuntime>,
>::new(
config.local_ipv4_addr()?,
runtime.clone(),
Expand Down
4 changes: 2 additions & 2 deletions src/rust/demikernel/libos/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use crate::catnap::transport::SharedCatnapTransport;
#[cfg(feature = "catnip-libos")]
use crate::catnip::runtime::SharedDPDKRuntime;
#[cfg(feature = "catpowder-libos")]
use crate::catpowder::runtime::LinuxRuntime;
use crate::catpowder::CatpowderRuntime;

//======================================================================================================================
// Structures
Expand All @@ -53,7 +53,7 @@ use crate::catpowder::runtime::LinuxRuntime;
/// Network LIBOS.
pub enum NetworkLibOSWrapper {
#[cfg(feature = "catpowder-libos")]
Catpowder(SharedNetworkLibOS<SharedInetStack<LinuxRuntime>>),
Catpowder(SharedNetworkLibOS<SharedInetStack<CatpowderRuntime>>),
#[cfg(all(feature = "catnap-libos"))]
Catnap(SharedNetworkLibOS<SharedCatnapTransport>),
#[cfg(feature = "catnip-libos")]
Expand Down
52 changes: 52 additions & 0 deletions xdp-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

[package]
name = "xdp-rs"
version = "1.0.0"
authors = ["Microsoft Corporation"]
edition = "2021"
description = "Rust Bindings for XDP"
homepage = "https://aka.ms/demikernel"
repository = "https://github.com/demikernel/demikernel"

[dependencies]
cfg-if = "1.0.0"
windows = { version = "0.56.0", features = [
"Win32_Foundation",
"Win32_Networking_WinSock",
"Win32_Security",
"Win32_Storage_FileSystem",
"Win32_System_IO",
"Win32_System_Pipes",
"Win32_System_Threading",
] }

[build-dependencies]
anyhow = "1.0.83"
bindgen = "0.69.4"
cc = "1.0.97"

# Build profile used for releases.
[profile.release]
opt-level = 3 # Enable all compiler optimizations.
debug = false # Do not include any debug info in the binary.
debug-assertions = false # Do not include any debug assertions in the binary.
overflow-checks = false # Do not check for overflows at runtime.
lto = "fat" # Perform link time optimizations across all dependencies (overridden).
panic = "abort" # Terminate the process upon panic (overridden).
incremental = false # Disable incremental compilation.
codegen-units = 1 # Produce a single code generation unit (overridden).
rpath = false # Disable runtime search path.

# Build profile used for development and debugging.
[profile.dev]
opt-level = 0 # Disable all compiler optimizations.
debug = true # Output full debug info in the binary.
debug-assertions = true # Include debug assertions in the binary.
overflow-checks = true # Check for overflows at runtime.
lto = "off" # Disable link time optimization (overridden).
panic = 'unwind' # Unwind the stack upon panic.
incremental = true # Incremental build.
codegen-units = 256 # Produce multiple code generation units.
rpath = false # Disable runtime search path.
100 changes: 100 additions & 0 deletions xdp-rs/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

//==============================================================================
// Extern Linkage
//==============================================================================

extern crate bindgen;
extern crate cc;

//==============================================================================
// Imports
//==============================================================================

use anyhow::Result;
use bindgen::{
Bindings,
Builder,
};
use cc::Build;
use std::{
env,
path::{
Path,
PathBuf,
},
};

//==============================================================================
// Standalone Functions
//==============================================================================

static WRAPPER_HEADER_NAME: &str = "wrapper.h";
static INLINED_C_NAME: &str = "inlined.c";
static OUT_DIR_VAR: &str = "OUT_DIR";
static XDP_PATH_VAR: &str = "XDP_PATH";
static INCLUDE_DIR: &str = "\\include";
static LIB_DIR: &str = "\\lib";
static XDP_API_LIB: &str = "xdpapi";
static SAL_BLOCKLIST_REGEX: &str = r".*SAL.*";
static TYPE_BLOCKLIST: [&str; 8] = [
".*OVERLAPPED.*",
"HANDLE",
"HRESULT",
"IN_ADDR",
"IN6_ADDR",
"in_addr.*",
"in6_addr.*",
"_?XDP_INET_ADDR",
];
static FILE_ALLOWLIST_REGEX: &str = r".*xdp.*";

fn main() -> Result<()> {
let out_dir_s: String = env::var(OUT_DIR_VAR).unwrap();
let out_dir: &Path = Path::new(&out_dir_s);

let libxdp_path: String = env::var(XDP_PATH_VAR)?;

let include_path: String = format!("{}{}", &libxdp_path, INCLUDE_DIR);
let lib_path: String = format!("{}{}", &libxdp_path, LIB_DIR);

println!("include_path: {}", include_path);
println!("lib_path: {}", lib_path);

// Point cargo to the libraries.
println!("cargo:rustc-link-search={}", lib_path);
println!("cargo:rustc-link-lib=dylib={}", XDP_API_LIB);

let mut builder = Builder::default();
for t in TYPE_BLOCKLIST.iter() {
builder = builder.blocklist_type(t);
}

// Generate bindings for headers.
let bindings: Bindings = builder
.clang_arg(&format!("-I{}", include_path))
.clang_arg("-mavx")
.header(WRAPPER_HEADER_NAME)
// NB SAL defines still get included despite having no functional impact.
.blocklist_item(SAL_BLOCKLIST_REGEX)
.allowlist_file(FILE_ALLOWLIST_REGEX)
// Allow the inline function wrappers to be generated.
.allowlist_file(format!(".*{}", WRAPPER_HEADER_NAME))
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.unwrap_or_else(|e| panic!("Failed to generate bindings: {:?}", e));
let bindings_out: PathBuf = out_dir.join("bindings.rs");
bindings.write_to_file(bindings_out).expect("Failed to write bindings");

// Build inlined functions.
let mut builder: Build = cc::Build::new();
builder.opt_level(3);
builder.pic(true);
builder.flag("-march=native");
builder.file(INLINED_C_NAME);
builder.include(include_path);
builder.compile("inlined");

Ok(())
}
3 changes: 3 additions & 0 deletions xdp-rs/generate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
REM bindgen --blocklist-item ".*SAL.*" --blocklist-file ".*vadefs.h" --blocklist-file ".*excpt.h" --blocklist-file ".*vcruntime.*" --blocklist-file ".*[Ww]indows [Kk]its.*" --verbose wrapper.h -o bindings.rs -- "-IC:\Program Files\xdp\include"

bindgen --blocklist-item ".*SAL.*" --blocklist-type ".*OVERLAPPED.*" --allowlist-file ".*xdp.*" --verbose wrapper.h -o bindings.rs -- "-IC:\Program Files\xdp\include"
85 changes: 85 additions & 0 deletions xdp-rs/inlined.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

/*
* This file contains wrappers for inline functions in <xdpapi.h>.
*/

#include "wrapper.h"

HRESULT
_XdpLoadApi(
_In_ UINT32 XdpApiVersion,
_Out_ XDP_LOAD_API_CONTEXT *XdpLoadApiContext,
_Out_ CONST XDP_API_TABLE **XdpApiTable
)
{
return XdpLoadApi(XdpApiVersion, XdpLoadApiContext, XdpApiTable);
}


VOID
_XdpUnloadApi(
_In_ XDP_LOAD_API_CONTEXT XdpLoadApiContext,
_In_ CONST XDP_API_TABLE *XdpApiTable
)
{
XdpUnloadApi(XdpLoadApiContext, XdpApiTable);
}

VOID
_XskRingInitialize(
_Out_ XSK_RING *Ring,
_In_ const XSK_RING_INFO *RingInfo
)
{
XskRingInitialize(Ring, RingInfo);
}

UINT32
_XskRingConsumerReserve(
_In_ XSK_RING *Ring,
_In_ UINT32 MaxCount,
_Out_ UINT32 *Index
)
{
return XskRingConsumerReserve(Ring, MaxCount, Index);
}

UINT32
_XskRingProducerReserve(
_In_ XSK_RING *Ring,
_In_ UINT32 MaxCount,
_Out_ UINT32 *Index
)
{
return XskRingProducerReserve(Ring, MaxCount, Index);
}

VOID
_XskRingConsumerRelease(
_Inout_ XSK_RING *Ring,
_In_ UINT32 Count
)
{
XskRingConsumerRelease(Ring, Count);
}

VOID
_XskRingProducerSubmit(
_Inout_ XSK_RING *Ring,
_In_ UINT32 Count
)
{
XskRingProducerSubmit(Ring, Count);
}

VOID *
_XskRingGetElement(
_In_ const XSK_RING *Ring,
_In_ UINT32 Index
) {
return XskRingGetElement(Ring, Index);
}
1 change: 1 addition & 0 deletions xdp-rs/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-2024-05-02
35 changes: 35 additions & 0 deletions xdp-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#![cfg_attr(feature = "strict", deny(clippy:all))]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(unused)]

// Redefinition of some types to allow tight integration with windows crate.
extern crate windows;

use windows::{
Win32::{
System::IO::OVERLAPPED,
Foundation::HANDLE,
Networking::WinSock::{
IN_ADDR,
IN6_ADDR,
}
},
core::HRESULT,
};

// Redefining this type prevents bindgen from having to wrap the whole union. Since this is a
// high-use type, prioritize syntax over maintainability.
#[repr(C)]
#[derive(Copy, Clone)]
pub union _XDP_INET_ADDR {
pub Ipv4: IN_ADDR,
pub Ipv6: IN6_ADDR,
}
pub type XDP_INET_ADDR = _XDP_INET_ADDR;

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
Loading

0 comments on commit 19e2a24

Please sign in to comment.