Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 30, 2025

The existing TestFilter function only validates BPF filter expressions against IPv4 packets. This adds equivalent coverage for IPv6 and implements classic BPF to eBPF instruction conversion to support advanced filter expressions.

Changes

IPv6 Filter Tests

  • Added TestFilterIPv6 function mirroring the IPv4 test structure
  • Tests 18 filter expressions adapted for IPv6 (protocol matching, host/port filters, packet length)
  • Validates both L2 (Ethernet) and L3 (IPv6) link types
  • Includes negation tests for all expressions
// IPv6 packet with addresses 2001::1 → 2001::2
_, _, _, _, bytes, _ := testPacketV6(
    &layers.Ethernet{...},
    &layers.IPv6{
        SrcIP: net.IP([]byte{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}),
        DstIP: net.IP([]byte{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}),
        NextHeader: layers.IPProtocolUDP,
    },
    &layers.UDP{...},
    payload,
)

// Test IPv6-specific filters
{"ip6", true},                                      // matches IPv6
{"host 2001::1 or host 2001::5", true},            // address matching
{"src 2001::1 and dst 2001::2", true},             // directional matching

BPF Instruction Conversion

  • Implemented classic BPF to eBPF ALU instruction conversion in cBPF2eBPF function
  • Added support for all 11 ALU operations: add, sub, mul, div, or, and, lsh, rsh, neg, mod, xor
  • Both immediate (K) and register (X) source operands are supported
  • Implemented scratch memory operations (ST, STX, LD MEM, LDX MEM) using stack-based emulation
  • Classic BPF scratch memory M[0-15] is emulated using eBPF stack slots

Implementation Details

  • Added bpfClassSt and bpfClassStx constants for store instruction classes
  • ALU operations properly convert from classic BPF's A/X register model to eBPF's R1/R2 registers
  • Scratch memory uses stack offsets: M[K] → [R10 - (K+1)*4]

Known Limitations

The ip6 protochain test still fails due to BPF verifier rejecting loop structures, which is a fundamental limitation when converting classic BPF programs with loops to eBPF without bounded loop support.

Addresses lines 29-144 of felix/bpf/ut/filter_test.go and implements ALU conversion in felix/bpf/filter/filter.go.

Original prompt

On filter_test.go:29-144, Modify this test function or add an equivalent that tests filters that match ipv6 packets.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@marvin-tigera marvin-tigera added this to the Calico v3.32.0 milestone Oct 30, 2025
@marvin-tigera marvin-tigera added release-note-required Change has user-facing impact (no matter how small) docs-pr-required Change is not yet documented labels Oct 30, 2025
Copilot AI changed the title [WIP] Add test function for IPv6 packet filters Add IPv6 filter tests for BPF packet filtering Oct 30, 2025
Copilot AI requested a review from tomastigera October 30, 2025 23:09
Copilot finished work on behalf of tomastigera October 30, 2025 23:09
@tomastigera tomastigera force-pushed the copilot/add-ipv6-filter-tests branch from 88594e2 to 75aa8e0 Compare November 5, 2025 02:00
Comment on lines +401 to +411
bpfAluAdd uint8 = 0x00
bpfAluSub uint8 = 0x10
bpfAluMul uint8 = 0x20
bpfAluDiv uint8 = 0x30
bpfAluOr uint8 = 0x40
bpfAluAnd uint8 = 0x50
bpfAluLsh uint8 = 0x60
bpfAluRsh uint8 = 0x70
bpfAluNeg uint8 = 0x80
bpfAluMod uint8 = 0x90
bpfAluXor uint8 = 0xa0
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot implement classic bpf to ebpf instruction conversion in function cBPF2eBPF in the class bpfClassAlu branch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented in commit 53d385f. Added conversion for all 11 ALU operations (add, sub, mul, div, or, and, lsh, rsh, neg, mod, xor) with support for both immediate (K) and register (X) source operands. Also implemented scratch memory operations (ST, STX, LD MEM, LDX MEM) using stack-based emulation.

The protochain test still fails due to BPF verifier rejecting loop structures - this is a known limitation when converting classic BPF loops to eBPF.

…memory operations

- Implemented all ALU operations (add, sub, mul, div, or, and, lsh, rsh, neg, mod, xor)
- Added support for both immediate (K) and register (X) source operands
- Implemented scratch memory operations (ST, STX, LD MEM, LDX MEM)
- Classic BPF scratch memory M[0-15] is emulated using stack slots

The protochain test still fails due to BPF verifier rejecting loop structures, which is a fundamental limitation when converting classic BPF loops to eBPF.

Co-authored-by: tomastigera <49207409+tomastigera@users.noreply.github.com>
Copilot AI changed the title Add IPv6 filter tests for BPF packet filtering Add IPv6 filter tests and implement BPF ALU instruction conversion Nov 5, 2025
Copilot AI requested a review from tomastigera November 5, 2025 02:22
Copilot finished work on behalf of tomastigera November 5, 2025 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs-pr-required Change is not yet documented release-note-required Change has user-facing impact (no matter how small)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants