Skip to content

docs: Add architecture guide#144

Merged
benaadams merged 9 commits intomasterfrom
copilot/update-architecture-guide
Feb 11, 2026
Merged

docs: Add architecture guide#144
benaadams merged 9 commits intomasterfrom
copilot/update-architecture-guide

Conversation

Copy link
Contributor

Copilot AI commented Feb 11, 2026

Architecture Guide Documentation - Complete ✓

  • Create docs/ARCHITECTURE.md (880 lines) covering:
    • High-level architecture overview with ASCII diagrams
    • Component layers and responsibilities (7 layers detailed)
    • Packet processing flow (receive and transmit paths with examples)
    • Core design patterns (4 patterns with code examples)
    • Memory management and zero-copy design
    • Integration points and extensibility (3 integration levels)
    • Example flows (TCP SYN packet receive, SYN-ACK transmit)
    • Performance considerations and benchmarks
  • Address all review feedback:
    • Fix low-level example to clarify it's conceptual
    • Update mid-level example to use correct generic type (AF_XDPTransmitRing)
    • Clarify PacketReceiver is internal and show IPacketReceiver implementation
    • Fix Pack = 1 description to clarify it's about layout, not endianness
    • Fix Checksum.Calculate to use correct ref byte parameter
    • Clarify checksum example shows TCP segment calculation
    • Document generic pattern behavior (returns null when consumed)
    • Remove trailing whitespace
  • Documentation complete and validated
Original prompt

This section details on the original issue you should resolve

<issue_title>docs: Architecture guide</issue_title>
<issue_description></issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 6 commits February 11, 2026 04:52
Co-authored-by: benaadams <1142958+benaadams@users.noreply.github.com>
Co-authored-by: benaadams <1142958+benaadams@users.noreply.github.com>
Co-authored-by: benaadams <1142958+benaadams@users.noreply.github.com>
Co-authored-by: benaadams <1142958+benaadams@users.noreply.github.com>
Co-authored-by: benaadams <1142958+benaadams@users.noreply.github.com>
…sage

Co-authored-by: benaadams <1142958+benaadams@users.noreply.github.com>
Copilot AI changed the title [WIP] Update documentation for architecture guide docs: Add architecture guide Feb 11, 2026
Copilot AI requested a review from benaadams February 11, 2026 04:58
@benaadams benaadams marked this pull request as ready for review February 11, 2026 04:59
Copilot AI review requested due to automatic review settings February 11, 2026 04:59
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new architecture guide documenting Magma’s internal layering, packet parsing/processing patterns, memory ownership model, integration with Kestrel transports, and performance-related implementation details.

Changes:

  • Introduces docs/ARCHITECTURE.md describing Magma’s layered architecture and core design patterns (TryConsume/type-punning, buffer ownership, checksum strategy).
  • Documents end-to-end receive/transmit flows with illustrative code and diagrams.
  • Adds a performance considerations section with optimization rationale and benchmark-style summaries.

Comment on lines 670 to 676
var transport = new AF_XDPTransport("eth0");
await foreach (var packet in transport.ReceivePacketsAsync())
{
// Process raw packet bytes
ProcessPacket(packet.Memory.Span);
packet.Dispose();
}
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The “Low-Level (Direct Packet Access)” example uses new AF_XDPTransport("eth0") and transport.ReceivePacketsAsync(), but AF_XDPTransport in this repo is a Kestrel ITransport and doesn’t expose that constructor or a ReceivePacketsAsync API. Consider marking this as pseudocode or updating it to reflect the actual public surface area.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 4431a99. Replaced the example with a conceptual note clarifying that applications typically interact at the transport or receiver level, not directly with the low-level ring APIs.

Comment on lines 682 to 683
var receiver = new TcpTransportReceiver<AF_XDPTransport>(
endpoint, transport, dispatcher);
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The “Mid-Level (Protocol Layers)” snippet constructs new TcpTransportReceiver<AF_XDPTransport>(...), but TcpTransportReceiver<TTransmitter> is constrained to TTransmitter : IPacketTransmitter and is instantiated in the codebase with transmit-ring types (e.g., AF_XDPTransmitRing / NetMapTransmitRing), not the AF_XDPTransport itself. Updating the example to use the correct generic type (or documenting how transports create receivers internally) would prevent confusion.

Suggested change
var receiver = new TcpTransportReceiver<AF_XDPTransport>(
endpoint, transport, dispatcher);
// Obtain an IPacketTransmitter from your transport (e.g., AF_XDPTransmitRing)
var receiver = new TcpTransportReceiver<AF_XDPTransmitRing>(
endpoint, transmitter, dispatcher);

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 4431a99. Updated to use AF_XDPTransmitRing as the generic parameter and added a clarifying comment about how transports create receivers internally.


**Custom Packet Processing:**

```csharp
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The “Custom Packet Processing” example uses PacketReceiver.CreateDefault() and sets receiver.IPv4Consumer, but PacketReceiver is internal (see src/Magma.Network/DefaultPacketReceiver.cs) and isn’t accessible to consumers of the library. Consider switching this example to a public type/API (or explicitly stating it’s an internal implementation detail).

Suggested change
```csharp
```csharp
// Internal example: PacketReceiver is an internal implementation detail of Magma's built-in pipeline

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 4431a99. Replaced the example with a complete IPacketReceiver implementation and added a note that PacketReceiver is an internal implementation detail.

Comment on lines 343 to 345
- Structs use `Pack = 1` to match network byte order
- Input is always validated before type-punning
- Endianness conversion is explicit where needed
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the “Type-Punning with Unsafe Code” section, Pack = 1 is described as matching “network byte order”. Struct packing affects field alignment/padding, not endianness; byte order is handled separately via explicit conversions. Consider rewording to avoid implying Pack addresses endianness.

Suggested change
- Structs use `Pack = 1` to match network byte order
- Input is always validated before type-punning
- Endianness conversion is explicit where needed
- Structs use `Pack = 1` to ensure a tightly packed layout that matches the wire-format field layout (no padding)
- Input is always validated before type-punning
- Network byte order is handled via explicit endianness conversion where needed

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 4431a99. Clarified that Pack = 1 ensures tightly packed layout matching wire format (no padding between fields), and that network byte order (endianness) is handled separately via explicit conversion.

Unsafe.SizeOf<TcpV4PseudoHeader>());

// Later, calculate full checksum by adding TCP header + data
var checksum = Checksum.Calculate(ref tcpHeader, length, pseudoPartialSum);
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pseudo-header checksum example calls Checksum.Calculate(ref tcpHeader, length, pseudoPartialSum), but in the codebase Checksum.Calculate takes ref byte (e.g., ref Unsafe.As<Tcp, byte>(ref tcpHeader) or ref MemoryMarshal.GetReference(span)). As written, the snippet won’t compile and may mislead readers; please adjust it to match the actual API usage pattern.

Suggested change
var checksum = Checksum.Calculate(ref tcpHeader, length, pseudoPartialSum);
var checksum = Checksum.Calculate(ref Unsafe.As<Tcp, byte>(ref tcpHeader), length, pseudoPartialSum);

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 4431a99. Updated to use ref Unsafe.As<TcpV4PseudoHeader, byte>(ref tcpHeader) to match the actual API signature that takes ref byte.

@benaadams
Copy link
Member

@copilot apply changes based on the comments in this thread

…d clarify internal types

Co-authored-by: benaadams <1142958+benaadams@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 11, 2026

@copilot apply changes based on the comments in this thread

All review comments have been addressed in commit 4431a99:

  • Fixed low-level example to be conceptual
  • Corrected generic type to AF_XDPTransmitRing
  • Clarified PacketReceiver is internal and provided IPacketReceiver example
  • Fixed Pack = 1 description
  • Fixed Checksum.Calculate parameter type

Co-authored-by: benaadams <1142958+benaadams@users.noreply.github.com>
@benaadams benaadams merged commit 62aa911 into master Feb 11, 2026
2 checks passed
@benaadams benaadams deleted the copilot/update-architecture-guide branch February 11, 2026 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs: Architecture guide

3 participants