Skip to content

Commit

Permalink
Merge pull request #44802 from akerouanton/libnetwork-sysfs-cleanup
Browse files Browse the repository at this point in the history
libnetwork: Clean up sysfs-based operations
  • Loading branch information
neersighted committed Jan 11, 2023
2 parents a0572a4 + ef161d4 commit ffb2c1f
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 76 deletions.
26 changes: 1 addition & 25 deletions libnetwork/drivers/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"sync"
"syscall"

"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi"
Expand Down Expand Up @@ -891,32 +889,10 @@ func addToBridge(nlh *netlink.Handle, ifaceName, bridgeName string) error {

func setHairpinMode(nlh *netlink.Handle, link netlink.Link, enable bool) error {
err := nlh.LinkSetHairpin(link, enable)
if err != nil && err != syscall.EINVAL {
// If error is not EINVAL something else went wrong, bail out right away
if err != nil {
return fmt.Errorf("unable to set hairpin mode on %s via netlink: %v",
link.Attrs().Name, err)
}

// Hairpin mode successfully set up
if err == nil {
return nil
}

// The netlink method failed with EINVAL which is probably because of an older
// kernel. Try one more time via the sysfs method.
path := filepath.Join("/sys/class/net", link.Attrs().Name, "brport/hairpin_mode")

var val []byte
if enable {
val = []byte{'1', '\n'}
} else {
val = []byte{'0', '\n'}
}

if err := os.WriteFile(path, val, 0644); err != nil {
return fmt.Errorf("unable to set hairpin mode on %s via sysfs: %v", link.Attrs().Name, err)
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions libnetwork/drivers/overlay/ov_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ func setDefaultVlan() {
}

brName := os.Args[2]
// IFLA_BR_VLAN_DEFAULT_PVID was added in Linux v4.4 (see torvalds/linux@0f963b7), so we can't use netlink for
// setting this until Docker drops support for CentOS/RHEL 7 (kernel 3.10, eol date: 2024-06-30).
path := filepath.Join("/sys/class/net", brName, "bridge/default_pvid")
data := []byte{'0', '\n'}

Expand Down
25 changes: 0 additions & 25 deletions libnetwork/osl/interface_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package osl
import (
"fmt"
"net"
"regexp"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -418,30 +417,6 @@ func setInterfaceRoutes(nlh *netlink.Handle, iface netlink.Link, i *nwIface) err
return nil
}

// In older kernels (like the one in Centos 6.6 distro) sysctl does not have netns support. Therefore
// we cannot gather the statistics from /sys/class/net/<dev>/statistics/<counter> files. Per-netns stats
// are naturally found in /proc/net/dev in kernels which support netns (ifconfig relies on that).
const (
base = "[ ]*%s:([ ]+[0-9]+){16}"
)

func scanInterfaceStats(data, ifName string, i *types.InterfaceStatistics) error {
var (
bktStr string
bkt uint64
)

regex := fmt.Sprintf(base, ifName)
re := regexp.MustCompile(regex)
line := re.FindString(data)

_, err := fmt.Sscanf(line, "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
&bktStr, &i.RxBytes, &i.RxPackets, &i.RxErrors, &i.RxDropped, &bkt, &bkt, &bkt,
&bkt, &i.TxBytes, &i.TxPackets, &i.TxErrors, &i.TxDropped, &bkt, &bkt, &bkt, &bkt)

return err
}

func checkRouteConflict(nlh *netlink.Handle, address *net.IPNet, family int) error {
routes, err := nlh.RouteList(nil, family)
if err != nil {
Expand Down
26 changes: 0 additions & 26 deletions libnetwork/osl/sandbox_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,32 +158,6 @@ func verifyCleanup(t *testing.T, s Sandbox, wait bool) {
}
}

func TestScanStatistics(t *testing.T) {
data :=
"Inter-| Receive | Transmit\n" +
" face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n" +
" eth0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" wlan0: 7787685 11141 0 0 0 0 0 0 1681390 7220 0 0 0 0 0 0\n" +
" lo: 783782 1853 0 0 0 0 0 0 783782 1853 0 0 0 0 0 0\n" +
"lxcbr0: 0 0 0 0 0 0 0 0 9006 61 0 0 0 0 0 0\n"

i := &types.InterfaceStatistics{}

if err := scanInterfaceStats(data, "wlan0", i); err != nil {
t.Fatal(err)
}
if i.TxBytes != 1681390 || i.TxPackets != 7220 || i.RxBytes != 7787685 || i.RxPackets != 11141 {
t.Fatalf("Error scanning the statistics")
}

if err := scanInterfaceStats(data, "lxcbr0", i); err != nil {
t.Fatal(err)
}
if i.TxBytes != 9006 || i.TxPackets != 61 || i.RxBytes != 0 || i.RxPackets != 0 {
t.Fatalf("Error scanning the statistics")
}
}

func TestDisableIPv6DAD(t *testing.T) {
defer testutils.SetupTestOSContext(t)()

Expand Down

0 comments on commit ffb2c1f

Please sign in to comment.