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 <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Trust: Cherry Mui <cherryyz@google.com>
diff --git a/unix/syscall_illumos.go b/unix/syscall_illumos.go
index 8c53576..8d5f294 100644
--- a/unix/syscall_illumos.go
+++ b/unix/syscall_illumos.go
@@ -162,6 +162,14 @@
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 45c89d0..dbfe566 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 @@
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)
+ }
+ }
+}