Skip to content

Conversation

@lixmal
Copy link
Collaborator

@lixmal lixmal commented Jul 27, 2025

Describe your changes

  • Check integers first (proto, ports) and check expensive prefixes last
  • Eliminate string comparison for protos

Issue ticket number and link

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

Copilot AI review requested due to automatic review settings July 27, 2025 15:04
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

This PR optimizes ACL (Access Control List) checking performance by reordering userspace checks to fail faster. The main change is moving protocol and port validation to the beginning of rule matching to exit early when there's no match.

  • Reorders ACL rule matching to check protocol and ports first before checking destination/source addresses
  • Changes internal representation from firewall.Protocol to gopacket.LayerType for consistency with packet parsing
  • Updates test code to use the new protoToLayer helper function

Reviewed Changes

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

Show a summary per file
File Description
client/firewall/uspfilter/filter.go Main optimization logic - reorders rule matching and adds protoToLayer helper
client/firewall/uspfilter/rule.go Changes RouteRule struct to use LayerType instead of Protocol
client/firewall/uspfilter/tracer.go Updates to use LayerType directly from decoded packet
client/firewall/uspfilter/filter_test.go Updates test calls to use protoToLayer helper
client/firewall/uspfilter/filter_filter_test.go Updates test calls to use protoToLayer helper
client/firewall/uspfilter/filter_bench_test.go Updates benchmark to use protoToLayer helper

nftypes "github.com/netbirdio/netbird/client/internal/netflow/types"
"github.com/netbirdio/netbird/client/internal/statemanager"
)

Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

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

The magic number 255 should be documented. Consider adding a comment explaining why this specific value was chosen for layerTypeAll.

Suggested change
// layerTypeAll represents a special value used to indicate all possible layer types.
// The value 255 is chosen because it is the maximum value that can be represented
// in an unsigned 8-bit integer, which aligns with the layer type representation in
// certain protocols or libraries.

Copilot uses AI. Check for mistakes.
case firewall.ProtocolALL:
return layerTypeAll
}
return 0
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

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

Returning 0 as a fallback in protoToLayer could be confusing. Consider using a named constant or documenting what this represents, as 0 might conflict with actual LayerType values.

Suggested change
return 0
return LayerTypeUnknown

Copilot uses AI. Check for mistakes.
@sonarqubecloud
Copy link

@sonarqubecloud
Copy link

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.

2 participants