Skip to content

virtio_net: enable checksum and GSO offloads#2968

Merged
jstarks merged 2 commits intomicrosoft:mainfrom
jstarks:virtio-net-offload
Mar 16, 2026
Merged

virtio_net: enable checksum and GSO offloads#2968
jstarks merged 2 commits intomicrosoft:mainfrom
jstarks:virtio-net-offload

Conversation

@jstarks
Copy link
Copy Markdown
Member

@jstarks jstarks commented Mar 13, 2026

Add TX and RX offload support to the virtio-net device.

TX path:

  • Query the endpoint's TxOffloadSupport at device creation and advertise the corresponding virtio features (VIRTIO_NET_F_CSUM, GUEST_CSUM, HOST_TSO4, HOST_TSO6).
  • Parse the virtio-net header on each TX packet to extract checksum and GSO fields. Translate VIRTIO_NET_HDR_F_NEEDS_CSUM into TxFlags (offload_tcp_checksum / offload_udp_checksum / offload_ip_header_checksum) by inspecting csum_offset (16 = TCP, 6 = UDP) and determining IP version from the GSO type or the Ethernet header's EtherType.
  • For TSO packets (gso_type TCPV4/TCPV6), populate max_tcp_segment_size, l2/l3/l4 header lengths, and the segmentation offload flag.
  • Peek at the first 18 bytes of the Ethernet frame (enough for a VLAN tag) to derive l2_len and IP version when the GSO type alone is insufficient.

RX path:

  • Set VIRTIO_NET_HDR_F_DATA_VALID in the virtio-net header when both the IP and L4 checksums are reported as valid by the backend, allowing the guest to skip re-verification.

Hardening:

  • Gate offload parsing on negotiated features: only honor NEEDS_CSUM when VIRTIO_NET_F_CSUM was negotiated, only honor GSO when HOST_TSO4/HOST_TSO6 was negotiated. Store negotiated features in the Worker.
  • Validate header lengths before enabling offloads: require l2_len > 0, csum_start > l2_len, csum_start <= packet_len for checksum offloads. Require gso_size > 0, l3_len > 0, l4_len > 0 for TSO.
  • Make packet errors non-fatal: malformed guest descriptors (empty, too small, too many segments, unreadable) are completed with 0 bytes, logged rate-limited, and counted via a new tx_dropped counter — the worker continues processing.
  • Split TX packet queuing into try_queue_tx_packet (returns Result) and queue_tx_packet (handles cleanup structurally: truncate segments, complete descriptor, increment counter).
  • Avoid per-packet Vec allocation by pushing segments directly into the shared tx_segments buffer, with truncate-on-error rollback.

Tests:

  • Unit tests for parse_tx_offloads covering no-offloads, TCP/UDP checksum, IPv4/IPv6, TSO4/TSO6, TSO-without-needs_csum, and VLAN-tagged frames.
  • End-to-end TX test posting a packet with checksum offload flags through the full virtio queue path, asserting on TxMetadata flags, header lengths, and packet length.
  • RX tests verifying data_valid is set/cleared for Good, Unknown, Bad, and ValidatedButWrong checksum states.
  • Feature negotiation tests confirming correct VIRTIO_NET_F_* advertisement based on endpoint capabilities.

@jstarks jstarks requested a review from a team as a code owner March 13, 2026 00:30
Copilot AI review requested due to automatic review settings March 13, 2026 00:30
Copy link
Copy Markdown
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

This PR adds virtio-net TX/RX offload support by advertising checksum/TSO features based on backend capabilities, translating virtio-net header offload fields into net_backend::TxMetadata, and setting VIRTIO_NET_HDR_F_DATA_VALID on RX when the backend reports validated checksums.

Changes:

  • Advertise VIRTIO_NET_F_CSUM and VIRTIO_NET_F_HOST_TSO4/6 based on Endpoint::tx_offload_support().
  • Parse virtio-net TX header + Ethernet prefix to set TxMetadata offload flags and header-length fields.
  • Set RX data_valid flag when backend checksum states are validated; add/extend unit and integration-style tests.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
vm/devices/virtio/virtio_net/src/lib.rs Adds feature advertisement and TX offload parsing to populate TxMetadata for the backend.
vm/devices/virtio/virtio_net/src/buffers.rs Sets RX virtio-net header DATA_VALID flag based on backend checksum validity.
vm/devices/virtio/virtio_net/src/tests.rs Adds TX offload parsing tests, RX data_valid tests, and feature negotiation tests.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/buffers.rs
Comment thread vm/devices/virtio/virtio_net/src/tests.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/tests.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Copilot AI review requested due to automatic review settings March 13, 2026 18:36
Copy link
Copy Markdown
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 virtio-net TX/RX offload plumbing so the device can advertise and translate virtio-net checksum/GSO fields into backend TxMetadata/RX header flags, with accompanying tests.

Changes:

  • Advertise virtio-net offload-related feature bits based on backend TxOffloadSupport.
  • Parse virtio-net TX header fields (csum/GSO) and map them into net_backend::TxMetadata offload flags and header lengths.
  • Set VIRTIO_NET_HDR_F_DATA_VALID on RX when backend checksum state is reported as valid; add unit/integration tests for offload behaviors.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 10 comments.

File Description
vm/devices/virtio/virtio_net/src/lib.rs Offload feature advertisement + TX header peeking/parsing into TxMetadata.
vm/devices/virtio/virtio_net/src/buffers.rs Map RX checksum validity into virtio-net header data_valid flag.
vm/devices/virtio/virtio_net/src/tests.rs Add TX offload parsing tests, RX data_valid tests, and feature negotiation tests.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/tests.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/tests.rs
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/buffers.rs Outdated
@jstarks jstarks requested a review from a team as a code owner March 13, 2026 23:12
@jstarks jstarks requested a review from Copilot March 15, 2026 15:29
Copy link
Copy Markdown
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 virtio-net TX/RX offload plumbing (checksum + TCP segmentation) and corresponding test coverage, integrating backend offload capabilities into virtio feature advertisement and per-packet metadata.

Changes:

  • Advertise virtio-net checksum/TSO features based on Endpoint::tx_offload_support() and propagate parsed offload metadata into TxMetadata.
  • Parse virtio-net TX headers + Ethernet EtherType/VLAN prefix to derive TxFlags and header lengths for checksum/GSO offloads.
  • Set RX VIRTIO_NET_HDR_F_DATA_VALID based on backend-reported checksum validity, with new unit/integration tests.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
vm/devices/virtio/virtio_net/src/lib.rs Feature negotiation + TX header parsing to generate backend offload metadata.
vm/devices/virtio/virtio_net/src/buffers.rs Map RX checksum validation state to virtio-net header data_valid flag.
vm/devices/virtio/virtio_net/src/tests.rs Adds parsing/unit tests, RX data_valid tests, and feature negotiation tests.
vm/devices/virtio/virtio_net/Cargo.toml Adds tracelimit dependency for rate-limited logging.
Cargo.lock Records the new tracelimit dependency usage.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs
Comment thread vm/devices/virtio/virtio_net/src/tests.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Copy link
Copy Markdown
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 virtio-net TX/RX offload support (checksum + TSO/GSO) to improve networking performance while hardening malformed-guest handling.

Changes:

  • Advertise virtio-net offload features based on backend TxOffloadSupport and store negotiated features in the worker.
  • Parse virtio-net TX headers to populate TxMetadata offload flags/lengths; mark malformed TX packets as dropped (non-fatal) with ratelimited logging + new counter.
  • Set RX VIRTIO_NET_HDR_F_DATA_VALID based on backend checksum validation state; add extensive unit + integration-style tests.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
vm/devices/virtio/virtio_net/src/lib.rs Feature advertisement, negotiated-feature plumbing, TX packet hardening + offload parsing, tx_dropped counter
vm/devices/virtio/virtio_net/src/buffers.rs Sets RX data_valid header flag based on checksum validity
vm/devices/virtio/virtio_net/src/tests.rs Adds offload parsing tests, RX data_valid tests, and feature negotiation tests; extends harness utilities
vm/devices/virtio/virtio_net/Cargo.toml Adds tracelimit dependency for ratelimited TX-drop logging
Cargo.lock Locks tracelimit dependency

You can also share your feedback on Copilot code review. Take the survey.

Comment thread vm/devices/virtio/virtio_net/src/lib.rs
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs
Comment thread vm/devices/virtio/virtio_net/src/tests.rs
Copilot AI review requested due to automatic review settings March 15, 2026 17:33
Copy link
Copy Markdown
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 virtio-net TX/RX offload support by negotiating and translating virtio-net header offload fields into backend TxMetadata, and by reflecting backend RX checksum validity back to the guest via VIRTIO_NET_HDR_F_DATA_VALID.

Changes:

  • Advertise checksum/TSO-related virtio-net features based on endpoint TxOffloadSupport, and gate TX offload parsing on negotiated features.
  • Harden TX/RX queue handling by dropping malformed/duplicate guest submissions non-fatally with rate-limited logging and new drop counters.
  • Extend unit and end-to-end tests to cover TX offload parsing, RX data_valid, duplicate descriptor handling, and feature negotiation.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
vm/devices/virtio/virtio_net/src/lib.rs Feature advertisement, negotiated-feature plumbing into workers, TX offload parsing, and non-fatal drop handling with counters/logging.
vm/devices/virtio/virtio_net/src/buffers.rs Map backend RX checksum states to virtio-net header DATA_VALID; handle duplicate RX descriptor submissions without panicking.
vm/devices/virtio/virtio_net/src/tests.rs Add comprehensive TX offload parsing tests, RX data_valid tests, duplicate TX descriptor test, and feature negotiation tests.
vm/devices/virtio/virtio_net/Cargo.toml Add tracelimit dependency for rate-limited guest-triggerable warnings.
Cargo.lock Record the new tracelimit dependency in the lockfile.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread vm/devices/virtio/virtio_net/src/lib.rs Outdated
Comment thread vm/devices/virtio/virtio_net/src/lib.rs
Copy link
Copy Markdown
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

This PR adds virtio-net TX/RX offload support, translating virtio-net header offload fields into backend TxMetadata (checksum + TSO) and reporting RX checksum validation back to the guest via VIRTIO_NET_HDR_F_DATA_VALID.

Changes:

  • Advertise virtio-net offload feature bits based on backend TxOffloadSupport, and store negotiated features in each worker for per-packet gating.
  • Implement TX offload parsing (CSUM + TSO4/TSO6) with validation + non-fatal drop behavior and new dropped counters/logging.
  • Set RX data_valid based on backend checksum states; extend tests to cover offload parsing, end-to-end TX metadata, RX data_valid, and feature negotiation.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
vm/devices/virtio/virtio_net/src/lib.rs Advertises offload features, stores negotiated features in workers, parses TX offloads, and hardens TX/RX queueing with drop handling + counters.
vm/devices/virtio/virtio_net/src/buffers.rs Makes RX buffer queueing detect duplicates and sets RX data_valid header flag based on checksum validity.
vm/devices/virtio/virtio_net/src/tests.rs Adds unit + integration tests covering TX offload parsing, end-to-end TX metadata, RX data_valid, and feature advertisement.
vm/devices/virtio/virtio_net/Cargo.toml Adds tracelimit dependency for rate-limited drop logging.
Cargo.lock Records the new tracelimit dependency usage.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +1069 to +1076
if l3_len > 0 && l4_len > 0 {
flags.set_offload_tcp_segmentation(true);
flags.set_offload_tcp_checksum(true);
max_tcp_segment_size = header.gso_size;

flags.set_is_ipv4(is_ipv4_from_gso);
flags.set_is_ipv6(is_ipv6_from_gso);
if is_ipv4_from_gso {
Brian-Perkins
Brian-Perkins previously approved these changes Mar 16, 2026
Copilot AI review requested due to automatic review settings March 16, 2026 22:46
Copy link
Copy Markdown
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 virtio-net TX/RX offload support by translating virtio-net header offload fields into backend TxMetadata flags and by propagating backend RX checksum validity to the guest via VIRTIO_NET_HDR_F_DATA_VALID. The PR also hardens queue handling against malformed/duplicate guest submissions and expands test coverage around offload parsing and feature negotiation.

Changes:

  • Advertise virtio-net checksum/TSO features based on backend TxOffloadSupport, and gate offload parsing on negotiated features.
  • Implement TX offload parsing (CSUM + TSO4/TSO6) and RX DATA_VALID signaling; make malformed/duplicate packets non-fatal and counted as dropped.
  • Add unit + end-to-end tests for TX offloads, RX data_valid, duplicate descriptor handling, and feature negotiation.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
vm/devices/virtio/virtio_net/src/lib.rs Advertises offload features, adds negotiated-feature gating, TX offload parsing, and drop-on-error hardening with new counters/logging.
vm/devices/virtio/virtio_net/src/buffers.rs Handles duplicate RX descriptor submissions without panicking and sets DATA_VALID based on checksum validity.
vm/devices/virtio/virtio_net/src/tests.rs Extends mock logging to capture TX metadata; adds extensive TX/RX offload and negotiation tests.
vm/devices/virtio/virtio_net/Cargo.toml Adds tracelimit dependency for rate-limited drop logging.
Cargo.lock Locks the added tracelimit dependency.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread vm/devices/virtio/virtio_net/src/lib.rs
Comment thread vm/devices/virtio/virtio_net/src/tests.rs
@jstarks jstarks enabled auto-merge (squash) March 16, 2026 23:01
@jstarks jstarks disabled auto-merge March 16, 2026 23:52
@jstarks jstarks merged commit 71e7202 into microsoft:main Mar 16, 2026
58 of 59 checks passed
@jstarks jstarks deleted the virtio-net-offload branch March 16, 2026 23:53
@github-actions
Copy link
Copy Markdown

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.

4 participants