Skip to content

Net utils for finding default network interfaces #4665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 2, 2025

Conversation

amogh09
Copy link
Contributor

@amogh09 amogh09 commented May 30, 2025

Summary

Fault injection endpoint handlers require finding the default network interfaces on the host so that faults can be applied to those interfaces. So far we only needed the name of the default interface but now we need their IP addresses also for IPv6 specific logic. This PR adds new utility functions to find default IPv4 and IPv6 interfaces and their IPv4 and IPv6 addresses. The functions will be consumed in upcoming PRs.

Implementation details

This PR adds three new functions to handle default network interface detection:

  1. Lower level GetDefaultNetworkInterface:

    • Finds the highest priority default route for the specified IP family
    • A default route must have a nil/default destination and a gateway address
    • Returns the corresponding network link or os.ErrNotExist if no default route exists
  2. Higher level GetDefaultNetworkInterface:

    • Builds upon the lower level function
    • Returns a NetworkInterface struct containing:
      • Device name
      • All global unicast IPv4 addresses
      • All global unicast IPv6 addresses
    • The IP addresses are returned regardless of the specified IP family
  3. GetDefaultNetworkInterfaces:

    • Returns information about both IPv4 and IPv6 default interfaces
    • If both default routes use the same interface, returns just one entry
    • Returns an empty slice if no default routes are found
    • Handles partial configurations (e.g., only IPv4 default route exists)

The implementation filters out non-global unicast IP addresses (link-local, loopback, etc.) to ensure only routable addresses are returned.

Testing

Wrote a temporary test to test GetDefaultNetworkInterfaces manually without involving any mocks.

func TestMyTest(t *testing.T) {
    result, err := GetDefaultNetworkInterfaces(netlinkwrapper.New())
    require.NoError(t, err)
    fmt.Println(result)
}

On an instance with a dual-stack ENI, a single default network interface is found with IPv4 and IPv6 addresses.

ip-10-0-0-23 ❱ go test -tags unit ./tmds/utils -v -run TestMyTest
=== RUN   TestMyTest
[{ens5 [10.0.0.23] [2600:1f14:323a:e001:3120:f10b:dc95:ce8f]}]
--- PASS: TestMyTest (0.00s)
PASS

On an instance with two ENIs - one IPv4-only and one IPv6-only - two default network interfaces are found.

[ec2-user@ip-10-0-1-50 ecs-agent]$ go test -tags unit ./tmds/utils -v -run TestMyTest
=== RUN   TestMyTest
[{ens5 [10.0.1.50] []} {ens6 [] [2600:1f14:323a:e000:5448:955f:8c78:b6c5]}]
--- PASS: TestMyTest (0.00s)
PASS

New tests cover the changes: yes

Description for the changelog

NA

Additional Information

Does this PR include breaking model changes? If so, Have you added transformation functions?

No

Does this PR include the addition of new environment variables in the README?

No

Licensing

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@amogh09 amogh09 force-pushed the ipv6-tmds-netutils branch from 965e341 to a877a5f Compare May 30, 2025 22:49
@amogh09 amogh09 force-pushed the ipv6-tmds-netutils branch from 81097e0 to c06e9b1 Compare May 30, 2025 23:17
@amogh09 amogh09 marked this pull request as ready for review May 30, 2025 23:32
@amogh09 amogh09 requested a review from a team as a code owner May 30, 2025 23:32
xxx0624
xxx0624 previously approved these changes Jun 2, 2025
@amogh09 amogh09 merged commit d197a29 into aws:dev Jun 2, 2025
40 checks passed
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