Skip to content

Commit

Permalink
Ignore default PVID
Browse files Browse the repository at this point in the history
Related issue: harvester/harvester#1669
The command to configure PVID is different from other VIDs. Assume we
want to configure PVID as 1 and add VID 10 for harvester-mgmt, the commands are as followings.
- Configure PVID 1
  `bridge vlan add vid 1 dev harvester-mgmt pvid untagged master`
- Add VID 10
  `bridge vlan add vid 10 dev harvester-mgmt master`
The default PVID is 1, so we ignore it.
  • Loading branch information
yaocw2020 authored and guangbochen committed Dec 14, 2021
1 parent c3b0e85 commit 1868342
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
11 changes: 11 additions & 0 deletions pkg/network/iface/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (

tableFilter = "filter"
chainForward = "FORWARD"

defaultPVID = uint16(1)
)

type Link struct {
Expand Down Expand Up @@ -79,6 +81,11 @@ func GetLinkByIndex(index int) (*Link, error) {
// AddBridgeVlan adds a new vlan filter entry
// Equivalent to: `bridge vlan add dev DEV vid VID master`
func (l *Link) AddBridgeVlan(vid uint16) error {
// The command to configure PVID is not `bridge vlan add dev DEV vid VID master`
if vid == defaultPVID {
return nil
}

if err := netlink.BridgeVlanAdd(l.link, vid, false, false, false, true); err != nil {
return fmt.Errorf("add iface vlan failed, error: %v, link: %s, vid: %d", err, l.Name(), vid)
}
Expand All @@ -89,6 +96,10 @@ func (l *Link) AddBridgeVlan(vid uint16) error {
// DelBridgeVlan adds a new vlan filter entry
// Equivalent to: `bridge vlan del dev DEV vid VID master`
func (l *Link) DelBridgeVlan(vid uint16) error {
if vid == defaultPVID {
return nil
}

if err := netlink.BridgeVlanDel(l.link, vid, false, false, false, true); err != nil {
return fmt.Errorf("delete iface vlan failed, error: %v, link: %s, vid: %d", err, l.Name(), vid)
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/network/iface/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ func EnsureRouteViaGateway(cidr string) error {
Gw: gateway,
Protocol: netlink.FAMILY_V4,
}
if err := netlink.RouteAdd(route); err != nil {
if err := netlink.RouteAdd(route); err != nil && err != syscall.EEXIST {
return err
} else if err == nil {
klog.Infof("add route: %+v", route)
}
klog.Infof("add route: %+v", route)

return nil
}
Expand All @@ -89,19 +90,21 @@ func DeleteRouteViaGateway(cidr string) error {
if err != nil {
return err
}
gateway, _, err := getGateway()
gateway, linkIndex, err := getGateway()
if err != nil {
return err
}

route := &netlink.Route{
Dst: network,
Gw: gateway,
Dst: network,
Gw: gateway,
LinkIndex: linkIndex,
}
if err := netlink.RouteDel(route); err != nil && !errors.Is(err, syscall.ESRCH) {
return err
} else if err == nil {
klog.Infof("delete route: %+v", route)
}
klog.Infof("delete route: %+v", err)

return nil
}
7 changes: 0 additions & 7 deletions pkg/network/vlan/vlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Vlan struct {

const (
BridgeName = "harvester-br0"
PVID = 1
)

func (v *Vlan) Type() string {
Expand Down Expand Up @@ -179,9 +178,6 @@ func (v *Vlan) AddLocalArea(id int, cidr string) error {
if v.nic == nil {
return fmt.Errorf("physical nic vlan network")
}
if id == PVID {
return nil
}

if err := v.nic.AddBridgeVlan(uint16(id)); err != nil {
return fmt.Errorf("add bridge vlan %d failed, error: %w", id, err)
Expand All @@ -206,9 +202,6 @@ func (v *Vlan) RemoveLocalArea(id int, cidr string) error {
if v.nic == nil {
return fmt.Errorf("physical nic vlan network")
}
if id == PVID {
return nil
}

if err := v.nic.DelBridgeVlan(uint16(id)); err != nil {
return fmt.Errorf("remove bridge vlan %d failed, error: %w", id, err)
Expand Down

0 comments on commit 1868342

Please sign in to comment.