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>
This commit is contained in:
Nahum Shalman
2021-08-27 12:59:39 +00:00
committed by Tobias Klauser
parent 164ac21ac1
commit f475640dd0
2 changed files with 51 additions and 0 deletions

View File

@@ -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)))
}

View File

@@ -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)
}
}
}