Skip to content

Commit

Permalink
Add vlan remote mechanism support (see #358) (#365)
Browse files Browse the repository at this point in the history
Add domain to device mapping

see related:
networkservicemesh/sdk-vpp#399

Signed-off-by: Laszlo Kiraly <[email protected]>
  • Loading branch information
ljkiraly authored Nov 22, 2021
1 parent e45c47f commit 5007aba
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/networkservicemesh/sdk v0.5.1-0.20211119122624-518ed724f8c6
github.com/networkservicemesh/sdk-k8s v0.0.0-20211103091718-33b5af79cf03
github.com/networkservicemesh/sdk-sriov v0.0.0-20211117213205-b1980f419277
github.com/networkservicemesh/sdk-vpp v0.0.0-20211122163200-210bc45fb1bc
github.com/networkservicemesh/sdk-vpp v0.0.0-20211122201437-8b7a950e7c29
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.7.0
github.com/spiffe/go-spiffe/v2 v2.0.0-beta.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ github.com/networkservicemesh/sdk-kernel v0.0.0-20211122162902-8ac04fdcd0a8 h1:Q
github.com/networkservicemesh/sdk-kernel v0.0.0-20211122162902-8ac04fdcd0a8/go.mod h1:sDxemuiJE/VJIMNvj51Y5PFLoyBlQfz2sX7wmwe8aEQ=
github.com/networkservicemesh/sdk-sriov v0.0.0-20211117213205-b1980f419277 h1:FzDi+Ogpm6GMlx8AupMuiPlw8RyKYmZpg2YLJQZ6vDw=
github.com/networkservicemesh/sdk-sriov v0.0.0-20211117213205-b1980f419277/go.mod h1:3/Fucpztn8a0jBk/JIFCLBG5rjkfwrsbFeN61G1DpZY=
github.com/networkservicemesh/sdk-vpp v0.0.0-20211122163200-210bc45fb1bc h1:PTLk3jta+/59/R3UBHry+0ZBpr6WFDC6r9wdciUtMGo=
github.com/networkservicemesh/sdk-vpp v0.0.0-20211122163200-210bc45fb1bc/go.mod h1:3S1gVEw2n5hzMx7po84h0JdnaKFReYsT5LSjkgKflvQ=
github.com/networkservicemesh/sdk-vpp v0.0.0-20211122201437-8b7a950e7c29 h1:vhuVO+mHd8fYdQoTTeu76qP9ih39B3Bm05HgBfepeUw=
github.com/networkservicemesh/sdk-vpp v0.0.0-20211122201437-8b7a950e7c29/go.mod h1:d46L9dIcbfdIT15ur4lTfEentpdjQTU43+jCTvpkdQU=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Config struct {
ResourcePollTimeout time.Duration `default:"30s" desc:"device plugin polling timeout" split_words:"true"`
DevicePluginPath string `default:"/var/lib/kubelet/device-plugins/" desc:"path to the device plugin directory" split_words:"true"`
PodResourcesPath string `default:"/var/lib/kubelet/pod-resources/" desc:"path to the pod resources directory" split_words:"true"`
DeviceSelectorFile string `default:"" desc:"config file for device name to label matching" split_words:"true"`
SRIOVConfigFile string `default:"" desc:"PCI resources config path" split_words:"true"`
PCIDevicesPath string `default:"/sys/bus/pci/devices" desc:"path to the PCI devices directory" split_words:"true"`
PCIDriversPath string `default:"/sys/bus/pci/drivers" desc:"path to the PCI drivers directory" split_words:"true"`
Expand Down
22 changes: 22 additions & 0 deletions internal/devicecfg/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
interfaces:
- name: eth1
matches:
- labelSelector:
- via: gw0
- name: eth2
matches:
- labelSelector:
- via: gw1
- labelSelector:
- via: gw2
- name: eth3
matches:
- labelSelector:
- via: gw2
- name: eth4
matches:
- labelSelector:
- via: gw2
- labelSelector:
- via: gw3
138 changes: 138 additions & 0 deletions internal/devicecfg/devicecfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright (c) 2021 Nordix Foundation.
//
// 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 devicecfg provides service domain to device config
package devicecfg

import (
"context"
"fmt"
"strings"

"github.com/pkg/errors"

"github.com/networkservicemesh/sdk-sriov/pkg/tools/yamlhelper"
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"
)

const linuxIfMaxLength int = 15

// Config contains list of available service domains
type Config struct {
Interfaces []*Device `yaml:"interfaces"`
}

func (c *Config) String() string {
sb := &strings.Builder{}
_, _ = sb.WriteString("&{")

_, _ = sb.WriteString("Interfaces:[")
var strs []string
for _, device := range c.Interfaces {
strs = append(strs, fmt.Sprintf("%+v", device))
}

_, _ = sb.WriteString(strings.Join(strs, " "))
_, _ = sb.WriteString("]")

_, _ = sb.WriteString("}")
return sb.String()
}

// Device contains an available interface name and related matches
type Device struct {
Name string `yaml:"name"`
Matches []*Selectors `yaml:"matches"`
}

func (iface *Device) String() string {
sb := &strings.Builder{}
_, _ = sb.WriteString("&{")

_, _ = sb.WriteString("Name:")
_, _ = sb.WriteString(iface.Name)

_, _ = sb.WriteString(" Matches:[")
var strs []string
for _, selector := range iface.Matches {
strs = append(strs, fmt.Sprintf("%+v", selector))
}

_, _ = sb.WriteString(strings.Join(strs, " "))
_, _ = sb.WriteString("]")

_, _ = sb.WriteString("}")
return sb.String()
}

// Selectors contains a list of selectors
type Selectors struct {
LabelSelector []*Labels `yaml:"labelSelector"`
}

func (mh *Selectors) String() string {
sb := &strings.Builder{}
_, _ = sb.WriteString("&{")

_, _ = sb.WriteString("LabelSelector[")
var strs []string
for _, labelSel := range mh.LabelSelector {
strs = append(strs, fmt.Sprintf("%+v", labelSel))
}
_, _ = sb.WriteString(strings.Join(strs, " "))
_, _ = sb.WriteString("]")

_, _ = sb.WriteString("}")
return sb.String()
}

// Labels contins the via selector
type Labels struct {
Via string `yaml:"via"`
}

// ReadConfig reads configuration from file
func ReadConfig(ctx context.Context, configFile string) (*Config, error) {
logger := logruslogger.New(ctx)

cfg := &Config{}
if err := yamlhelper.UnmarshalFile(configFile, cfg); err != nil {
return nil, err
}

for _, device := range cfg.Interfaces {
if device.Name == "" {
return nil, errors.Errorf("intrface name must be set")
}
if len(device.Name) >= linuxIfMaxLength {
return nil, errors.Errorf("too long interface name set")
}
for i := range device.Matches {
if len(device.Matches[i].LabelSelector) == 0 {
return nil, errors.Errorf("at least one label selector must be specified")
}
for j := range device.Matches[i].LabelSelector {
if device.Matches[i].LabelSelector[j].Via == "" {
return nil, errors.Errorf("%s unsupported label selector specified", device.Name)
}
}
}
}

logger.WithField("Config", "ReadConfig").Infof("unmarshalled Config: %+v", cfg)

return cfg, nil
}
109 changes: 109 additions & 0 deletions internal/devicecfg/devicecfg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) 2021 Nordix Foundation.
//
// 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 devicecfg_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/networkservicemesh/cmd-forwarder-vpp/internal/devicecfg"
)

const (
configFileName = "config.yml"
ifName1 = "eth1"
ifName2 = "eth2"
ifName3 = "eth3"
ifName4 = "eth4"
via1 = "gw0"
via2 = "gw1"
via3 = "gw2"
via4 = "gw3"
)

func TestReadConfigFile(t *testing.T) {
cfg, err := devicecfg.ReadConfig(context.Background(), configFileName)
require.NoError(t, err)
require.Equal(t, &devicecfg.Config{
Interfaces: []*devicecfg.Device{
{
Name: ifName1,
Matches: []*devicecfg.Selectors{
{
LabelSelector: []*devicecfg.Labels{
{
Via: via1,
},
},
},
},
},
{
Name: ifName2,
Matches: []*devicecfg.Selectors{
{
LabelSelector: []*devicecfg.Labels{
{
Via: via2,
},
},
},
{
LabelSelector: []*devicecfg.Labels{
{
Via: via3,
},
},
},
},
},
{
Name: ifName3,
Matches: []*devicecfg.Selectors{
{
LabelSelector: []*devicecfg.Labels{
{
Via: via3,
},
},
},
},
},
{
Name: ifName4,
Matches: []*devicecfg.Selectors{
{
LabelSelector: []*devicecfg.Labels{
{
Via: via3,
},
},
},
{
LabelSelector: []*devicecfg.Labels{
{
Via: via4,
},
},
},
},
},
},
}, cfg)
}
Loading

0 comments on commit 5007aba

Please sign in to comment.