Skip to content

Commit 4b786ff

Browse files
authored
xds: replace net with netip in xds/xdsclient and xds/server (#8909)
Updates #8884 This PR replace `net.IP` and `net.IPNet` with `netip.Addr` and `netip.Prefix` in directories `internal/xds/xdsclient` and `internal/xds/server` RELEASE NOTES: - TBD
1 parent ccc8d7b commit 4b786ff

File tree

7 files changed

+119
-117
lines changed

7 files changed

+119
-117
lines changed

internal/xds/server/filter_chain_manager.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24-
"net"
24+
"net/netip"
2525
"slices"
2626
"strings"
2727
"sync/atomic"
@@ -67,7 +67,7 @@ func newFilterChainManager(filterChainConfigs *xdsresource.NetworkFilterChainMap
6767

6868
if filterChainConfigs != nil {
6969
for _, entry := range filterChainConfigs.DstPrefixes {
70-
dstEntry := &destPrefixEntry{net: entry.Prefix}
70+
dstEntry := &destPrefixEntry{prefix: entry.Prefix}
7171

7272
for i, srcPrefixes := range entry.SourceTypeArr {
7373
if len(srcPrefixes.Entries) == 0 {
@@ -77,7 +77,7 @@ func newFilterChainManager(filterChainConfigs *xdsresource.NetworkFilterChainMap
7777
dstEntry.srcTypeArr[i] = stDest
7878
for _, srcEntryConfig := range srcPrefixes.Entries {
7979
srcEntry := &sourcePrefixEntry{
80-
net: srcEntryConfig.Prefix,
80+
prefix: srcEntryConfig.Prefix,
8181
srcPortMap: make(map[int]*filterChain, len(srcEntryConfig.PortMap)),
8282
}
8383
stDest.srcPrefixes = append(stDest.srcPrefixes, srcEntry)
@@ -140,7 +140,7 @@ func (fcm *filterChainManager) stop() {
140140
// destPrefixEntry contains a destination prefix entry and associated source
141141
// type matchers.
142142
type destPrefixEntry struct {
143-
net *net.IPNet
143+
prefix netip.Prefix
144144
srcTypeArr sourceTypesArray
145145
}
146146

@@ -164,7 +164,7 @@ type sourcePrefixes struct {
164164
// sourcePrefixEntry contains a source prefix entry and associated source port
165165
// matchers.
166166
type sourcePrefixEntry struct {
167-
net *net.IPNet
167+
prefix netip.Prefix
168168
srcPortMap map[int]*filterChain
169169
}
170170

@@ -204,10 +204,10 @@ type routeWithInterceptors struct {
204204
}
205205

206206
type lookupParams struct {
207-
isUnspecifiedListener bool // Whether the server is listening on a wildcard address.
208-
dstAddr net.IP // dstAddr is the local address of an incoming connection.
209-
srcAddr net.IP // srcAddr is the remote address of an incoming connection.
210-
srcPort int // srcPort is the remote port of an incoming connection.
207+
isUnspecifiedListener bool // Whether the server is listening on a wildcard address.
208+
dstAddr netip.Addr // dstAddr is the local address of an incoming connection.
209+
srcAddr netip.Addr // srcAddr is the remote address of an incoming connection.
210+
srcPort int // srcPort is the remote port of an incoming connection.
211211
}
212212

213213
// lookup returns the most specific matching filter chain to be used for an
@@ -223,7 +223,7 @@ func (fcm *filterChainManager) lookup(params lookupParams) (*filterChain, error)
223223
}
224224

225225
srcType := sourceTypeExternal
226-
if params.srcAddr.Equal(params.dstAddr) || params.srcAddr.IsLoopback() {
226+
if params.srcAddr == params.dstAddr || params.srcAddr.IsLoopback() {
227227
srcType = sourceTypeSameOrLoopback
228228
}
229229
srcPrefixes := filterBySourceType(dstPrefixes, srcType)
@@ -250,7 +250,7 @@ func (fcm *filterChainManager) lookup(params lookupParams) (*filterChain, error)
250250
// matching algorithm. It takes the complete set of configured filter chain
251251
// matchers and returns the most specific matchers based on the destination
252252
// prefix match criteria (the prefixes which match the most number of bits).
253-
func filterByDestinationPrefixes(dstPrefixes []*destPrefixEntry, isUnspecified bool, dstAddr net.IP) []*destPrefixEntry {
253+
func filterByDestinationPrefixes(dstPrefixes []*destPrefixEntry, isUnspecified bool, dstAddr netip.Addr) []*destPrefixEntry {
254254
if !isUnspecified {
255255
// Destination prefix matchers are considered only when the listener is
256256
// bound to the wildcard address.
@@ -259,18 +259,18 @@ func filterByDestinationPrefixes(dstPrefixes []*destPrefixEntry, isUnspecified b
259259

260260
var matchingDstPrefixes []*destPrefixEntry
261261
maxSubnetMatch := noPrefixMatch
262-
for _, prefix := range dstPrefixes {
263-
if prefix.net != nil && !prefix.net.Contains(dstAddr) {
262+
for _, entry := range dstPrefixes {
263+
if entry.prefix.IsValid() && !entry.prefix.Contains(dstAddr) {
264264
// Skip prefixes which don't match.
265265
continue
266266
}
267-
// For unspecified prefixes, since we do not store a real net.IPNet
267+
// For unspecified prefixes, since we do not store a real netip.Prefix
268268
// inside prefix, we do not perform a match. Instead we simply set
269269
// the matchSize to -1, which is less than the matchSize (0) for a
270270
// wildcard prefix.
271271
matchSize := unspecifiedPrefixMatch
272-
if prefix.net != nil {
273-
matchSize, _ = prefix.net.Mask.Size()
272+
if entry.prefix.IsValid() {
273+
matchSize = entry.prefix.Bits()
274274
}
275275
if matchSize < maxSubnetMatch {
276276
continue
@@ -279,7 +279,7 @@ func filterByDestinationPrefixes(dstPrefixes []*destPrefixEntry, isUnspecified b
279279
maxSubnetMatch = matchSize
280280
matchingDstPrefixes = make([]*destPrefixEntry, 0, 1)
281281
}
282-
matchingDstPrefixes = append(matchingDstPrefixes, prefix)
282+
matchingDstPrefixes = append(matchingDstPrefixes, entry)
283283
}
284284
return matchingDstPrefixes
285285
}
@@ -293,12 +293,12 @@ func filterBySourceType(dstPrefixes []*destPrefixEntry, srcType sourceType) []*s
293293
srcPrefixes []*sourcePrefixes
294294
bestSrcTypeMatch sourceType
295295
)
296-
for _, prefix := range dstPrefixes {
296+
for _, entry := range dstPrefixes {
297297
match := srcType
298-
srcPrefix := prefix.srcTypeArr[srcType]
298+
srcPrefix := entry.srcTypeArr[srcType]
299299
if srcPrefix == nil {
300300
match = sourceTypeAny
301-
srcPrefix = prefix.srcTypeArr[sourceTypeAny]
301+
srcPrefix = entry.srcTypeArr[sourceTypeAny]
302302
}
303303
if match < bestSrcTypeMatch {
304304
continue
@@ -319,22 +319,22 @@ func filterBySourceType(dstPrefixes []*destPrefixEntry, srcType sourceType) []*s
319319
// filterBySourcePrefixes is the third stage of the filter chain matching
320320
// algorithm. It trims the filter chains based on the source prefix. At most one
321321
// filter chain with the most specific match progress to the next stage.
322-
func filterBySourcePrefixes(srcPrefixes []*sourcePrefixes, srcAddr net.IP) (*sourcePrefixEntry, error) {
322+
func filterBySourcePrefixes(srcPrefixes []*sourcePrefixes, srcAddr netip.Addr) (*sourcePrefixEntry, error) {
323323
var matchingSrcPrefixes []*sourcePrefixEntry
324324
maxSubnetMatch := noPrefixMatch
325325
for _, sp := range srcPrefixes {
326-
for _, prefix := range sp.srcPrefixes {
327-
if prefix.net != nil && !prefix.net.Contains(srcAddr) {
326+
for _, entry := range sp.srcPrefixes {
327+
if entry.prefix.IsValid() && !entry.prefix.Contains(srcAddr) {
328328
// Skip prefixes which don't match.
329329
continue
330330
}
331-
// For unspecified prefixes, since we do not store a real net.IPNet
331+
// For unspecified prefixes, since we do not store a real netip.Prefix
332332
// inside prefix, we do not perform a match. Instead we simply set
333333
// the matchSize to -1, which is less than the matchSize (0) for a
334334
// wildcard prefix.
335335
matchSize := unspecifiedPrefixMatch
336-
if prefix.net != nil {
337-
matchSize, _ = prefix.net.Mask.Size()
336+
if entry.prefix.IsValid() {
337+
matchSize = entry.prefix.Bits()
338338
}
339339
if matchSize < maxSubnetMatch {
340340
continue
@@ -343,7 +343,7 @@ func filterBySourcePrefixes(srcPrefixes []*sourcePrefixes, srcAddr net.IP) (*sou
343343
maxSubnetMatch = matchSize
344344
matchingSrcPrefixes = make([]*sourcePrefixEntry, 0, 1)
345345
}
346-
matchingSrcPrefixes = append(matchingSrcPrefixes, prefix)
346+
matchingSrcPrefixes = append(matchingSrcPrefixes, entry)
347347
}
348348
}
349349
if len(matchingSrcPrefixes) == 0 {

internal/xds/server/filter_chain_manager_test.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24-
"net"
2524
"net/netip"
2625
"strings"
2726
"testing"
@@ -219,7 +218,7 @@ func (s) TestLookup_Failures(t *testing.T) {
219218
},
220219
params: lookupParams{
221220
isUnspecifiedListener: true,
222-
dstAddr: net.IPv4(10, 1, 1, 1),
221+
dstAddr: netip.AddrFrom4([4]byte([]byte{10, 1, 1, 1})),
223222
},
224223
wantErr: "no matching filter chain based on destination prefix match",
225224
},
@@ -238,8 +237,8 @@ func (s) TestLookup_Failures(t *testing.T) {
238237
},
239238
params: lookupParams{
240239
isUnspecifiedListener: true,
241-
dstAddr: net.IPv4(192, 168, 100, 1),
242-
srcAddr: net.IPv4(192, 168, 100, 2),
240+
dstAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 1})),
241+
srcAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 2})),
243242
},
244243
wantErr: "no matching filter chain based on source type match",
245244
},
@@ -258,8 +257,8 @@ func (s) TestLookup_Failures(t *testing.T) {
258257
},
259258
params: lookupParams{
260259
isUnspecifiedListener: true,
261-
dstAddr: net.IPv4(192, 168, 100, 1),
262-
srcAddr: net.IPv4(192, 168, 100, 1),
260+
dstAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 1})),
261+
srcAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 1})),
263262
},
264263
wantErr: "no matching filter chain after all match criteria",
265264
},
@@ -283,8 +282,8 @@ func (s) TestLookup_Failures(t *testing.T) {
283282
params: lookupParams{
284283
// IsUnspecified is not set. This means that the destination
285284
// prefix matchers will be ignored.
286-
dstAddr: net.IPv4(192, 168, 100, 1),
287-
srcAddr: net.IPv4(192, 168, 100, 1),
285+
dstAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 1})),
286+
srcAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 1})),
288287
srcPort: 1,
289288
},
290289
wantErr: "multiple matching filter chains",
@@ -301,8 +300,8 @@ func (s) TestLookup_Failures(t *testing.T) {
301300
},
302301
params: lookupParams{
303302
isUnspecifiedListener: true,
304-
dstAddr: net.IPv4(192, 168, 100, 1),
305-
srcAddr: net.IPv4(192, 168, 100, 1),
303+
dstAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 1})),
304+
srcAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 1})),
306305
srcPort: 80,
307306
},
308307
wantErr: "no matching filter chain after all match criteria",
@@ -419,7 +418,7 @@ func (s) TestLookup_Successes(t *testing.T) {
419418
lis: lisWithDefaultChain,
420419
params: lookupParams{
421420
isUnspecifiedListener: true,
422-
dstAddr: net.IPv4(10, 1, 1, 1),
421+
dstAddr: netip.AddrFrom4([4]byte([]byte{10, 1, 1, 1})),
423422
},
424423
wantFC: &filterChain{
425424
securityCfg: &xdsresource.SecurityConfig{IdentityInstanceName: "default"},
@@ -432,8 +431,8 @@ func (s) TestLookup_Successes(t *testing.T) {
432431
lis: lisWithoutDefaultChain,
433432
params: lookupParams{
434433
isUnspecifiedListener: true,
435-
dstAddr: netip.MustParseAddr("2001:68::db8").AsSlice(),
436-
srcAddr: net.IPv4(10, 1, 1, 1),
434+
dstAddr: netip.MustParseAddr("2001:68::db8"),
435+
srcAddr: netip.AddrFrom4([4]byte([]byte{10, 1, 1, 1})),
437436
srcPort: 1,
438437
},
439438
wantFC: &filterChain{
@@ -447,8 +446,8 @@ func (s) TestLookup_Successes(t *testing.T) {
447446
lis: lisWithoutDefaultChain,
448447
params: lookupParams{
449448
isUnspecifiedListener: true,
450-
dstAddr: net.IPv4(10, 1, 1, 1),
451-
srcAddr: net.IPv4(10, 1, 1, 1),
449+
dstAddr: netip.AddrFrom4([4]byte([]byte{10, 1, 1, 1})),
450+
srcAddr: netip.AddrFrom4([4]byte([]byte{10, 1, 1, 1})),
452451
srcPort: 1,
453452
},
454453
wantFC: &filterChain{
@@ -462,8 +461,8 @@ func (s) TestLookup_Successes(t *testing.T) {
462461
lis: lisWithoutDefaultChain,
463462
params: lookupParams{
464463
isUnspecifiedListener: true,
465-
dstAddr: netip.MustParseAddr("2001:68::1").AsSlice(),
466-
srcAddr: netip.MustParseAddr("2001:68::2").AsSlice(),
464+
dstAddr: netip.MustParseAddr("2001:68::1"),
465+
srcAddr: netip.MustParseAddr("2001:68::2"),
467466
srcPort: 1,
468467
},
469468
wantFC: &filterChain{
@@ -477,8 +476,8 @@ func (s) TestLookup_Successes(t *testing.T) {
477476
lis: lisWithoutDefaultChain,
478477
params: lookupParams{
479478
isUnspecifiedListener: true,
480-
dstAddr: net.IPv4(192, 168, 100, 1),
481-
srcAddr: net.IPv4(192, 168, 100, 1),
479+
dstAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 1})),
480+
srcAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 1})),
482481
srcPort: 80,
483482
},
484483
wantFC: &filterChain{
@@ -492,8 +491,8 @@ func (s) TestLookup_Successes(t *testing.T) {
492491
lis: lisWithoutDefaultChain,
493492
params: lookupParams{
494493
isUnspecifiedListener: true,
495-
dstAddr: net.IPv4(192, 168, 1, 1),
496-
srcAddr: net.IPv4(10, 1, 1, 1),
494+
dstAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 1, 1})),
495+
srcAddr: netip.AddrFrom4([4]byte([]byte{10, 1, 1, 1})),
497496
srcPort: 80,
498497
},
499498
wantFC: &filterChain{
@@ -507,8 +506,8 @@ func (s) TestLookup_Successes(t *testing.T) {
507506
lis: lisWithoutDefaultChain,
508507
params: lookupParams{
509508
isUnspecifiedListener: true,
510-
dstAddr: net.IPv4(192, 168, 1, 1),
511-
srcAddr: net.IPv4(192, 168, 92, 100),
509+
dstAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 1, 1})),
510+
srcAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 92, 100})),
512511
srcPort: 70,
513512
},
514513
wantFC: &filterChain{
@@ -522,8 +521,8 @@ func (s) TestLookup_Successes(t *testing.T) {
522521
lis: lisWithoutDefaultChain,
523522
params: lookupParams{
524523
isUnspecifiedListener: true,
525-
dstAddr: net.IPv4(192, 168, 1, 1),
526-
srcAddr: net.IPv4(192, 168, 92, 100),
524+
dstAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 1, 1})),
525+
srcAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 92, 100})),
527526
srcPort: 80,
528527
},
529528
wantFC: &filterChain{
@@ -779,8 +778,8 @@ func (s) TestLookup_DroppedChainFallback(t *testing.T) {
779778
}
780779
params := lookupParams{
781780
isUnspecifiedListener: true,
782-
dstAddr: net.IPv4(192, 168, 100, 1),
783-
srcAddr: net.IPv4(192, 168, 100, 1),
781+
dstAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 1})),
782+
srcAddr: netip.AddrFrom4([4]byte([]byte{192, 168, 100, 1})),
784783
srcPort: 80,
785784
}
786785

internal/xds/server/listener_wrapper.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package server
2323
import (
2424
"fmt"
2525
"net"
26+
"net/netip"
2627
"sync"
2728
"time"
2829

@@ -307,10 +308,12 @@ func (l *listenerWrapper) Accept() (net.Conn, error) {
307308
continue
308309
}
309310

311+
destIP, _ := netip.AddrFromSlice(destAddr.IP)
312+
srcIP, _ := netip.AddrFromSlice(srcAddr.IP)
310313
fc, err := l.activeFilterChainManager.lookup(lookupParams{
311314
isUnspecifiedListener: l.isUnspecifiedAddr,
312-
dstAddr: destAddr.IP,
313-
srcAddr: srcAddr.IP,
315+
dstAddr: destIP.Unmap(),
316+
srcAddr: srcIP.Unmap(),
314317
srcPort: srcAddr.Port,
315318
})
316319
if err != nil {

internal/xds/xdsclient/xdsresource/filter_chain.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package xdsresource
1919

2020
import (
2121
"fmt"
22-
"net"
22+
"net/netip"
2323

2424
"google.golang.org/grpc/internal/xds/xdsclient/xdsresource/version"
2525

@@ -40,7 +40,7 @@ type NetworkFilterChainMap struct {
4040
// source type matchers.
4141
type DestinationPrefixEntry struct {
4242
// Prefix is the destination IP prefix.
43-
Prefix *net.IPNet
43+
Prefix netip.Prefix
4444
// SourceTypeArr contains the source type matchers. The supported source
4545
// types and their associated indices in the array are:
4646
// - 0: Any: matches connection attempts from any source.
@@ -59,7 +59,7 @@ type SourcePrefixes struct {
5959
// port matchers.
6060
type SourcePrefixEntry struct {
6161
// Prefix is the source IP prefix.
62-
Prefix *net.IPNet
62+
Prefix netip.Prefix
6363
// PortMap contains the matchers for source ports.
6464
PortMap map[int]NetworkFilterChainConfig
6565
}

0 commit comments

Comments
 (0)