Skip to content

Conversation

@YutaroHayakawa
Copy link
Contributor

@YutaroHayakawa YutaroHayakawa commented Nov 10, 2025

Add support for Light Weight Tunnel encapsulation in the nexthop object. Since the netlink encoding is exactly the same as the one for route, we can reuse most of the encoding/decoding code. What we needed to do was supporting NHA_ENCAP_TYPE and NHA_ENCAP types.

Summary by CodeRabbit

  • New Features
    • Added encapsulation support for nexthops with multiple encap types and ensured encapsulation is included when creating nexthops; route decoding now centralizes encap handling.
  • Bug Fixes
    • Nexthop string output is now safe for nil receivers and shows encap presence or absence.
  • Tests
    • Added tests for nexthop encapsulation and string handling to validate create/retrieve behavior.

@coderabbitai
Copy link

coderabbitai bot commented Nov 10, 2025

Walkthrough

Adds Encap support to nexthops: new public Encap field on Nexthop, broadened nexthop attribute decoder signatures to receive all attrs, added NHA_ENCAP_TYPE / NHA_ENCAP handlers and encode paths, introduced shared decodeEncap helper, and added tests for String() and encap round-trip.

Changes

Cohort / File(s) Summary
Nexthop struct & String
netlink/nexthop.go
Added public field Encap Encap to Nexthop; Nexthop.String() now handles nil receiver and includes an Encap: entry (renders Encap.String() or <nil>).
Nexthop attribute handlers
netlink/nexthop_linux.go
Changed nexthop attribute handler decode signature to func(*Nexthop, *nl.RtAttr, []*nl.RtAttr); added handlers for unix.NHA_ENCAP_TYPE (encode) and unix.NHA_ENCAP (encode + decode); NHA_ENCAP decode uses the full attrs slice to find NHA_ENCAP_TYPE and decode encapsulation; prepare/encode path now includes both attributes.
Encap decoding helper & route usage
route_linux.go
Added decodeEncap(typ int, v []byte) (Encap, error) to centralize decoding of MPLS, SEG6, SEG6_LOCAL, BPF, and IP6 encapsulation types; replaced inline encap decoding in route deserialization with this helper.
Tests
netlink/nexthop_test.go
Added TestNexthopString (nil-safe String) and TestNexthopEncap to exercise creating, encoding, and decoding a nexthop with SEG6 encap; added nl import for tests.

Sequence Diagram(s)

sequenceDiagram
    participant Kernel as Kernel (netlink attrs)
    participant Decoder as decodeNexthopAttrs
    participant Handler as nexthopAttrHandler
    participant Nexthop as Nexthop
    participant decodeEncap as decodeEncap()

    rect rgb(245,250,255)
    note right of Kernel: incoming nexthop attributes (attrs[])
    end

    Kernel->>Decoder: send attrs[]
    Decoder->>Handler: for each attr -> handler.decode(nh, attr, attrs)
    alt attr == NHA_ENCAP
        Handler->>Handler: locate NHA_ENCAP_TYPE in attrs[]
        Handler->>decodeEncap: decodeEncap(encapType, attr.Value)
        decodeEncap-->>Handler: Encap or error
        Handler->>Nexthop: nh.Encap = decoded Encap
    else other attrs
        Handler->>Nexthop: populate other fields
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Verify correct propagation of the new decode signature across all attribute handlers.
  • Confirm NHA_ENCAP decode reliably finds and validates NHA_ENCAP_TYPE.
  • Review decodeEncap mappings and route deserialization call sites for consistency and error handling.

Possibly related PRs

Suggested reviewers

  • borkmann
  • tklauser

Poem

🐰 I nibble bytes and hop with cheer,

Encapsulations now stay near,
Types and attrs together bind,
Nexthops carry trails aligned,
A tiny hop for netlink here.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'nexthop: Support LWT encapsulation' accurately summarizes the main change - adding Light Weight Tunnel encapsulation support to the nexthop object, which aligns with all the modifications across nexthop.go, nexthop_linux.go, nexthop_test.go, and route_linux.go.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6ace38b and 18c7bdd.

📒 Files selected for processing (4)
  • nexthop.go (1 hunks)
  • nexthop_linux.go (7 hunks)
  • nexthop_test.go (2 hunks)
  • route_linux.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • nexthop.go
🧰 Additional context used
🧬 Code graph analysis (3)
route_linux.go (2)
route.go (1)
  • Encap (40-46)
nl/syscall.go (5)
  • LWTUNNEL_ENCAP_MPLS (62-62)
  • LWTUNNEL_ENCAP_SEG6 (66-66)
  • LWTUNNEL_ENCAP_SEG6_LOCAL (68-68)
  • LWTUNNEL_ENCAP_BPF (67-67)
  • LWTUNNEL_ENCAP_IP6 (65-65)
nexthop_test.go (5)
nexthop.go (1)
  • Nexthop (10-17)
route.go (1)
  • Encap (40-46)
route_linux.go (1)
  • SEG6Encap (202-205)
nl/seg6_linux.go (1)
  • SEG6_IPTUN_MODE_ENCAP (42-42)
nexthop_linux.go (2)
  • NexthopAdd (13-15)
  • NexthopList (69-71)
nexthop_linux.go (5)
nexthop.go (1)
  • Nexthop (10-17)
nl/nl_linux.go (2)
  • RtAttr (395-399)
  • NewRtAttr (402-410)
route.go (1)
  • Encap (40-46)
nl/conntrack_linux.go (1)
  • NLA_F_NESTED (77-77)
nl/syscall.go (1)
  • LWTUNNEL_ENCAP_NONE (61-61)
🔇 Additional comments (8)
nexthop_test.go (2)

15-22: LGTM!

Defensive nil-safety test for String() method.


167-222: LGTM!

Comprehensive test for Encap round-trip with SEG6 encapsulation.

route_linux.go (2)

1572-1577: LGTM!

Clean refactoring to use the centralized decodeEncap helper.


1582-1612: LGTM!

The centralized decodeEncap helper properly dispatches to the appropriate Encap implementation based on type and propagates decode errors.

nexthop_linux.go (4)

180-189: LGTM!

NHA_ENCAP_TYPE encode handler properly extracts and encodes the encap type.


201-220: LGTM!

The decode logic properly searches for NHA_ENCAP_TYPE, validates bounds, and delegates to the shared decodeEncap helper. Silent error handling matches the pattern used by other attribute decoders in this file.


297-303: LGTM!

NHA_ENCAP_TYPE and NHA_ENCAP correctly added to the set of attributes encoded during nexthop creation.


191-200: Address error handling in Encap.Encode() or document why silent omission is acceptable.

The NHA_ENCAP encode handler is unique among encode functions in this file—it's the only one calling an error-returning method. When nh.Encap.Encode() fails, the handler returns nil, causing the attribute to be silently omitted from the netlink request (line 235-237 shows callers check if attr != nil before appending). This could send incomplete nexthop configuration to the kernel.

Either:

  1. Propagate the error to the caller so incomplete operations can be detected, or
  2. Document that silent attribute omission on encode error is intentional and acceptable for your use case.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@YutaroHayakawa YutaroHayakawa marked this pull request as ready for review November 10, 2025 21:27
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
route_linux.go (1)

1582-1612: Good refactoring: centralized encap decoding.

Extracting the encap type decoding logic into a helper function eliminates duplication and will be reused by the nexthop encap decoding. The function correctly handles all supported encap types.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6e61cd4 and 6ace38b.

📒 Files selected for processing (4)
  • nexthop.go (2 hunks)
  • nexthop_linux.go (7 hunks)
  • nexthop_test.go (2 hunks)
  • route_linux.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (4)
route_linux.go (2)
route.go (1)
  • Encap (40-46)
nl/syscall.go (5)
  • LWTUNNEL_ENCAP_MPLS (62-62)
  • LWTUNNEL_ENCAP_SEG6 (66-66)
  • LWTUNNEL_ENCAP_SEG6_LOCAL (68-68)
  • LWTUNNEL_ENCAP_BPF (67-67)
  • LWTUNNEL_ENCAP_IP6 (65-65)
nexthop.go (1)
route.go (1)
  • Encap (40-46)
nexthop_test.go (5)
nexthop.go (1)
  • Nexthop (10-17)
route.go (1)
  • Encap (40-46)
route_linux.go (1)
  • SEG6Encap (202-205)
nl/seg6_linux.go (1)
  • SEG6_IPTUN_MODE_ENCAP (42-42)
nexthop_linux.go (2)
  • NexthopAdd (13-15)
  • NexthopList (69-71)
nexthop_linux.go (5)
nexthop.go (1)
  • Nexthop (10-17)
nl/nl_linux.go (2)
  • RtAttr (395-399)
  • NewRtAttr (402-410)
route.go (1)
  • Encap (40-46)
nl/conntrack_linux.go (1)
  • NLA_F_NESTED (77-77)
nl/syscall.go (1)
  • LWTUNNEL_ENCAP_NONE (61-61)
🔇 Additional comments (9)
nexthop.go (1)

16-16: LGTM! Encap field added to Nexthop struct.

The addition of the Encap field follows the existing pattern and correctly uses the Encap interface type.

nexthop_test.go (1)

158-213: Well-structured test for nexthop encapsulation.

The test properly validates the round-trip encoding/decoding of SEG6Encap, including type assertions and field verification.

route_linux.go (1)

1572-1577: LGTM! Inline decoding replaced with helper function.

The refactoring to use decodeEncap reduces code duplication while maintaining the same error handling behavior.

nexthop_linux.go (6)

115-118: Good: Updated decode signature for multi-attribute context.

The signature change to accept the full attribute list enables decoders like NHA_ENCAP to access related attributes (e.g., NHA_ENCAP_TYPE) for proper decoding.


129-179: LGTM! Existing decoders updated for new signature.

The unused parameter _ []*nl.RtAttr maintains signature compatibility with the updated decode function type.


180-189: LGTM! NHA_ENCAP_TYPE encode handler.

The encode function correctly emits the encapsulation type when Encap is set. Note that this attribute has no decode function because NHA_ENCAP's decoder handles both attributes together.


252-252: Correct: Full attribute list passed to decoders.

This change enables the NHA_ENCAP decoder to access NHA_ENCAP_TYPE from the full attribute list.


301-302: NHA_ENCAP_TYPE and NHA_ENCAP added to encoding.

Correctly includes both encapsulation attributes in the prepareNewNexthop encoding path.


190-221: Encoding errors are silently ignored—confirmed, but by architectural design.

The review comment is correct: lines 194-196 return nil when nh.Encap.Encode() fails, causing the attribute to be silently skipped. However, this behavior is not accidental—the nexthopAttrHandlers framework is architected this way.

The encode signature func(*Nexthop) *nl.RtAttr (line 112) deliberately supports only nil returns, with an explicit comment (line 111): "It should return nil if the attribute is not set." This means:

  • Returning nil on error is indistinguishable from the attribute being unset
  • encodeNexthopAttrs (line 234) skips nil returns: if attr != nil { append }
  • The result: encoding failures silently disappear without trace

The suggested fix requires either:

  1. Changing the encode signature across all nexthopAttrHandlers to return errors, and updating encodeNexthopAttrs to propagate them, or
  2. Adding error logging within individual handlers like NHA_ENCAP

Verify with the maintainers whether silent attribute skips on encoding errors are acceptable for nexthop updates, or if the architectural constraint should be lifted to support error propagation.

@YutaroHayakawa YutaroHayakawa force-pushed the yutaro/nexthop-lwt-encap branch from 6ace38b to 910cd5f Compare November 11, 2025 06:18
Add support for Light Weight Tunnel encapsulation in the nexthop object.
Since the netlink encoding is exactly the same as the one for route, we
can reuse most of the encoding/decoding code. What we needed to do was
supporting NHA_ENCAP_TYPE and NHA_ENCAP types.

Signed-off-by: Yutaro Hayakawa <[email protected]>
Handle nil receiver case and all-zero values case. Also added test cases
for that so that we can check this for the fields added in the future.

Signed-off-by: Yutaro Hayakawa <[email protected]>
@YutaroHayakawa YutaroHayakawa force-pushed the yutaro/nexthop-lwt-encap branch from 910cd5f to 18c7bdd Compare November 11, 2025 06:21
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.

1 participant