Skip to content

Commit ad9910e

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 ad9910e

File tree

2 files changed

+61
-27
lines changed

2 files changed

+61
-27
lines changed

pkg/link/opstate.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
var err error
27+
retries := []int{0, 50, 500, 1000, 1000}
28+
for idx, sleep := range retries {
29+
time.Sleep(time.Duration(sleep) * time.Millisecond)
30+
31+
link, err = netlink.LinkByName(linkName)
32+
if err != nil {
33+
return nil, err
34+
}
35+
linkOpState := link.Attrs().OperState
36+
if linkOpState == netlink.OperUp {
37+
break
38+
}
39+
40+
if idx == len(retries)-1 {
41+
return nil, fmt.Errorf("timeout waiting for %q state %q to be up", linkName, linkOpState)
42+
}
43+
}
44+
return link, nil
45+
}
46+
47+
func SetUp(linkName string) error {
48+
link, err := netlink.LinkByName(linkName)
49+
if err != nil {
50+
return fmt.Errorf("failed to retrieve link: %v", err)
51+
}
52+
if err = netlink.LinkSetUp(link); err != nil {
53+
return fmt.Errorf("failed to set %q up: %v", linkName, err)
54+
}
55+
return nil
56+
}

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)