Skip to content

Commit

Permalink
Add extra parameter for dual stack (IPv4+IPv6)
Browse files Browse the repository at this point in the history
Signed-off-by: Laszlo Kiraly <laszlo.kiraly@est.tech>
  • Loading branch information
ljkiraly committed Apr 26, 2022
1 parent 538a9ee commit 1c860cd
Showing 1 changed file with 61 additions and 12 deletions.
73 changes: 61 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2022 Nordix and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -21,12 +22,14 @@ package main

import (
"context"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"os/signal"
"path/filepath"
"strings"
"sync/atomic"
"syscall"
"time"
Expand Down Expand Up @@ -59,6 +62,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/common/onidle"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/policyroute"
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/ipam/point2pointipam"
registryclient "github.com/networkservicemesh/sdk/pkg/registry/chains/client"
registrysendfd "github.com/networkservicemesh/sdk/pkg/registry/common/sendfd"
Expand All @@ -84,6 +88,7 @@ type Config struct {
Labels map[string]string `default:"" desc:"Endpoint labels"`
DNSConfigs dnstools.Decoder `default:"[]" desc:"DNSConfigs represents array of DNSConfig in json format. See at model definition: https://github.com/networkservicemesh/api/blob/main/pkg/api/networkservice/connectioncontext.pb.go#L426-L435" split_words:"true"`
CidrPrefix string `default:"169.254.0.0/16" desc:"CIDR Prefix to assign IPs from" split_words:"true"`
Ipv6Prefix string `default:"" desc:"IPv6 CIDR Prefix to assign IPv6 addresses from (in dual-stack configuration)" split_words:"true"`
IdleTimeout time.Duration `default:"0" desc:"timeout for automatic shutdown when there were no requests for specified time. Set 0 to disable auto-shutdown." split_words:"true"`
RegisterService bool `default:"true" desc:"if true then registers network service on startup" split_words:"true"`
PBRConfigPath string `default:"/etc/policy-based-routing/config.yaml" desc:"Path to policy based routing config file" split_words:"true"`
Expand Down Expand Up @@ -186,10 +191,11 @@ func main() {
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 3: creating icmp server ipam")
// ********************************************************************************
_, ipnet, err := net.ParseCIDR(config.CidrPrefix)
ipamChain, err := getIPAMChain(config.CidrPrefix, config.Ipv6Prefix)
if err != nil {
log.FromContext(ctx).Fatalf("error parsing cidr: %+v", err)
log.FromContext(ctx).Fatalf("error parsing CIDR from config: %v", err)
}
log.FromContext(ctx).Infof("network prefixes parsed successfully")

// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 4: create icmp-server network service endpoint")
Expand All @@ -203,7 +209,7 @@ func main() {
endpoint.WithAuthorizeServer(authorize.NewServer()),
endpoint.WithAdditionalFunctionality(
onidle.NewServer(ctx, cancel, config.IdleTimeout),
point2pointipam.NewServer(ipnet),
ipamChain,
policyroute.NewServer(newPolicyRoutesGetter(ctx, config.PBRConfigPath).Get),
recvfd.NewServer(),
mechanisms.NewServer(map[string]networkservice.NetworkServiceServer{
Expand Down Expand Up @@ -278,15 +284,7 @@ func main() {
registrysendfd.NewNetworkServiceEndpointRegistryClient(),
),
)
nse := &registryapi.NetworkServiceEndpoint{
Name: config.Name,
NetworkServiceNames: config.ServiceNames,
NetworkServiceLabels: make(map[string]*registryapi.NetworkServiceLabels),
Url: listenOn.String(),
}
for _, serviceName := range config.ServiceNames {
nse.NetworkServiceLabels[serviceName] = &registryapi.NetworkServiceLabels{Labels: config.Labels}
}
nse := getNseEndpoint(config, listenOn)
nse, err = nseRegistryClient.Register(ctx, nse)
logrus.Infof("nse: %+v", nse)

Expand All @@ -302,6 +300,19 @@ func main() {
<-ctx.Done()
}

func getNseEndpoint(config *Config, listenOn fmt.Stringer) *registryapi.NetworkServiceEndpoint {
nse := &registryapi.NetworkServiceEndpoint{
Name: config.Name,
NetworkServiceNames: config.ServiceNames,
NetworkServiceLabels: make(map[string]*registryapi.NetworkServiceLabels),
Url: listenOn.String(),
}
for _, serviceName := range config.ServiceNames {
nse.NetworkServiceLabels[serviceName] = &registryapi.NetworkServiceLabels{Labels: config.Labels}
}
return nse
}

func getSriovTokenServerChainElement(ctx context.Context) (tokenServer networkservice.NetworkServiceServer) {
sriovTokens := tokens.FromEnv(os.Environ())
switch len(sriovTokens) {
Expand Down Expand Up @@ -376,3 +387,41 @@ type policyRoutesGetter struct {
ctx context.Context
policyRoutes atomic.Value
}

func getIPAMChain(ip4CIDR, ip6CIDR string) (networkservice.NetworkServiceServer, error) {
var ipNet1 *net.IPNet
var ipNet2 *net.IPNet

if ip4CIDR != "" {
var parseErr error
_, ipNet1, parseErr = net.ParseCIDR(ip4CIDR)
if parseErr != nil {
logrus.Fatalf("Could not parse IPv4 CIDR %s; %+v", ip4CIDR, parseErr)
}
// accept IPv6 address also for backward compatibility reason
}
if ip6CIDR != "" {
var parseErr error
_, ipNet2, parseErr = net.ParseCIDR(ip6CIDR)
if parseErr != nil {
logrus.Fatalf("Could not parse IPv6 CIDR: %s; %+v", ip6CIDR, parseErr)
}
if !strings.Contains(ip6CIDR, ":") {
return nil, errors.New("not ipv6 address is set")
}
}

switch {
case ipNet1 != nil && ipNet2 != nil:
return chain.NewNetworkServiceServer(
point2pointipam.NewServer(ipNet1),
point2pointipam.NewServer(ipNet2),
), nil
case ipNet2 != nil:
return chain.NewNetworkServiceServer(point2pointipam.NewServer(ipNet2)), nil
case ipNet1 != nil:
return chain.NewNetworkServiceServer(point2pointipam.NewServer(ipNet1)), nil
default:
return nil, errors.New("can not set CIDR")
}
}

0 comments on commit 1c860cd

Please sign in to comment.