Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Magma.Link\Magma.Link.csproj" />
<ProjectReference Include="..\..\src\Magma.Common\Magma.Common.csproj" />
<ProjectReference Include="..\..\src\Magma.PCap\Magma.PCap.csproj" />
</ItemGroup>

</Project>
227 changes: 227 additions & 0 deletions sample/Magma.DPDK.PacketForwarder/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
using System;
using System.Buffers;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Magma.Link;

namespace Magma.DPDK.PacketForwarder
{
/// <summary>
/// DPDK-based L2 packet forwarder that forwards Ethernet frames between two ports.
/// This is a reference implementation demonstrating the forwarding logic.
///
/// Note: This sample requires a full DPDK transport implementation (see issue #128).
/// Currently serves as a reference/placeholder for the DPDK integration.
/// </summary>
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Magma DPDK Packet Forwarder");
Console.WriteLine("===========================");
Console.WriteLine();

if (args.Length < 2)
{
PrintUsage();
return;
}

var port0 = args[0];
var port1 = args[1];

Console.WriteLine($"Forwarding configuration:");
Console.WriteLine($" Port 0: {port0}");
Console.WriteLine($" Port 1: {port1}");
Console.WriteLine();
Console.WriteLine("Note: Full DPDK transport implementation required (issue #128)");
Console.WriteLine("Press Ctrl+C to exit");
Console.WriteLine();

var forwarder = new PacketForwarder(port0, port1);

using var cts = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
cts.Cancel();
};

try
{
await forwarder.RunAsync(cts.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Shutting down...");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
Environment.ExitCode = 1;
}
}

static void PrintUsage()
{
Console.WriteLine("Usage: Magma.DPDK.PacketForwarder <port0> <port1>");
Console.WriteLine();
Console.WriteLine("Arguments:");
Console.WriteLine(" port0 First DPDK port (e.g., 0000:00:08.0)");
Console.WriteLine(" port1 Second DPDK port (e.g., 0000:00:09.0)");
Console.WriteLine();
Console.WriteLine("Example:");
Console.WriteLine(" Magma.DPDK.PacketForwarder 0000:00:08.0 0000:00:09.0");
Console.WriteLine();
Console.WriteLine("Prerequisites:");
Console.WriteLine(" - DPDK libraries installed");
Console.WriteLine(" - Huge pages configured");
Console.WriteLine(" - NICs bound to DPDK-compatible driver (igb_uio/vfio-pci)");
Console.WriteLine(" - See README.md for detailed setup instructions");
}
}

/// <summary>
/// L2 packet forwarder that exchanges packets between two ports.
/// Demonstrates zero-copy forwarding pattern for DPDK integration.
/// </summary>
class PacketForwarder
{
private readonly string _port0;
private readonly string _port1;
private long _packetsForwarded0to1;
private long _packetsForwarded1to0;
private long _bytesForwarded0to1;
private long _bytesForwarded1to0;

public PacketForwarder(string port0, string port1)
{
_port0 = port0;
_port1 = port1;
}

public async Task RunAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Starting packet forwarding...");

// TODO: Initialize DPDK environment (EAL)
// TODO: Configure ports
// TODO: Setup memory pools

// Start statistics reporting
var statsTask = ReportStatisticsAsync(cancellationToken);

// Main forwarding loop (placeholder)
await Task.Run(() => ForwardingLoop(cancellationToken), cancellationToken);

await statsTask;
}

private void ForwardingLoop(CancellationToken cancellationToken)
{
// Placeholder for actual DPDK forwarding logic
// In a real implementation, this would:
// 1. Receive burst of packets from port 0
// 2. Forward to port 1
// 3. Receive burst of packets from port 1
// 4. Forward to port 0
// 5. Repeat

Console.WriteLine("Forwarding loop started (placeholder - requires DPDK transport)");

while (!cancellationToken.IsCancellationRequested)
{
// Simulate packet forwarding delay
Thread.Sleep(100);

// In real implementation:
// - Use rte_eth_rx_burst() to receive packets
// - Use rte_eth_tx_burst() to send packets
// - Update statistics
}
}

/// <summary>
/// Demonstrates the pattern for processing Ethernet frames.
/// This method shows how packet data would be handled once DPDK transport is available.
/// </summary>
private void ProcessPacket(ReadOnlySpan<byte> packetData, int sourcePort)
{
// Parse Ethernet header
if (!EthernetFrame.TryConsume(packetData, out var frame, out var payload))
{
return;
}

// In a real L2 forwarder:
// - Optionally update MAC addresses
// - Forward to the other port
// - Update statistics

var targetPort = sourcePort == 0 ? 1 : 0;
var length = packetData.Length;

if (sourcePort == 0)
{
Interlocked.Increment(ref _packetsForwarded0to1);
Interlocked.Add(ref _bytesForwarded0to1, length);
}
else
{
Interlocked.Increment(ref _packetsForwarded1to0);
Interlocked.Add(ref _bytesForwarded1to0, length);
}
}

private async Task ReportStatisticsAsync(CancellationToken cancellationToken)
{
var lastReport = DateTime.UtcNow;
var lastPackets0to1 = 0L;
var lastPackets1to0 = 0L;
var lastBytes0to1 = 0L;
var lastBytes1to0 = 0L;

while (!cancellationToken.IsCancellationRequested)
{
try
{
await Task.Delay(5000, cancellationToken);
}
catch (OperationCanceledException)
{
break;
}

var now = DateTime.UtcNow;
var elapsed = (now - lastReport).TotalSeconds;

var packets0to1 = Interlocked.Read(ref _packetsForwarded0to1);
var packets1to0 = Interlocked.Read(ref _packetsForwarded1to0);
var bytes0to1 = Interlocked.Read(ref _bytesForwarded0to1);
var bytes1to0 = Interlocked.Read(ref _bytesForwarded1to0);

var pps0to1 = (packets0to1 - lastPackets0to1) / elapsed;
var pps1to0 = (packets1to0 - lastPackets1to0) / elapsed;
var mbps0to1 = ((bytes0to1 - lastBytes0to1) * 8.0 / elapsed) / 1_000_000.0;
var mbps1to0 = ((bytes1to0 - lastBytes1to0) * 8.0 / elapsed) / 1_000_000.0;

Console.WriteLine($"[{now:HH:mm:ss}] Statistics:");
Console.WriteLine($" {_port0} -> {_port1}: {packets0to1:N0} packets ({pps0to1:F2} pps, {mbps0to1:F2} Mbps)");
Console.WriteLine($" {_port1} -> {_port0}: {packets1to0:N0} packets ({pps1to0:F2} pps, {mbps1to0:F2} Mbps)");

lastReport = now;
lastPackets0to1 = packets0to1;
lastPackets1to0 = packets1to0;
lastBytes0to1 = bytes0to1;
lastBytes1to0 = bytes1to0;
}

// Print final statistics
Console.WriteLine();
Console.WriteLine("Final Statistics:");
Console.WriteLine($" {_port0} -> {_port1}: {_packetsForwarded0to1:N0} packets ({_bytesForwarded0to1:N0} bytes)");
Console.WriteLine($" {_port1} -> {_port0}: {_packetsForwarded1to0:N0} packets ({_bytesForwarded1to0:N0} bytes)");
}
}
}
Loading