Skip to content

Commit 1d9040b

Browse files
committed
syscall: provide and use ioctlPtr for all BSD platforms
Provide ioctlPtr for all BSD platforms, then use this for BPF. This reduces darwin specific code, as well as avoiding the use of an indirect system call on OpenBSD. Updates #63900 Change-Id: I81f3e74a3149150abe972f106903310e3cf26929 Reviewed-on: https://go-review.googlesource.com/c/go/+/540019 Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Joel Sing <[email protected]> Reviewed-by: Josh Rickmar <[email protected]>
1 parent fd59c87 commit 1d9040b

31 files changed

+461
-387
lines changed

src/syscall/bpf_bsd.go

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// Copyright 2011 The Go Authors. All rights reserved.
1+
// Copyright 2018 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build dragonfly || freebsd || netbsd || openbsd
5+
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
66

77
// Berkeley packet filter for BSD variants
88

@@ -25,55 +25,55 @@ func BpfJump(code, k, jt, jf int) *BpfInsn {
2525
// Deprecated: Use golang.org/x/net/bpf instead.
2626
func BpfBuflen(fd int) (int, error) {
2727
var l int
28-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGBLEN, uintptr(unsafe.Pointer(&l)))
29-
if err != 0 {
30-
return 0, Errno(err)
28+
err := ioctlPtr(fd, BIOCGBLEN, unsafe.Pointer(&l))
29+
if err != nil {
30+
return 0, err
3131
}
3232
return l, nil
3333
}
3434

3535
// Deprecated: Use golang.org/x/net/bpf instead.
3636
func SetBpfBuflen(fd, l int) (int, error) {
37-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSBLEN, uintptr(unsafe.Pointer(&l)))
38-
if err != 0 {
39-
return 0, Errno(err)
37+
err := ioctlPtr(fd, BIOCSBLEN, unsafe.Pointer(&l))
38+
if err != nil {
39+
return 0, err
4040
}
4141
return l, nil
4242
}
4343

4444
// Deprecated: Use golang.org/x/net/bpf instead.
4545
func BpfDatalink(fd int) (int, error) {
4646
var t int
47-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGDLT, uintptr(unsafe.Pointer(&t)))
48-
if err != 0 {
49-
return 0, Errno(err)
47+
err := ioctlPtr(fd, BIOCGDLT, unsafe.Pointer(&t))
48+
if err != nil {
49+
return 0, err
5050
}
5151
return t, nil
5252
}
5353

5454
// Deprecated: Use golang.org/x/net/bpf instead.
5555
func SetBpfDatalink(fd, t int) (int, error) {
56-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSDLT, uintptr(unsafe.Pointer(&t)))
57-
if err != 0 {
58-
return 0, Errno(err)
56+
err := ioctlPtr(fd, BIOCSDLT, unsafe.Pointer(&t))
57+
if err != nil {
58+
return 0, err
5959
}
6060
return t, nil
6161
}
6262

6363
// Deprecated: Use golang.org/x/net/bpf instead.
6464
func SetBpfPromisc(fd, m int) error {
65-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCPROMISC, uintptr(unsafe.Pointer(&m)))
66-
if err != 0 {
67-
return Errno(err)
65+
err := ioctlPtr(fd, BIOCPROMISC, unsafe.Pointer(&m))
66+
if err != nil {
67+
return err
6868
}
6969
return nil
7070
}
7171

7272
// Deprecated: Use golang.org/x/net/bpf instead.
7373
func FlushBpf(fd int) error {
74-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCFLUSH, 0)
75-
if err != 0 {
76-
return Errno(err)
74+
err := ioctlPtr(fd, BIOCFLUSH, nil)
75+
if err != nil {
76+
return err
7777
}
7878
return nil
7979
}
@@ -86,9 +86,9 @@ type ivalue struct {
8686
// Deprecated: Use golang.org/x/net/bpf instead.
8787
func BpfInterface(fd int, name string) (string, error) {
8888
var iv ivalue
89-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGETIF, uintptr(unsafe.Pointer(&iv)))
90-
if err != 0 {
91-
return "", Errno(err)
89+
err := ioctlPtr(fd, BIOCGETIF, unsafe.Pointer(&iv))
90+
if err != nil {
91+
return "", err
9292
}
9393
return name, nil
9494
}
@@ -97,47 +97,47 @@ func BpfInterface(fd int, name string) (string, error) {
9797
func SetBpfInterface(fd int, name string) error {
9898
var iv ivalue
9999
copy(iv.name[:], []byte(name))
100-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETIF, uintptr(unsafe.Pointer(&iv)))
101-
if err != 0 {
102-
return Errno(err)
100+
err := ioctlPtr(fd, BIOCSETIF, unsafe.Pointer(&iv))
101+
if err != nil {
102+
return err
103103
}
104104
return nil
105105
}
106106

107107
// Deprecated: Use golang.org/x/net/bpf instead.
108108
func BpfTimeout(fd int) (*Timeval, error) {
109109
var tv Timeval
110-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGRTIMEOUT, uintptr(unsafe.Pointer(&tv)))
111-
if err != 0 {
112-
return nil, Errno(err)
110+
err := ioctlPtr(fd, BIOCGRTIMEOUT, unsafe.Pointer(&tv))
111+
if err != nil {
112+
return nil, err
113113
}
114114
return &tv, nil
115115
}
116116

117117
// Deprecated: Use golang.org/x/net/bpf instead.
118118
func SetBpfTimeout(fd int, tv *Timeval) error {
119-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSRTIMEOUT, uintptr(unsafe.Pointer(tv)))
120-
if err != 0 {
121-
return Errno(err)
119+
err := ioctlPtr(fd, BIOCSRTIMEOUT, unsafe.Pointer(tv))
120+
if err != nil {
121+
return err
122122
}
123123
return nil
124124
}
125125

126126
// Deprecated: Use golang.org/x/net/bpf instead.
127127
func BpfStats(fd int) (*BpfStat, error) {
128128
var s BpfStat
129-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGSTATS, uintptr(unsafe.Pointer(&s)))
130-
if err != 0 {
131-
return nil, Errno(err)
129+
err := ioctlPtr(fd, BIOCGSTATS, unsafe.Pointer(&s))
130+
if err != nil {
131+
return nil, err
132132
}
133133
return &s, nil
134134
}
135135

136136
// Deprecated: Use golang.org/x/net/bpf instead.
137137
func SetBpfImmediate(fd, m int) error {
138-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCIMMEDIATE, uintptr(unsafe.Pointer(&m)))
139-
if err != 0 {
140-
return Errno(err)
138+
err := ioctlPtr(fd, BIOCIMMEDIATE, unsafe.Pointer(&m))
139+
if err != nil {
140+
return err
141141
}
142142
return nil
143143
}
@@ -147,19 +147,19 @@ func SetBpf(fd int, i []BpfInsn) error {
147147
var p BpfProgram
148148
p.Len = uint32(len(i))
149149
p.Insns = (*BpfInsn)(unsafe.Pointer(&i[0]))
150-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETF, uintptr(unsafe.Pointer(&p)))
151-
if err != 0 {
152-
return Errno(err)
150+
err := ioctlPtr(fd, BIOCSETF, unsafe.Pointer(&p))
151+
if err != nil {
152+
return err
153153
}
154154
return nil
155155
}
156156

157157
// Deprecated: Use golang.org/x/net/bpf instead.
158158
func CheckBpfVersion(fd int) error {
159159
var v BpfVersion
160-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCVERSION, uintptr(unsafe.Pointer(&v)))
161-
if err != 0 {
162-
return Errno(err)
160+
err := ioctlPtr(fd, BIOCVERSION, unsafe.Pointer(&v))
161+
if err != nil {
162+
return err
163163
}
164164
if v.Major != BPF_MAJOR_VERSION || v.Minor != BPF_MINOR_VERSION {
165165
return EINVAL
@@ -170,18 +170,18 @@ func CheckBpfVersion(fd int) error {
170170
// Deprecated: Use golang.org/x/net/bpf instead.
171171
func BpfHeadercmpl(fd int) (int, error) {
172172
var f int
173-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCGHDRCMPLT, uintptr(unsafe.Pointer(&f)))
174-
if err != 0 {
175-
return 0, Errno(err)
173+
err := ioctlPtr(fd, BIOCGHDRCMPLT, unsafe.Pointer(&f))
174+
if err != nil {
175+
return 0, err
176176
}
177177
return f, nil
178178
}
179179

180180
// Deprecated: Use golang.org/x/net/bpf instead.
181181
func SetBpfHeadercmpl(fd, f int) error {
182-
_, _, err := Syscall(SYS_IOCTL, uintptr(fd), BIOCSHDRCMPLT, uintptr(unsafe.Pointer(&f)))
183-
if err != 0 {
184-
return Errno(err)
182+
err := ioctlPtr(fd, BIOCSHDRCMPLT, unsafe.Pointer(&f))
183+
if err != nil {
184+
return err
185185
}
186186
return nil
187187
}

src/syscall/bpf_darwin.go

Lines changed: 0 additions & 185 deletions
This file was deleted.

0 commit comments

Comments
 (0)