Skip to content

Commit

Permalink
bridge: refresh host-veth mac after port add
Browse files Browse the repository at this point in the history
Sometimes the mac address changes after adding the port to the bridge.

Fixes #805

Signed-off-by: Casey Callendrello <[email protected]>
  • Loading branch information
squeed committed Jan 16, 2023
1 parent ac86731 commit f551f4a
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions plugins/main/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func loadNetConf(bytes []byte, envArgs string) (*NetConf, string, error) {

// calcGateways processes the results from the IPAM plugin and does the
// following for each IP family:
// - Calculates and compiles a list of gateway addresses
// - Adds a default route if needed
// - Calculates and compiles a list of gateway addresses
// - Adds a default route if needed
func calcGateways(result *current.Result, n *NetConf) (*gwInfo, *gwInfo, error) {

gwsV4 := &gwInfo{}
Expand Down Expand Up @@ -358,7 +358,6 @@ func setupVeth(netns ns.NetNS, br *netlink.Bridge, ifName string, mtu int, hairp
if err != nil {
return nil, nil, fmt.Errorf("failed to lookup %q: %v", hostIface.Name, err)
}
hostIface.Mac = hostVeth.Attrs().HardwareAddr.String()

// connect host veth end to the bridge
if err := netlink.LinkSetMaster(hostVeth, br); err != nil {
Expand All @@ -377,6 +376,26 @@ func setupVeth(netns ns.NetNS, br *netlink.Bridge, ifName string, mtu int, hairp
}
}

// wait for bridge to be up
retries := []int{0, 50, 500, 1000, 1000}
for idx, sleep := range retries {
time.Sleep(time.Duration(sleep) * time.Millisecond)

hostVeth, err := netlink.LinkByName(hostIface.Name)
if err != nil {
return nil, nil, err
}
if hostVeth.Attrs().OperState == netlink.OperUp {
break
}

if idx == len(retries)-1 {
return nil, nil, fmt.Errorf("bridge port in error state: %s", hostVeth.Attrs().OperState)
}
}

hostIface.Mac = hostVeth.Attrs().HardwareAddr.String()

return hostIface, contIface, nil
}

Expand Down Expand Up @@ -517,24 +536,6 @@ func cmdAdd(args *skel.CmdArgs) error {
return err
}

// check bridge port state
retries := []int{0, 50, 500, 1000, 1000}
for idx, sleep := range retries {
time.Sleep(time.Duration(sleep) * time.Millisecond)

hostVeth, err := netlink.LinkByName(hostInterface.Name)
if err != nil {
return err
}
if hostVeth.Attrs().OperState == netlink.OperUp {
break
}

if idx == len(retries)-1 {
return fmt.Errorf("bridge port in error state: %s", hostVeth.Attrs().OperState)
}
}

if n.IsGW {
var firstV4Addr net.IP
var vlanInterface *current.Interface
Expand Down

0 comments on commit f551f4a

Please sign in to comment.