Skip to content

Commit f475640

Browse files
nshalmantklauser
authored andcommitted
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 <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Tobias Klauser <[email protected]> Trust: Cherry Mui <[email protected]>
1 parent 164ac21 commit f475640

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

unix/syscall_illumos.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ func (l *Lifreq) GetLifruInt() int {
162162
return *(*int)(unsafe.Pointer(&l.Lifru[0]))
163163
}
164164

165+
func (l *Lifreq) SetLifruUint(d uint) {
166+
*(*uint)(unsafe.Pointer(&l.Lifru[0])) = d
167+
}
168+
169+
func (l *Lifreq) GetLifruUint() uint {
170+
return *(*uint)(unsafe.Pointer(&l.Lifru[0]))
171+
}
172+
165173
func IoctlLifreq(fd int, req uint, l *Lifreq) error {
166174
return ioctl(fd, req, uintptr(unsafe.Pointer(l)))
167175
}

unix/syscall_illumos_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
package unix_test
99

1010
import (
11+
"fmt"
12+
"os/exec"
13+
"strings"
1114
"testing"
1215

1316
"golang.org/x/sys/unix"
@@ -24,3 +27,43 @@ func TestLifreqSetName(t *testing.T) {
2427
t.Errorf(`Lifreq.SetName("tun0") failed: %v`, err)
2528
}
2629
}
30+
31+
func TestLifreqGetMTU(t *testing.T) {
32+
// Find links and their MTU using CLI tooling
33+
// $ dladm show-link -p -o link,mtu
34+
// net0:1500
35+
out, err := exec.Command("dladm", "show-link", "-p", "-o", "link,mtu").Output()
36+
if err != nil {
37+
t.Fatalf("unable to use dladm to find data links: %v", err)
38+
}
39+
lines := strings.Split(string(out), "\n")
40+
tc := make(map[string]string)
41+
for _, line := range lines {
42+
v := strings.Split(line, ":")
43+
if len(v) == 2 {
44+
tc[v[0]] = v[1]
45+
}
46+
}
47+
ip_fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0)
48+
if err != nil {
49+
t.Fatalf("could not open udp socket: %v", err)
50+
}
51+
// SIOCGLIFMTU is negative which confuses the compiler if used inline:
52+
// Using "unix.IoctlLifreq(ip_fd, unix.SIOCGLIFMTU, &l)" results in
53+
// "constant -1065850502 overflows uint"
54+
reqnum := int(unix.SIOCGLIFMTU)
55+
var l unix.Lifreq
56+
for link, mtu := range tc {
57+
err = l.SetName(link)
58+
if err != nil {
59+
t.Fatalf("Lifreq.SetName(%q) failed: %v", link, err)
60+
}
61+
if err = unix.IoctlLifreq(ip_fd, uint(reqnum), &l); err != nil {
62+
t.Fatalf("unable to SIOCGLIFMTU: %v", err)
63+
}
64+
m := l.GetLifruUint()
65+
if fmt.Sprintf("%d", m) != mtu {
66+
t.Errorf("unable to read MTU correctly: expected %s, got %d", mtu, m)
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)