Skip to content

Commit

Permalink
fix route for vlan
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangzii committed Aug 23, 2024
1 parent 97277a9 commit 01c9731
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/allocator/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (a *Allocator) IPAddrReNew() {
for _, nic := range a.nics {
if nic != nil {
nicKey := getNicKey(nic.Nic)
if nic.isOK() && nic.Nic.VxNet.TunnelType == qcclient.TunnelTypeVlan {
if nic.isOK() && nic.Nic.VxNet.TunnelType == constants.TunnelTypeVlan {
brName := constants.GetHostNicBridgeName(int(nic.Nic.RouteTableNum))
// renew ip lease
err := networkutils.UpdateLinkIPAddrAndLease(nic.Nic)
Expand Down
5 changes: 5 additions & 0 deletions pkg/constants/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ const (
EventDelete = "delete"

MetricsDummyNamespaceForSubnet = "Dummy-ns-for-unmapped-subnets"

TunnelTypeVlan = "vlan"

RouteExistsError = "file exists"
RouteNotExistsError = "no such process"
)

func GetHostNicBridgeName(routeTableNum int) string {
Expand Down
21 changes: 17 additions & 4 deletions pkg/networkutils/networkutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"os"
"os/exec"
"strings"
"time"

"github.com/containernetworking/plugins/pkg/ns"
Expand Down Expand Up @@ -222,7 +223,7 @@ func (n NetworkUtils) setupBridgeNetwork(link netlink.Link, brName, tunnelType s
if err != nil {
return fmt.Errorf("failed to set link %s up: %v", la.Name, err)
}
if tunnelType == qcclient.TunnelTypeVlan {
if tunnelType == constants.TunnelTypeVlan {
// get an ip addr and add to br
// 1.get an ip addr from dhcp server
bootConf, err := getIPAddrFromDHCPServer(brName)
Expand Down Expand Up @@ -284,9 +285,21 @@ func (n NetworkUtils) setupRouteTable(nic *rpc.HostNic) error {
},
}

for _, r := range routes {
if err := netlink.RouteAdd(&r); err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to add route %v: %v", r, err)
// if tunnel type is vlan, clear route to repair hostnic
if nic.VxNet.TunnelType == constants.TunnelTypeVlan {
for _, r := range routes {
// netlink.RouteDel return error if route not exists: no such process
if err := netlink.RouteDel(&r); err != nil && !strings.Contains(err.Error(), constants.RouteNotExistsError) {
return fmt.Errorf("failed to del route %v: %v", r, err)
}
}

} else {
for _, r := range routes {
// netlink.RouteAdd return error if route already exists: file exists ; shouldn't use os.IsExist here
if err := netlink.RouteAdd(&r); err != nil && !strings.Contains(err.Error(), constants.RouteExistsError) {
return fmt.Errorf("failed to add route %v: %v", r, err)
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/qcclient/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const (
reservedVIPCount = 12
reservedVIPCountForVlan int64 = 7 //reserve 1/7 ip for hostnic br

TunnelTypeVlan = "vlan"
)

var (
Expand Down Expand Up @@ -478,7 +477,7 @@ func (q *qingcloudAPIWrapper) getVxNets(ids []string, public bool) ([]*rpc.VxNet
if qcVxNet.TunnelType != nil {
vxnetItem.TunnelType = *qcVxNet.TunnelType
}
if vxnetItem.TunnelType == TunnelTypeVlan {
if vxnetItem.TunnelType == constants.TunnelTypeVlan {
// parse ip_network to get mask; if mask is 24, reserve more ip than specifc in config
_, ipNet, err := net.ParseCIDR(vxnetItem.Network)
if err != nil {
Expand Down

0 comments on commit 01c9731

Please sign in to comment.