Skip to content

Commit

Permalink
Initial version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jing Zhang committed May 16, 2023
1 parent 77c66ac commit 880ea4b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/configuration-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ An SR-IOV CNI config with each field filled out looks like:
"max_tx_rate": 200,
"spoofchk": "off",
"trust": "on",
"link_state": "enable"
"link_state": "enable",
"vlan_trunk": "10,12,20-30,1100"
}
```

Expand Down
9 changes: 9 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ func LoadConf(bytes []byte) (*sriovtypes.NetConf, error) {
return nil, fmt.Errorf("LoadConf(): invalid link_state value: %s", n.LinkState)
}

if n.VlanTrunk != "" {
if err := utils.ValidateVlanTrunkValue(n.VlanTrunk); err != nil {
return nil, fmt.Errorf("LoadConf(): invalid vlan_trunk value: %s", n.VlanTrunk)
}
if n.Vlan != nil && *n.Vlan > 0 {
return nil, fmt.Errorf("LoadConf(): vlan and vlan_trunk fields can not co-exist")
}
}

return n, nil
}

Expand Down
37 changes: 36 additions & 1 deletion pkg/sriov/sriov.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sriov
import (
"fmt"
"net"
"plugin"
"time"

"github.com/containernetworking/plugins/pkg/ns"
Expand Down Expand Up @@ -297,6 +298,23 @@ func (s *sriovManager) ApplyVFConfig(conf *sriovtypes.NetConf) error {
}
}

// 7. Set vlan trunking
if conf.VlanTrunk != "" {
trunkPlugin, err := plugin.Open("/opt/cni/bin/sriov-trunk.so")
if err != nil {
return fmt.Errorf("failed to load sriov-trunk plugin sriov-trunk.so: %v", err)
}

applyConfig, err := trunkPlugin.Lookup("ApplyConfig")
if err != nil {
return fmt.Errorf("failed to load sriov-trunk plugin symbol ApplyConfig: %v", err)
}

if err = applyConfig.(func(*sriovtypes.NetConf) error)(conf); err != nil {
return fmt.Errorf("failed to invoke sriov-trunk plugin function ApplyConfig(): %v", err)
}
}

return nil
}

Expand All @@ -323,7 +341,7 @@ func (s *sriovManager) ResetVFConfig(conf *sriovtypes.NetConf) error {
}

// Restore VLAN
if conf.Vlan != nil {
if conf.Vlan != nil && conf.VlanTrunk == "" {
if conf.VlanQoS != nil {
if err = s.nLink.LinkSetVfVlanQos(pfLink, conf.VFID, conf.OrigVfState.Vlan, conf.OrigVfState.VlanQoS); err != nil {
return fmt.Errorf("failed to restore vf %d vlan: %v", conf.VFID, err)
Expand Down Expand Up @@ -389,5 +407,22 @@ func (s *sriovManager) ResetVFConfig(conf *sriovtypes.NetConf) error {
}
}

// Restore VLAN Trunk
if conf.VlanTrunk != "" {
trunkPlugin, err := plugin.Open("/opt/cni/bin/sriov-trunk.so")
if err != nil {
return fmt.Errorf("failed to load sriov-trunk plugin sriov-trunk.so: %v", err)
}

removeConfig, err := trunkPlugin.Lookup("RemoveConfig")
if err != nil {
return fmt.Errorf("failed to load sriov-trunk plugin symbol RemoveConfig: %v", err)
}

if err = removeConfig.(func(*sriovtypes.NetConf) error)(conf); err != nil {
return fmt.Errorf("failed to invoke sriov-trunk plugin function RemoveConfig(): %v", err)
}
}

return nil
}
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type NetConf struct {
SpoofChk string `json:"spoofchk,omitempty"` // on|off
Trust string `json:"trust,omitempty"` // on|off
LinkState string `json:"link_state,omitempty"` // auto|enable|disable
VlanTrunk string `json:"vlan_trunk,omitempty"` // vlan trunking
RuntimeConfig struct {
Mac string `json:"mac,omitempty"`
} `json:"runtimeConfig,omitempty"`
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -284,6 +285,15 @@ func CleanCachedNetConf(cRefPath string) error {
return nil
}

// ValidateVlanTrunkValue validates vlan trunking input
func ValidateVlanTrunkValue(vlanTrunk string) error {
validTrunkValue := regexp.MustCompile("^[0-9]+([,\\-][0-9]+)*$")
if !validTrunkValue.MatchString(vlanTrunk) {
return fmt.Errorf("Invalid vlan_trunk value")
}
return nil
}

// IsValidMACAddress checks if net.HardwareAddr is a valid MAC address.
func IsValidMACAddress(addr net.HardwareAddr) bool {
invalidMACAddresses := [][]byte{
Expand Down

0 comments on commit 880ea4b

Please # to comment.