generated from networkservicemesh/cmd-template
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test; Parameters for vpp af interface creation read from config file
Signed-off-by: Laszlo Kiraly <[email protected]>
- Loading branch information
Showing
7 changed files
with
288 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.