Merge pull request #44802 from akerouanton/libnetwork-sysfs-cleanup

libnetwork: Clean up sysfs-based operations
This commit is contained in:
Bjorn Neergaard 2023-01-11 10:04:51 -07:00 committed by GitHub
commit ffb2c1fb4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 3 additions and 76 deletions

View file

@ -9,10 +9,8 @@ import (
"net" "net"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strconv" "strconv"
"sync" "sync"
"syscall"
"github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi" "github.com/docker/docker/libnetwork/discoverapi"
@ -891,32 +889,10 @@ func addToBridge(nlh *netlink.Handle, ifaceName, bridgeName string) error {
func setHairpinMode(nlh *netlink.Handle, link netlink.Link, enable bool) error { func setHairpinMode(nlh *netlink.Handle, link netlink.Link, enable bool) error {
err := nlh.LinkSetHairpin(link, enable) err := nlh.LinkSetHairpin(link, enable)
if err != nil && err != syscall.EINVAL { if err != nil {
// If error is not EINVAL something else went wrong, bail out right away
return fmt.Errorf("unable to set hairpin mode on %s via netlink: %v", return fmt.Errorf("unable to set hairpin mode on %s via netlink: %v",
link.Attrs().Name, err) 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 return nil
} }

View file

@ -124,6 +124,8 @@ func setDefaultVlan() {
} }
brName := os.Args[2] 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") path := filepath.Join("/sys/class/net", brName, "bridge/default_pvid")
data := []byte{'0', '\n'} data := []byte{'0', '\n'}

View file

@ -3,7 +3,6 @@ package osl
import ( import (
"fmt" "fmt"
"net" "net"
"regexp"
"sync" "sync"
"syscall" "syscall"
"time" "time"
@ -418,30 +417,6 @@ func setInterfaceRoutes(nlh *netlink.Handle, iface netlink.Link, i *nwIface) err
return nil 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 { func checkRouteConflict(nlh *netlink.Handle, address *net.IPNet, family int) error {
routes, err := nlh.RouteList(nil, family) routes, err := nlh.RouteList(nil, family)
if err != nil { if err != nil {

View file

@ -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) { func TestDisableIPv6DAD(t *testing.T) {
defer testutils.SetupTestOSContext(t)() defer testutils.SetupTestOSContext(t)()