From f475640dd07b1f334edaf5dc3d7e1ec3ae954d41 Mon Sep 17 00:00:00 2001 From: Nahum Shalman Date: Fri, 27 Aug 2021 12:59:39 +0000 Subject: [PATCH] unix: add more illumos Lifreq helpers This work is in support of wireGuard/wireguard-go#39 Change-Id: Id205d28935b76b49ec6b29aba0a741659c5c01d7 Reviewed-on: https://go-review.googlesource.com/c/sys/+/345610 Run-TryBot: Ian Lance Taylor TryBot-Result: Go Bot Reviewed-by: Tobias Klauser Trust: Cherry Mui --- unix/syscall_illumos.go | 8 +++++++ unix/syscall_illumos_test.go | 43 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/unix/syscall_illumos.go b/unix/syscall_illumos.go index 8c535768..8d5f294c 100644 --- a/unix/syscall_illumos.go +++ b/unix/syscall_illumos.go @@ -162,6 +162,14 @@ func (l *Lifreq) GetLifruInt() int { return *(*int)(unsafe.Pointer(&l.Lifru[0])) } +func (l *Lifreq) SetLifruUint(d uint) { + *(*uint)(unsafe.Pointer(&l.Lifru[0])) = d +} + +func (l *Lifreq) GetLifruUint() uint { + return *(*uint)(unsafe.Pointer(&l.Lifru[0])) +} + func IoctlLifreq(fd int, req uint, l *Lifreq) error { return ioctl(fd, req, uintptr(unsafe.Pointer(l))) } diff --git a/unix/syscall_illumos_test.go b/unix/syscall_illumos_test.go index 45c89d03..dbfe5665 100644 --- a/unix/syscall_illumos_test.go +++ b/unix/syscall_illumos_test.go @@ -8,6 +8,9 @@ package unix_test import ( + "fmt" + "os/exec" + "strings" "testing" "golang.org/x/sys/unix" @@ -24,3 +27,43 @@ func TestLifreqSetName(t *testing.T) { t.Errorf(`Lifreq.SetName("tun0") failed: %v`, err) } } + +func TestLifreqGetMTU(t *testing.T) { + // Find links and their MTU using CLI tooling + // $ dladm show-link -p -o link,mtu + // net0:1500 + out, err := exec.Command("dladm", "show-link", "-p", "-o", "link,mtu").Output() + if err != nil { + t.Fatalf("unable to use dladm to find data links: %v", err) + } + lines := strings.Split(string(out), "\n") + tc := make(map[string]string) + for _, line := range lines { + v := strings.Split(line, ":") + if len(v) == 2 { + tc[v[0]] = v[1] + } + } + ip_fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0) + if err != nil { + t.Fatalf("could not open udp socket: %v", err) + } + // SIOCGLIFMTU is negative which confuses the compiler if used inline: + // Using "unix.IoctlLifreq(ip_fd, unix.SIOCGLIFMTU, &l)" results in + // "constant -1065850502 overflows uint" + reqnum := int(unix.SIOCGLIFMTU) + var l unix.Lifreq + for link, mtu := range tc { + err = l.SetName(link) + if err != nil { + t.Fatalf("Lifreq.SetName(%q) failed: %v", link, err) + } + if err = unix.IoctlLifreq(ip_fd, uint(reqnum), &l); err != nil { + t.Fatalf("unable to SIOCGLIFMTU: %v", err) + } + m := l.GetLifruUint() + if fmt.Sprintf("%d", m) != mtu { + t.Errorf("unable to read MTU correctly: expected %s, got %d", mtu, m) + } + } +}