Skip to content

Commit

Permalink
bridge: Extract some netlink operations code to file
Browse files Browse the repository at this point in the history
This is a small refactoring around netlink operations
logic.
Setting link state up and waiting for link oper state
code is moved to link package.

Signed-off-by: Or Mergi <[email protected]>
  • Loading branch information
ormergi committed Jan 11, 2024
1 parent b6a0e0b commit 2f3559d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
54 changes: 54 additions & 0 deletions pkg/link/opstate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2024 CNI authors
//
// 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 link

import (
"fmt"
"time"

"github.com/vishvananda/netlink"
)

func WaitForOperStateUp(linkName string) (netlink.Link, error) {
var link netlink.Link
retries := []int{0, 50, 500, 1000, 1000}
for idx, sleep := range retries {
time.Sleep(time.Duration(sleep) * time.Millisecond)

link, err := netlink.LinkByName(linkName)
if err != nil {
return nil, err
}
if link.Attrs().OperState == netlink.OperUp {
break
}

if idx == len(retries)-1 {
return nil, fmt.Errorf("timeout waiting for %q state up", linkName)
}
}
return link, nil
}

func SetUp(linkName string) error {
link, err := netlink.LinkByName(linkName)
if err != nil {
return fmt.Errorf("failed to retrieve link: %v", err)
}
if err = netlink.LinkSetUp(link); err != nil {
return fmt.Errorf("failed to set %q up: %v", linkName, err)
}
return nil
}
32 changes: 5 additions & 27 deletions plugins/main/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"runtime"
"sort"
"syscall"
"time"

"github.com/vishvananda/netlink"

Expand Down Expand Up @@ -677,39 +676,18 @@ func cmdAdd(args *skel.CmdArgs) error {
}
}
} else {
// If layer 2 we still need to set the container veth to up
if err := netns.Do(func(_ ns.NetNS) error {
link, err := netlink.LinkByName(args.IfName)
if err != nil {
return fmt.Errorf("failed to retrieve link: %v", err)
}
// If layer 2 we still need to set the container veth to up
if err = netlink.LinkSetUp(link); err != nil {
return fmt.Errorf("failed to set %q up: %v", args.IfName, err)
}
return nil
return link.SetUp(args.IfName)
}); err != nil {
return err
}
}

var hostVeth netlink.Link

// 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)
}
hostVeth, err := link.WaitForOperStateUp(hostInterface.Name)
if err != nil {
return fmt.Errorf("bridge port in error state %q: %v", hostVeth.Attrs().OperState, err)
}

// In certain circumstances, the host-side of the veth may change addrs
Expand Down

0 comments on commit 2f3559d

Please sign in to comment.