Skip to content
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

Add DualStack IPPool that support both IPv4 and IPv6 addresses #1659

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/networkservice/ipam/strictipam/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ import (
"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/ippool"
"github.com/networkservicemesh/sdk/pkg/tools/dualstack"
)

type strictIPAMServer struct {
ipPool *ippool.IPPool
ipPool *dualstack.IPPool
}

// NewServer - returns a new ipam networkservice.NetworkServiceServer that validates the incoming IP context parameters and resets them based on the validation result.
func NewServer(newIPAMServer func(...*net.IPNet) networkservice.NetworkServiceServer, prefixes ...*net.IPNet) networkservice.NetworkServiceServer {
if newIPAMServer == nil {
panic("newIPAMServer should not be nil")
}
var ipPool = ippool.New(net.IPv6len)
var ipPool = dualstack.New()
for _, p := range prefixes {
ipPool.AddNet(p)
}
Expand Down
88 changes: 88 additions & 0 deletions pkg/tools/dualstack/ippool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2024 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package dualstack provides tools for managing both ipv4 and ipv6 addresses
package dualstack

import (
"net"

"github.com/pkg/errors"

"github.com/networkservicemesh/sdk/pkg/tools/ippool"
)

// IPPool holds available IPv4 and IPv6 addresses in the structure of red-black tree
type IPPool struct {
IPv4IPPool *ippool.IPPool
IPv6IPPool *ippool.IPPool
}

// New instantiates a dualstack ip pool as red-black tree
func New() *IPPool {
pool := new(IPPool)
pool.IPv4IPPool = ippool.New(net.IPv4len)
pool.IPv6IPPool = ippool.New(net.IPv6len)
return pool
}

// AddNetString - adds ip addresses from network to the pool by string value
func (p *IPPool) AddNetString(ipNetString string) {
_, ipNet, err := net.ParseCIDR(ipNetString)
if err != nil {
return
}
p.AddNet(ipNet)
}

// AddNet - adds ip addresses from network to the pool
func (p *IPPool) AddNet(ipNet *net.IPNet) {
if ipNet.IP.To4() != nil {
p.IPv4IPPool.AddNet(ipNet)
return
}
p.IPv6IPPool.AddNet(ipNet)
}

// ContainsString parses ip string and checks that pool contains ip
func (p *IPPool) ContainsString(in string) bool {
return p.Contains(net.ParseIP(in))
}

// Contains checks that pool contains ip
func (p *IPPool) Contains(ip net.IP) bool {
if ip.To4() != nil {
return p.IPv4IPPool.Contains(ip)
}
return p.IPv6IPPool.Contains(ip)
}

// PullIPString - returns requested IP address from the pool by string
func (p *IPPool) PullIPString(in string) (*net.IPNet, error) {
ip, _, err := net.ParseCIDR(in)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s as a CIDR", in)
}
return p.PullIP(ip)
}

// PullIP - returns requested IP address from the pool
func (p *IPPool) PullIP(ip net.IP) (*net.IPNet, error) {
if ip.To4() != nil {
return p.IPv4IPPool.PullIP(ip)
}
return p.IPv6IPPool.PullIP(ip)
}
56 changes: 56 additions & 0 deletions pkg/tools/dualstack/ippool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dualstack

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestContains(t *testing.T) {
ipPool := New()
ipPool.AddNetString("192.168.0.0/16")
ipPool.AddNetString("ff80::/64")
require.True(t, ipPool.ContainsString("192.168.0.1"))
require.False(t, ipPool.ContainsString("193.169.0.1"))
require.True(t, ipPool.ContainsString("ff80::ff10"))
require.False(t, ipPool.ContainsString("ff90::ff10"))
}

//nolint:dupl
func TestPull(t *testing.T) {
ipPool := New()
ipPool.AddNetString("192.0.0.0/8")
require.NotNil(t, ipPool)

ip, err := ipPool.PullIPString("192.168.0.1/32")
require.NoError(t, err)
require.Equal(t, ip.String(), "192.168.0.1/32")

ipPool.AddNetString("ff80::/64")
ip, err = ipPool.PullIPString("ff80::1/128")
require.NoError(t, err)
require.Equal(t, ip.String(), "ff80::1/128")
}

func TestInvalidCIDR(t *testing.T) {
ipPool := New()
ipPool.AddNetString("300.168.0.0/16")
_, err := ipPool.PullIPString("300.168.0.0/32")
require.Error(t, err)
}
Loading