Skip to content

Commit 2f3559d

Browse files
committed
bridge: Extract some netlink operations code to file
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]>
1 parent b6a0e0b commit 2f3559d

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

pkg/link/opstate.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2024 CNI authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package link
16+
17+
import (
18+
"fmt"
19+
"time"
20+
21+
"github.com/vishvananda/netlink"
22+
)
23+
24+
func WaitForOperStateUp(linkName string) (netlink.Link, error) {
25+
var link netlink.Link
26+
retries := []int{0, 50, 500, 1000, 1000}
27+
for idx, sleep := range retries {
28+
time.Sleep(time.Duration(sleep) * time.Millisecond)
29+
30+
link, err := netlink.LinkByName(linkName)
31+
if err != nil {
32+
return nil, err
33+
}
34+
if link.Attrs().OperState == netlink.OperUp {
35+
break
36+
}
37+
38+
if idx == len(retries)-1 {
39+
return nil, fmt.Errorf("timeout waiting for %q state up", linkName)
40+
}
41+
}
42+
return link, nil
43+
}
44+
45+
func SetUp(linkName string) error {
46+
link, err := netlink.LinkByName(linkName)
47+
if err != nil {
48+
return fmt.Errorf("failed to retrieve link: %v", err)
49+
}
50+
if err = netlink.LinkSetUp(link); err != nil {
51+
return fmt.Errorf("failed to set %q up: %v", linkName, err)
52+
}
53+
return nil
54+
}

plugins/main/bridge/bridge.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"runtime"
2424
"sort"
2525
"syscall"
26-
"time"
2726

2827
"github.com/vishvananda/netlink"
2928

@@ -677,39 +676,18 @@ func cmdAdd(args *skel.CmdArgs) error {
677676
}
678677
}
679678
} else {
679+
// If layer 2 we still need to set the container veth to up
680680
if err := netns.Do(func(_ ns.NetNS) error {
681-
link, err := netlink.LinkByName(args.IfName)
682-
if err != nil {
683-
return fmt.Errorf("failed to retrieve link: %v", err)
684-
}
685-
// If layer 2 we still need to set the container veth to up
686-
if err = netlink.LinkSetUp(link); err != nil {
687-
return fmt.Errorf("failed to set %q up: %v", args.IfName, err)
688-
}
689-
return nil
681+
return link.SetUp(args.IfName)
690682
}); err != nil {
691683
return err
692684
}
693685
}
694686

695-
var hostVeth netlink.Link
696-
697687
// check bridge port state
698-
retries := []int{0, 50, 500, 1000, 1000}
699-
for idx, sleep := range retries {
700-
time.Sleep(time.Duration(sleep) * time.Millisecond)
701-
702-
hostVeth, err = netlink.LinkByName(hostInterface.Name)
703-
if err != nil {
704-
return err
705-
}
706-
if hostVeth.Attrs().OperState == netlink.OperUp {
707-
break
708-
}
709-
710-
if idx == len(retries)-1 {
711-
return fmt.Errorf("bridge port in error state: %s", hostVeth.Attrs().OperState)
712-
}
688+
hostVeth, err := link.WaitForOperStateUp(hostInterface.Name)
689+
if err != nil {
690+
return fmt.Errorf("bridge port in error state %q: %v", hostVeth.Attrs().OperState, err)
713691
}
714692

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

0 commit comments

Comments
 (0)