From 3097d938cabf6c54dfb23d0e719abe0770981268 Mon Sep 17 00:00:00 2001 From: Martin Maartensson Date: Tue, 15 Aug 2023 00:02:30 +0200 Subject: [PATCH] change all ioutil calls to os --- bridge_linux_test.go | 4 +- ipset_linux_test.go | 89 +++++++++++++++++++++++------ link_linux.go | 130 ++++++++++++++++++++++++++++++++----------- netlink_test.go | 4 +- qdisc_linux.go | 7 ++- rdma_link_test.go | 5 +- 6 files changed, 182 insertions(+), 57 deletions(-) diff --git a/bridge_linux_test.go b/bridge_linux_test.go index 82704e7d..3016aa55 100644 --- a/bridge_linux_test.go +++ b/bridge_linux_test.go @@ -2,7 +2,7 @@ package netlink import ( "fmt" - "io/ioutil" + "os" "testing" ) @@ -19,7 +19,7 @@ func TestBridgeVlan(t *testing.T) { if err := LinkAdd(bridge); err != nil { t.Fatal(err) } - if err := ioutil.WriteFile(fmt.Sprintf("/sys/devices/virtual/net/%s/bridge/vlan_filtering", bridgeName), []byte("1"), 0644); err != nil { + if err := os.WriteFile(fmt.Sprintf("/sys/devices/virtual/net/%s/bridge/vlan_filtering", bridgeName), []byte("1"), 0644); err != nil { t.Fatal(err) } if vlanMap, err := BridgeVlanList(); err != nil { diff --git a/ipset_linux_test.go b/ipset_linux_test.go index 298c3a3a..25704913 100644 --- a/ipset_linux_test.go +++ b/ipset_linux_test.go @@ -2,16 +2,17 @@ package netlink import ( "bytes" - "io/ioutil" "net" + "os" "testing" - "github.com/vishvananda/netlink/nl" "golang.org/x/sys/unix" + + "github.com/vishvananda/netlink/nl" ) func TestParseIpsetProtocolResult(t *testing.T) { - msgBytes, err := ioutil.ReadFile("testdata/ipset_protocol_result") + msgBytes, err := os.ReadFile("testdata/ipset_protocol_result") if err != nil { t.Fatalf("reading test fixture failed: %v", err) } @@ -23,7 +24,7 @@ func TestParseIpsetProtocolResult(t *testing.T) { } func TestParseIpsetListResult(t *testing.T) { - msgBytes, err := ioutil.ReadFile("testdata/ipset_list_result") + msgBytes, err := os.ReadFile("testdata/ipset_list_result") if err != nil { t.Fatalf("reading test fixture failed: %v", err) } @@ -76,14 +77,22 @@ func TestParseIpsetListResult(t *testing.T) { } expectedMAC := net.HardwareAddr{0xde, 0xad, 0x0, 0x0, 0xbe, 0xef} if !bytes.Equal(ent.MAC, expectedMAC) { - t.Errorf("expected MAC for first entry to be %s, got %s", expectedMAC.String(), ent.MAC.String()) + t.Errorf( + "expected MAC for first entry to be %s, got %s", + expectedMAC.String(), + ent.MAC.String(), + ) } // second entry ent = msg.Entries[1] expectedMAC = net.HardwareAddr{0x1, 0x2, 0x3, 0x0, 0x1, 0x2} if !bytes.Equal(ent.MAC, expectedMAC) { - t.Errorf("expected MAC for second entry to be %s, got %s", expectedMAC.String(), ent.MAC.String()) + t.Errorf( + "expected MAC for second entry to be %s, got %s", + expectedMAC.String(), + ent.MAC.String(), + ) } } @@ -496,58 +505,98 @@ func TestIpsetCreateListAddDelDestroyWithTestCases(t *testing.T) { if tC.entry.IP != nil { if !tC.entry.IP.Equal(result.Entries[0].IP) { - t.Fatalf("expected entry to be '%v', got '%v'", tC.entry.IP, result.Entries[0].IP) + t.Fatalf( + "expected entry to be '%v', got '%v'", + tC.entry.IP, + result.Entries[0].IP, + ) } } if tC.entry.CIDR > 0 { if result.Entries[0].CIDR != tC.entry.CIDR { - t.Fatalf("expected cidr to be '%d', got '%d'", tC.entry.CIDR, result.Entries[0].CIDR) + t.Fatalf( + "expected cidr to be '%d', got '%d'", + tC.entry.CIDR, + result.Entries[0].CIDR, + ) } } if tC.entry.IP2 != nil { if !tC.entry.IP2.Equal(result.Entries[0].IP2) { - t.Fatalf("expected entry.ip2 to be '%v', got '%v'", tC.entry.IP2, result.Entries[0].IP2) + t.Fatalf( + "expected entry.ip2 to be '%v', got '%v'", + tC.entry.IP2, + result.Entries[0].IP2, + ) } } if tC.entry.CIDR2 > 0 { if result.Entries[0].CIDR2 != tC.entry.CIDR2 { - t.Fatalf("expected cidr2 to be '%d', got '%d'", tC.entry.CIDR2, result.Entries[0].CIDR2) + t.Fatalf( + "expected cidr2 to be '%d', got '%d'", + tC.entry.CIDR2, + result.Entries[0].CIDR2, + ) } } if tC.entry.Port != nil { if *result.Entries[0].Protocol != *tC.entry.Protocol { - t.Fatalf("expected protocol to be '%d', got '%d'", *tC.entry.Protocol, *result.Entries[0].Protocol) + t.Fatalf( + "expected protocol to be '%d', got '%d'", + *tC.entry.Protocol, + *result.Entries[0].Protocol, + ) } if *result.Entries[0].Port != *tC.entry.Port { - t.Fatalf("expected port to be '%d', got '%d'", *tC.entry.Port, *result.Entries[0].Port) + t.Fatalf( + "expected port to be '%d', got '%d'", + *tC.entry.Port, + *result.Entries[0].Port, + ) } } if tC.entry.MAC != nil { if result.Entries[0].MAC.String() != tC.entry.MAC.String() { - t.Fatalf("expected mac to be '%v', got '%v'", tC.entry.MAC, result.Entries[0].MAC) + t.Fatalf( + "expected mac to be '%v', got '%v'", + tC.entry.MAC, + result.Entries[0].MAC, + ) } } if tC.entry.IFace != "" { if result.Entries[0].IFace != tC.entry.IFace { - t.Fatalf("expected iface to be '%v', got '%v'", tC.entry.IFace, result.Entries[0].IFace) + t.Fatalf( + "expected iface to be '%v', got '%v'", + tC.entry.IFace, + result.Entries[0].IFace, + ) } } if tC.entry.Mark != nil { if *result.Entries[0].Mark != *tC.entry.Mark { - t.Fatalf("expected mark to be '%v', got '%v'", *tC.entry.Mark, *result.Entries[0].Mark) + t.Fatalf( + "expected mark to be '%v', got '%v'", + *tC.entry.Mark, + *result.Entries[0].Mark, + ) } } if result.Entries[0].Comment != tC.entry.Comment { // This is only supported in the kernel module from revision 2 or 4, so comments may be ignored. - t.Logf("expected comment to be '%s', got '%s'", tC.entry.Comment, result.Entries[0].Comment) + t.Logf( + "expected comment to be '%s', got '%s'", + tC.entry.Comment, + result.Entries[0].Comment, + ) } err = IpsetDel(tC.setname, tC.entry) @@ -630,7 +679,13 @@ func TestIpsetBitmapCreateListWithTestCases(t *testing.T) { if tC.typename == "bitmap:port" { if result.PortFrom != tC.options.PortFrom || result.PortTo != tC.options.PortTo { - t.Fatalf("expected port range %d-%d, got %d-%d", tC.options.PortFrom, tC.options.PortTo, result.PortFrom, result.PortTo) + t.Fatalf( + "expected port range %d-%d, got %d-%d", + tC.options.PortFrom, + tC.options.PortTo, + result.PortFrom, + result.PortTo, + ) } } else if tC.typename == "bitmap:ip" { if result.IPFrom == nil || result.IPTo == nil || result.IPFrom.Equal(tC.options.IPFrom) || result.IPTo.Equal(tC.options.IPTo) { diff --git a/link_linux.go b/link_linux.go index a2c6b472..12c42e98 100644 --- a/link_linux.go +++ b/link_linux.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/binary" "fmt" - "io/ioutil" "net" "os" "strconv" @@ -12,9 +11,10 @@ import ( "syscall" "unsafe" - "github.com/vishvananda/netlink/nl" "github.com/vishvananda/netns" "golang.org/x/sys/unix" + + "github.com/vishvananda/netlink/nl" ) const ( @@ -959,28 +959,28 @@ func LinkSetXdpFdWithFlags(link Link, fd, flags int) error { // LinkSetGSOMaxSegs sets the GSO maximum segment count of the link device. // Equivalent to: `ip link set $link gso_max_segs $maxSegs` func LinkSetGSOMaxSegs(link Link, maxSegs int) error { - return pkgHandle.LinkSetGSOMaxSegs(link, maxSegs) + return pkgHandle.LinkSetGSOMaxSegs(link, maxSegs) } // LinkSetGSOMaxSegs sets the GSO maximum segment count of the link device. // Equivalent to: `ip link set $link gso_max_segs $maxSegs` func (h *Handle) LinkSetGSOMaxSegs(link Link, maxSize int) error { - base := link.Attrs() - h.ensureIndex(base) - req := h.newNetlinkRequest(unix.RTM_SETLINK, unix.NLM_F_ACK) + base := link.Attrs() + h.ensureIndex(base) + req := h.newNetlinkRequest(unix.RTM_SETLINK, unix.NLM_F_ACK) - msg := nl.NewIfInfomsg(unix.AF_UNSPEC) - msg.Index = int32(base.Index) - req.AddData(msg) + msg := nl.NewIfInfomsg(unix.AF_UNSPEC) + msg.Index = int32(base.Index) + req.AddData(msg) - b := make([]byte, 4) - native.PutUint32(b, uint32(maxSize)) + b := make([]byte, 4) + native.PutUint32(b, uint32(maxSize)) - data := nl.NewRtAttr(unix.IFLA_GSO_MAX_SEGS, b) - req.AddData(data) + data := nl.NewRtAttr(unix.IFLA_GSO_MAX_SEGS, b) + req.AddData(data) - _, err := req.Execute(unix.NETLINK_ROUTE, 0) - return err + _, err := req.Execute(unix.NETLINK_ROUTE, 0) + return err } // LinkSetGSOMaxSize sets the IPv6 GSO maximum size of the link device. @@ -1347,7 +1347,12 @@ func (h *Handle) linkModify(link Link, flags int) error { return err } - _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&localReq))) + _, _, errno := unix.Syscall( + unix.SYS_IOCTL, + uintptr(fd), + uintptr(unix.TUNSETIFF), + uintptr(unsafe.Pointer(&localReq)), + ) if errno != 0 { // close the new fd unix.Close(fd) @@ -1356,13 +1361,23 @@ func (h *Handle) linkModify(link Link, flags int) error { return fmt.Errorf("Tuntap IOCTL TUNSETIFF failed [%d], errno %v", i, errno) } - _, _, errno = syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.TUNSETOWNER, uintptr(tuntap.Owner)) + _, _, errno = syscall.Syscall( + syscall.SYS_IOCTL, + uintptr(fd), + syscall.TUNSETOWNER, + uintptr(tuntap.Owner), + ) if errno != 0 { cleanupFds(fds) return fmt.Errorf("Tuntap IOCTL TUNSETOWNER failed [%d], errno %v", i, errno) } - _, _, errno = syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.TUNSETGROUP, uintptr(tuntap.Group)) + _, _, errno = syscall.Syscall( + syscall.SYS_IOCTL, + uintptr(fd), + syscall.TUNSETGROUP, + uintptr(tuntap.Group), + ) if errno != 0 { cleanupFds(fds) return fmt.Errorf("Tuntap IOCTL TUNSETGROUP failed [%d], errno %v", i, errno) @@ -2181,7 +2196,7 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) { func readSysPropAsInt64(ifname, prop string) (int64, error) { fname := fmt.Sprintf("/sys/class/net/%s/%s", ifname, prop) - contents, err := ioutil.ReadFile(fname) + contents, err := os.ReadFile(fname) if err != nil { return 0, err } @@ -2259,15 +2274,32 @@ type LinkSubscribeOptions struct { // LinkSubscribeWithOptions work like LinkSubscribe but enable to // provide additional options to modify the behavior. Currently, the // namespace can be provided as well as an error callback. -func LinkSubscribeWithOptions(ch chan<- LinkUpdate, done <-chan struct{}, options LinkSubscribeOptions) error { +func LinkSubscribeWithOptions( + ch chan<- LinkUpdate, + done <-chan struct{}, + options LinkSubscribeOptions, +) error { if options.Namespace == nil { none := netns.None() options.Namespace = &none } - return linkSubscribeAt(*options.Namespace, netns.None(), ch, done, options.ErrorCallback, options.ListExisting) + return linkSubscribeAt( + *options.Namespace, + netns.None(), + ch, + done, + options.ErrorCallback, + options.ListExisting, + ) } -func linkSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- LinkUpdate, done <-chan struct{}, cberr func(error), listExisting bool) error { +func linkSubscribeAt( + newNs, curNs netns.NsHandle, + ch chan<- LinkUpdate, + done <-chan struct{}, + cberr func(error), + listExisting bool, +) error { s, err := nl.SubscribeAt(newNs, curNs, unix.NETLINK_ROUTE, unix.RTNLGRP_LINK) if err != nil { return err @@ -2651,8 +2683,14 @@ func addBondSlaveAttrs(bondSlave *BondSlave, linkInfo *nl.RtAttr) { data.AddRtAttr(nl.IFLA_BOND_SLAVE_LINK_FAILURE_COUNT, nl.Uint32Attr(bondSlave.LinkFailureCount)) data.AddRtAttr(nl.IFLA_BOND_SLAVE_QUEUE_ID, nl.Uint16Attr(bondSlave.QueueId)) data.AddRtAttr(nl.IFLA_BOND_SLAVE_AD_AGGREGATOR_ID, nl.Uint16Attr(bondSlave.AggregatorId)) - data.AddRtAttr(nl.IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE, nl.Uint8Attr(bondSlave.AdActorOperPortState)) - data.AddRtAttr(nl.IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE, nl.Uint16Attr(bondSlave.AdPartnerOperPortState)) + data.AddRtAttr( + nl.IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE, + nl.Uint8Attr(bondSlave.AdActorOperPortState), + ) + data.AddRtAttr( + nl.IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE, + nl.Uint16Attr(bondSlave.AdPartnerOperPortState), + ) if mac := bondSlave.PermHardwareAddr; mac != nil { data.AddRtAttr(nl.IFLA_BOND_SLAVE_PERM_HWADDR, []byte(mac)) @@ -2744,7 +2782,9 @@ func parseMacvlanData(link Link, data []syscall.NetlinkRouteAttr) { case nl.IFLA_MACVLAN_MACADDR_DATA: macs, err := nl.ParseRouteAttr(datum.Value[:]) if err != nil { - panic(fmt.Sprintf("failed to ParseRouteAttr for IFLA_MACVLAN_MACADDR_DATA: %v", err)) + panic( + fmt.Sprintf("failed to ParseRouteAttr for IFLA_MACVLAN_MACADDR_DATA: %v", err), + ) } for _, macDatum := range macs { macv.MACAddrs = append(macv.MACAddrs, net.HardwareAddr(macDatum.Value[0:6])) @@ -3453,9 +3493,19 @@ func LinkSetBondSlave(link Link, master *Bond) error { ifreq := newIocltSlaveReq(link.Attrs().Name, master.Attrs().Name) - _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), unix.SIOCBONDENSLAVE, uintptr(unsafe.Pointer(ifreq))) + _, _, errno := syscall.Syscall( + syscall.SYS_IOCTL, + uintptr(fd), + unix.SIOCBONDENSLAVE, + uintptr(unsafe.Pointer(ifreq)), + ) if errno != 0 { - return fmt.Errorf("Failed to enslave %q to %q, errno=%v", link.Attrs().Name, master.Attrs().Name, errno) + return fmt.Errorf( + "Failed to enslave %q to %q, errno=%v", + link.Attrs().Name, + master.Attrs().Name, + errno, + ) } return nil } @@ -3515,9 +3565,18 @@ func VethPeerIndex(link *Veth) (int, error) { defer syscall.Close(fd) ifreq, sSet := newIocltStringSetReq(link.Name) - _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), SIOCETHTOOL, uintptr(unsafe.Pointer(ifreq))) + _, _, errno := syscall.Syscall( + syscall.SYS_IOCTL, + uintptr(fd), + SIOCETHTOOL, + uintptr(unsafe.Pointer(ifreq)), + ) if errno != 0 { - return -1, fmt.Errorf("SIOCETHTOOL request for %q failed, errno=%v", link.Attrs().Name, errno) + return -1, fmt.Errorf( + "SIOCETHTOOL request for %q failed, errno=%v", + link.Attrs().Name, + errno, + ) } stats := ethtoolStats{ @@ -3531,9 +3590,18 @@ func VethPeerIndex(link *Veth) (int, error) { } ifreq.Data = uintptr(unsafe.Pointer(&buffer[0])) - _, _, errno = syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), SIOCETHTOOL, uintptr(unsafe.Pointer(ifreq))) + _, _, errno = syscall.Syscall( + syscall.SYS_IOCTL, + uintptr(fd), + SIOCETHTOOL, + uintptr(unsafe.Pointer(ifreq)), + ) if errno != 0 { - return -1, fmt.Errorf("SIOCETHTOOL request for %q failed, errno=%v", link.Attrs().Name, errno) + return -1, fmt.Errorf( + "SIOCETHTOOL request for %q failed, errno=%v", + link.Attrs().Name, + errno, + ) } vstats, err := vethStatsDeserialize(buffer) diff --git a/netlink_test.go b/netlink_test.go index 43a061c5..94b6f6b5 100644 --- a/netlink_test.go +++ b/netlink_test.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package netlink @@ -7,7 +8,6 @@ import ( "crypto/rand" "encoding/hex" "fmt" - "io/ioutil" "log" "os" "os/exec" @@ -155,7 +155,7 @@ func setUpSEG6NetlinkTest(t *testing.T) tearDownNetlinkTest { } func setUpNetlinkTestWithKModule(t *testing.T, name string) tearDownNetlinkTest { - file, err := ioutil.ReadFile("/proc/modules") + file, err := os.ReadFile("/proc/modules") if err != nil { t.Fatal("Failed to open /proc/modules", err) } diff --git a/qdisc_linux.go b/qdisc_linux.go index e477bcd6..f73dd9d6 100644 --- a/qdisc_linux.go +++ b/qdisc_linux.go @@ -2,13 +2,14 @@ package netlink import ( "fmt" - "io/ioutil" + "os" "strconv" "strings" "syscall" - "github.com/vishvananda/netlink/nl" "golang.org/x/sys/unix" + + "github.com/vishvananda/netlink/nl" ) // NOTE function is here because it uses other linux functions @@ -645,7 +646,7 @@ var ( ) func initClock() { - data, err := ioutil.ReadFile("/proc/net/psched") + data, err := os.ReadFile("/proc/net/psched") if err != nil { return } diff --git a/rdma_link_test.go b/rdma_link_test.go index 241b89e8..f2844c34 100644 --- a/rdma_link_test.go +++ b/rdma_link_test.go @@ -1,9 +1,10 @@ +//go:build linux // +build linux package netlink import ( - "io/ioutil" + "os" "strings" "testing" @@ -12,7 +13,7 @@ import ( func setupRdmaKModule(t *testing.T, name string) { skipUnlessRoot(t) - file, err := ioutil.ReadFile("/proc/modules") + file, err := os.ReadFile("/proc/modules") if err != nil { t.Fatal("Failed to open /proc/modules", err) }