Skip to content

Commit

Permalink
test; Parameters for vpp af interface creation read from config file
Browse files Browse the repository at this point in the history
Signed-off-by: Laszlo Kiraly <[email protected]>
  • Loading branch information
ljkiraly committed Jan 11, 2024
1 parent 5081790 commit 3c987a2
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 24 deletions.
7 changes: 5 additions & 2 deletions internal/tests/suite_setup_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2023 Cisco and/or its affiliates.
//
// Copyright (c) 2024 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -52,6 +54,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/tools/token"

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

func (f *ForwarderTestSuite) SetupSuite() {
Expand Down Expand Up @@ -81,14 +84,14 @@ func (f *ForwarderTestSuite) SetupSuite() {
log.FromContext(f.ctx).Infof("Creating test vpp Server (time since start: %s)", time.Since(starttime))
// ********************************************************************************
f.vppServerConn, f.vppServerRoot, f.vppServerErrCh = f.createVpp(f.ctx, "vpp-server")
_, err = vppinit.LinkToSocket(f.ctx, f.vppServerConn, net.ParseIP(serverIP), vppinit.AfPacket)
_, err = vppinit.LinkToSocket(f.ctx, f.vppServerConn, net.ParseIP(serverIP), vppcfg.Config{}, vppinit.AfPacket)
f.Require().NoError(err)

// ********************************************************************************
log.FromContext(f.ctx).Infof("Creating test vpp Client (time since start: %s)", time.Since(starttime))
// ********************************************************************************
f.vppClientConn, f.vppClientRoot, f.vppClientErrCh = f.createVpp(f.ctx, "vpp-client")
_, err = vppinit.LinkToSocket(f.ctx, f.vppClientConn, net.ParseIP(clientIP), vppinit.AfPacket)
_, err = vppinit.LinkToSocket(f.ctx, f.vppClientConn, net.ParseIP(clientIP), vppcfg.Config{}, vppinit.AfPacket)
f.Require().NoError(err)

// ********************************************************************************
Expand Down
9 changes: 5 additions & 4 deletions internal/vppinit/links.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2021-2022 Nordix Foundation.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// Copyright (c) 2021-2024 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -31,8 +31,9 @@ import (

interfaces "github.com/networkservicemesh/govpp/binapi/interface"
"github.com/networkservicemesh/govpp/binapi/interface_types"

"github.com/networkservicemesh/sdk/pkg/tools/log"

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

func initDevice(ctx context.Context, vppConn api.Connection, tunnelIP net.IP, device string, errChan chan error) {
Expand Down Expand Up @@ -92,7 +93,7 @@ func isTunnelLink(link netlink.Link, tunnelIP net.IP) bool {
}

func setupLinkVpp(ctx context.Context, vppConn api.Connection, link netlink.Link) error {
swIfIndex, err := createAfPacket(ctx, vppConn, link)
swIfIndex, err := createAfPacket(ctx, vppConn, link, vppcfg.Config{})
if err != nil {
return err
}
Expand Down
23 changes: 23 additions & 0 deletions internal/vppinit/vppcfg/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
interfaces:
- type: AF_PACKET_V3
mode: 1 # 1 Ethernet, 2 IP
rxFrameSize: 2048
txFrameSize: 10240
rxFramesPerBlock: 32
txFramesPerBlock: 1024
# flags: Flags
numRxQueues: 1
numTxQueues: 1

- type: AF_PACKET_V2
rxFrameSize: 10240
txFrameSize: 10240
rxFramesPerBlock: 1024
txFramesPerBlock: 1024
# flags: Flags
numRxQueues: 1

- type: AF_XDP
rxqSize: 8192
txqSize: 8192
135 changes: 135 additions & 0 deletions internal/vppinit/vppcfg/vppcfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright (c) 2024 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 vppcfg provides parsing factilty for configuration parameters
// file for vpp init
package vppcfg

import (
"context"
"fmt"
"strconv"
"strings"

"github.com/pkg/errors"

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

// Config contains interfaces
type Config struct {
Interfaces []*Resource `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()
}

// Resource contains configuration parameters for an available interface
type Resource struct {
Type string `yaml:"type,omitempty"`
Mode int `yaml:"mode"`
RxFrameSize uint32 `yaml:"rxFrameSize"`
TxFrameSize uint32 `yaml:"txFrameSize"`
RxFramesPerBlock uint32 `yaml:"rxFramesPerBlock"`
TxFramesPerBlock uint32 `yaml:"txFramesPerBlock"`
NumRxQueues uint16 `yaml:"numRxQueues"`
NumTxQueues uint16 `yaml:"numTxQueues"`
RxqSize uint16 `yaml:"rxqSize"`
TxqSize uint16 `yaml:"txqSize"`
}

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

_, _ = sb.WriteString("Type:")
_, _ = sb.WriteString(res.Type)

_, _ = sb.WriteString("Mode:")
_, _ = sb.WriteString(strconv.Itoa(res.Mode))

_, _ = sb.WriteString("RxFrameSize:")
_, _ = sb.WriteString(strconv.FormatUint(uint64(res.RxFrameSize), 10))

_, _ = sb.WriteString("TxFrameSize:")
_, _ = sb.WriteString(strconv.FormatUint(uint64(res.TxFrameSize), 10))

_, _ = sb.WriteString("RxFramesPerBlock:")
_, _ = sb.WriteString(strconv.FormatUint(uint64(res.RxFramesPerBlock), 10))

_, _ = sb.WriteString("TxFramesPerBlock:")
_, _ = sb.WriteString(strconv.FormatUint(uint64(res.TxFramesPerBlock), 10))

_, _ = sb.WriteString("NumRxQueues:")
_, _ = sb.WriteString(strconv.FormatUint(uint64(res.NumRxQueues), 10))

_, _ = sb.WriteString("NumTxQueues:")
_, _ = sb.WriteString(strconv.FormatUint(uint64(res.NumTxQueues), 10))

_, _ = sb.WriteString("RxqSize:")
_, _ = sb.WriteString(strconv.FormatUint(uint64(res.RxqSize), 10))

_, _ = sb.WriteString("TxqSize:")
_, _ = sb.WriteString(strconv.FormatUint(uint64(res.TxqSize), 10))

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

// 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 {
err := validateResource(device)
if err != nil {
return nil, err
}
}
logger.WithField("Config", "ReadConfig").Infof("unmarshalled Config: %+v", cfg)
return cfg, nil
}

func validateResource(res *Resource) error {
if res == nil {
return nil
}
if res.Type == "" {
return errors.Errorf("resource type must be set")
}
if (res.Type != "AF_PACKET_V3") && (res.Type != "AF_PACKET_V2") && (res.Type != "AF_XDP") {
return errors.Errorf("unsupported resource type")
}
return nil
}
65 changes: 65 additions & 0 deletions internal/vppinit/vppcfg/vppcfg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2024 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 vppcfg_test

import (
"context"
"testing"

"github.com/networkservicemesh/cmd-forwarder-vpp/internal/vppinit/vppcfg"
"github.com/stretchr/testify/require"
)

const (
configFileName = "config.yaml"
type1 = "AF_PACKET_V3"
type2 = "AF_PACKET_V2"
type3 = "AF_XDP"
)

func TestReadConfigFile(t *testing.T) {
cfg, err := vppcfg.ReadConfig(context.Background(), configFileName)
require.NoError(t, err)
require.Equal(t, &vppcfg.Config{
Interfaces: []*vppcfg.Resource{
{
Type: type1,
Mode: 1,
RxFrameSize: 2048,
TxFrameSize: 10240,
RxFramesPerBlock: 32,
TxFramesPerBlock: 1024,
NumRxQueues: 1,
NumTxQueues: 1,
},
{
Type: type2,
RxFrameSize: 10240,
TxFrameSize: 10240,
RxFramesPerBlock: 1024,
TxFramesPerBlock: 1024,
NumRxQueues: 1,
},
{
Type: type3,
Mode: 0,
RxqSize: 8192,
TxqSize: 8192,
},
},
}, cfg)
}
Loading

0 comments on commit 3c987a2

Please sign in to comment.