Skip to content

Commit 0e3b74d

Browse files
Ian Bishopvishvananda
Ian Bishop
authored andcommitted
replace syscall with golang.org/x/sys/unix
1 parent b7fbf1f commit 0e3b74d

37 files changed

+605
-585
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ DIRS := \
33
nl
44

55
DEPS = \
6-
github.com/vishvananda/netns
6+
github.com/vishvananda/netns \
7+
golang.org/x/sys/unix
78

89
uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
910
testdirs = $(call uniq,$(foreach d,$(1),$(dir $(wildcard $(d)/*_test.go))))

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,4 @@ There are also a few pieces of low level netlink functionality that still
8989
need to be implemented. Routing rules are not in place and some of the
9090
more advanced link types. Hopefully there is decent structure and testing
9191
in place to make these fairly straightforward to add.
92+

addr_linux.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"fmt"
55
"net"
66
"strings"
7-
"syscall"
87

98
"github.com/vishvananda/netlink/nl"
109
"github.com/vishvananda/netns"
10+
"golang.org/x/sys/unix"
1111
)
1212

1313
// IFA_FLAGS is a u32 attribute.
@@ -22,7 +22,7 @@ func AddrAdd(link Link, addr *Addr) error {
2222
// AddrAdd will add an IP address to a link device.
2323
// Equivalent to: `ip addr add $addr dev $link`
2424
func (h *Handle) AddrAdd(link Link, addr *Addr) error {
25-
req := h.newNetlinkRequest(syscall.RTM_NEWADDR, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
25+
req := h.newNetlinkRequest(unix.RTM_NEWADDR, unix.NLM_F_CREATE|unix.NLM_F_EXCL|unix.NLM_F_ACK)
2626
return h.addrHandle(link, addr, req)
2727
}
2828

@@ -35,7 +35,7 @@ func AddrReplace(link Link, addr *Addr) error {
3535
// AddrReplace will replace (or, if not present, add) an IP address on a link device.
3636
// Equivalent to: `ip addr replace $addr dev $link`
3737
func (h *Handle) AddrReplace(link Link, addr *Addr) error {
38-
req := h.newNetlinkRequest(syscall.RTM_NEWADDR, syscall.NLM_F_CREATE|syscall.NLM_F_REPLACE|syscall.NLM_F_ACK)
38+
req := h.newNetlinkRequest(unix.RTM_NEWADDR, unix.NLM_F_CREATE|unix.NLM_F_REPLACE|unix.NLM_F_ACK)
3939
return h.addrHandle(link, addr, req)
4040
}
4141

@@ -48,7 +48,7 @@ func AddrDel(link Link, addr *Addr) error {
4848
// AddrDel will delete an IP address from a link device.
4949
// Equivalent to: `ip addr del $addr dev $link`
5050
func (h *Handle) AddrDel(link Link, addr *Addr) error {
51-
req := h.newNetlinkRequest(syscall.RTM_DELADDR, syscall.NLM_F_ACK)
51+
req := h.newNetlinkRequest(unix.RTM_DELADDR, unix.NLM_F_ACK)
5252
return h.addrHandle(link, addr, req)
5353
}
5454

@@ -75,7 +75,7 @@ func (h *Handle) addrHandle(link Link, addr *Addr, req *nl.NetlinkRequest) error
7575
localAddrData = addr.IP.To16()
7676
}
7777

78-
localData := nl.NewRtAttr(syscall.IFA_LOCAL, localAddrData)
78+
localData := nl.NewRtAttr(unix.IFA_LOCAL, localAddrData)
7979
req.AddData(localData)
8080
var peerAddrData []byte
8181
if addr.Peer != nil {
@@ -88,7 +88,7 @@ func (h *Handle) addrHandle(link Link, addr *Addr, req *nl.NetlinkRequest) error
8888
peerAddrData = localAddrData
8989
}
9090

91-
addressData := nl.NewRtAttr(syscall.IFA_ADDRESS, peerAddrData)
91+
addressData := nl.NewRtAttr(unix.IFA_ADDRESS, peerAddrData)
9292
req.AddData(addressData)
9393

9494
if addr.Flags != 0 {
@@ -109,14 +109,14 @@ func (h *Handle) addrHandle(link Link, addr *Addr, req *nl.NetlinkRequest) error
109109
}
110110
addr.Broadcast = calcBroadcast
111111
}
112-
req.AddData(nl.NewRtAttr(syscall.IFA_BROADCAST, addr.Broadcast))
112+
req.AddData(nl.NewRtAttr(unix.IFA_BROADCAST, addr.Broadcast))
113113

114114
if addr.Label != "" {
115-
labelData := nl.NewRtAttr(syscall.IFA_LABEL, nl.ZeroTerminated(addr.Label))
115+
labelData := nl.NewRtAttr(unix.IFA_LABEL, nl.ZeroTerminated(addr.Label))
116116
req.AddData(labelData)
117117
}
118118

119-
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
119+
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
120120
return err
121121
}
122122

@@ -131,11 +131,11 @@ func AddrList(link Link, family int) ([]Addr, error) {
131131
// Equivalent to: `ip addr show`.
132132
// The list can be filtered by link and ip family.
133133
func (h *Handle) AddrList(link Link, family int) ([]Addr, error) {
134-
req := h.newNetlinkRequest(syscall.RTM_GETADDR, syscall.NLM_F_DUMP)
134+
req := h.newNetlinkRequest(unix.RTM_GETADDR, unix.NLM_F_DUMP)
135135
msg := nl.NewIfInfomsg(family)
136136
req.AddData(msg)
137137

138-
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWADDR)
138+
msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWADDR)
139139
if err != nil {
140140
return nil, err
141141
}
@@ -187,21 +187,21 @@ func parseAddr(m []byte) (addr Addr, family, index int, err error) {
187187
var local, dst *net.IPNet
188188
for _, attr := range attrs {
189189
switch attr.Attr.Type {
190-
case syscall.IFA_ADDRESS:
190+
case unix.IFA_ADDRESS:
191191
dst = &net.IPNet{
192192
IP: attr.Value,
193193
Mask: net.CIDRMask(int(msg.Prefixlen), 8*len(attr.Value)),
194194
}
195195
addr.Peer = dst
196-
case syscall.IFA_LOCAL:
196+
case unix.IFA_LOCAL:
197197
local = &net.IPNet{
198198
IP: attr.Value,
199199
Mask: net.CIDRMask(int(msg.Prefixlen), 8*len(attr.Value)),
200200
}
201201
addr.IPNet = local
202-
case syscall.IFA_BROADCAST:
202+
case unix.IFA_BROADCAST:
203203
addr.Broadcast = attr.Value
204-
case syscall.IFA_LABEL:
204+
case unix.IFA_LABEL:
205205
addr.Label = string(attr.Value[:len(attr.Value)-1])
206206
case IFA_FLAGS:
207207
addr.Flags = int(native.Uint32(attr.Value[0:4]))
@@ -264,7 +264,7 @@ func AddrSubscribeWithOptions(ch chan<- AddrUpdate, done <-chan struct{}, option
264264
}
265265

266266
func addrSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- AddrUpdate, done <-chan struct{}, cberr func(error)) error {
267-
s, err := nl.SubscribeAt(newNs, curNs, syscall.NETLINK_ROUTE, syscall.RTNLGRP_IPV4_IFADDR, syscall.RTNLGRP_IPV6_IFADDR)
267+
s, err := nl.SubscribeAt(newNs, curNs, unix.NETLINK_ROUTE, unix.RTNLGRP_IPV4_IFADDR, unix.RTNLGRP_IPV6_IFADDR)
268268
if err != nil {
269269
return err
270270
}
@@ -286,7 +286,7 @@ func addrSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- AddrUpdate, done <-c
286286
}
287287
for _, m := range msgs {
288288
msgType := m.Header.Type
289-
if msgType != syscall.RTM_NEWADDR && msgType != syscall.RTM_DELADDR {
289+
if msgType != unix.RTM_NEWADDR && msgType != unix.RTM_DELADDR {
290290
if cberr != nil {
291291
cberr(fmt.Errorf("bad message type: %d", msgType))
292292
}
@@ -303,7 +303,7 @@ func addrSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- AddrUpdate, done <-c
303303

304304
ch <- AddrUpdate{LinkAddress: *addr.IPNet,
305305
LinkIndex: ifindex,
306-
NewAddr: msgType == syscall.RTM_NEWADDR,
306+
NewAddr: msgType == unix.RTM_NEWADDR,
307307
Flags: addr.Flags,
308308
Scope: addr.Scope,
309309
PreferedLft: addr.PreferedLft,

addr_test.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ package netlink
55
import (
66
"net"
77
"os"
8-
"syscall"
98
"testing"
109
"time"
10+
11+
"golang.org/x/sys/unix"
1112
)
1213

1314
func TestAddrAdd(t *testing.T) {
@@ -31,27 +32,27 @@ func DoTestAddr(t *testing.T, FunctionUndertest func(Link, *Addr) error) {
3132
}{
3233
{
3334
&Addr{IPNet: address},
34-
&Addr{IPNet: address, Label: "lo", Scope: syscall.RT_SCOPE_UNIVERSE, Flags: syscall.IFA_F_PERMANENT},
35+
&Addr{IPNet: address, Label: "lo", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
3536
},
3637
{
3738
&Addr{IPNet: address, Label: "local"},
38-
&Addr{IPNet: address, Label: "local", Scope: syscall.RT_SCOPE_UNIVERSE, Flags: syscall.IFA_F_PERMANENT},
39+
&Addr{IPNet: address, Label: "local", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
3940
},
4041
{
41-
&Addr{IPNet: address, Flags: syscall.IFA_F_OPTIMISTIC},
42-
&Addr{IPNet: address, Label: "lo", Flags: syscall.IFA_F_OPTIMISTIC | syscall.IFA_F_PERMANENT, Scope: syscall.RT_SCOPE_UNIVERSE},
42+
&Addr{IPNet: address, Flags: unix.IFA_F_OPTIMISTIC},
43+
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_UNIVERSE},
4344
},
4445
{
45-
&Addr{IPNet: address, Flags: syscall.IFA_F_OPTIMISTIC | syscall.IFA_F_DADFAILED},
46-
&Addr{IPNet: address, Label: "lo", Flags: syscall.IFA_F_OPTIMISTIC | syscall.IFA_F_DADFAILED | syscall.IFA_F_PERMANENT, Scope: syscall.RT_SCOPE_UNIVERSE},
46+
&Addr{IPNet: address, Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_DADFAILED},
47+
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_DADFAILED | unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_UNIVERSE},
4748
},
4849
{
49-
&Addr{IPNet: address, Scope: syscall.RT_SCOPE_NOWHERE},
50-
&Addr{IPNet: address, Label: "lo", Flags: syscall.IFA_F_PERMANENT, Scope: syscall.RT_SCOPE_NOWHERE},
50+
&Addr{IPNet: address, Scope: unix.RT_SCOPE_NOWHERE},
51+
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_NOWHERE},
5152
},
5253
{
5354
&Addr{IPNet: address, Peer: peer},
54-
&Addr{IPNet: address, Peer: peer, Label: "lo", Scope: syscall.RT_SCOPE_UNIVERSE, Flags: syscall.IFA_F_PERMANENT},
55+
&Addr{IPNet: address, Peer: peer, Label: "lo", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
5556
},
5657
}
5758

bridge_linux.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package netlink
22

33
import (
44
"fmt"
5-
"syscall"
65

76
"github.com/vishvananda/netlink/nl"
7+
"golang.org/x/sys/unix"
88
)
99

1010
// BridgeVlanList gets a map of device id to bridge vlan infos.
@@ -16,12 +16,12 @@ func BridgeVlanList() (map[int32][]*nl.BridgeVlanInfo, error) {
1616
// BridgeVlanList gets a map of device id to bridge vlan infos.
1717
// Equivalent to: `bridge vlan show`
1818
func (h *Handle) BridgeVlanList() (map[int32][]*nl.BridgeVlanInfo, error) {
19-
req := h.newNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_DUMP)
20-
msg := nl.NewIfInfomsg(syscall.AF_BRIDGE)
19+
req := h.newNetlinkRequest(unix.RTM_GETLINK, unix.NLM_F_DUMP)
20+
msg := nl.NewIfInfomsg(unix.AF_BRIDGE)
2121
req.AddData(msg)
2222
req.AddData(nl.NewRtAttr(nl.IFLA_EXT_MASK, nl.Uint32Attr(uint32(nl.RTEXT_FILTER_BRVLAN))))
2323

24-
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWLINK)
24+
msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWLINK)
2525
if err != nil {
2626
return nil, err
2727
}
@@ -63,7 +63,7 @@ func BridgeVlanAdd(link Link, vid uint16, pvid, untagged, self, master bool) err
6363
// BridgeVlanAdd adds a new vlan filter entry
6464
// Equivalent to: `bridge vlan add dev DEV vid VID [ pvid ] [ untagged ] [ self ] [ master ]`
6565
func (h *Handle) BridgeVlanAdd(link Link, vid uint16, pvid, untagged, self, master bool) error {
66-
return h.bridgeVlanModify(syscall.RTM_SETLINK, link, vid, pvid, untagged, self, master)
66+
return h.bridgeVlanModify(unix.RTM_SETLINK, link, vid, pvid, untagged, self, master)
6767
}
6868

6969
// BridgeVlanDel adds a new vlan filter entry
@@ -75,15 +75,15 @@ func BridgeVlanDel(link Link, vid uint16, pvid, untagged, self, master bool) err
7575
// BridgeVlanDel adds a new vlan filter entry
7676
// Equivalent to: `bridge vlan del dev DEV vid VID [ pvid ] [ untagged ] [ self ] [ master ]`
7777
func (h *Handle) BridgeVlanDel(link Link, vid uint16, pvid, untagged, self, master bool) error {
78-
return h.bridgeVlanModify(syscall.RTM_DELLINK, link, vid, pvid, untagged, self, master)
78+
return h.bridgeVlanModify(unix.RTM_DELLINK, link, vid, pvid, untagged, self, master)
7979
}
8080

8181
func (h *Handle) bridgeVlanModify(cmd int, link Link, vid uint16, pvid, untagged, self, master bool) error {
8282
base := link.Attrs()
8383
h.ensureIndex(base)
84-
req := h.newNetlinkRequest(cmd, syscall.NLM_F_ACK)
84+
req := h.newNetlinkRequest(cmd, unix.NLM_F_ACK)
8585

86-
msg := nl.NewIfInfomsg(syscall.AF_BRIDGE)
86+
msg := nl.NewIfInfomsg(unix.AF_BRIDGE)
8787
msg.Index = int32(base.Index)
8888
req.AddData(msg)
8989

@@ -107,7 +107,7 @@ func (h *Handle) bridgeVlanModify(cmd int, link Link, vid uint16, pvid, untagged
107107
}
108108
nl.NewRtAttrChild(br, nl.IFLA_BRIDGE_VLAN_INFO, vlanInfo.Serialize())
109109
req.AddData(br)
110-
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
110+
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
111111
if err != nil {
112112
return err
113113
}

class_linux.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"syscall"
66

77
"github.com/vishvananda/netlink/nl"
8+
"golang.org/x/sys/unix"
89
)
910

1011
// NOTE: function is in here because it uses other linux functions
@@ -50,7 +51,7 @@ func ClassDel(class Class) error {
5051
// ClassDel will delete a class from the system.
5152
// Equivalent to: `tc class del $class`
5253
func (h *Handle) ClassDel(class Class) error {
53-
return h.classModify(syscall.RTM_DELTCLASS, 0, class)
54+
return h.classModify(unix.RTM_DELTCLASS, 0, class)
5455
}
5556

5657
// ClassChange will change a class in place
@@ -64,7 +65,7 @@ func ClassChange(class Class) error {
6465
// Equivalent to: `tc class change $class`
6566
// The parent and handle MUST NOT be changed.
6667
func (h *Handle) ClassChange(class Class) error {
67-
return h.classModify(syscall.RTM_NEWTCLASS, 0, class)
68+
return h.classModify(unix.RTM_NEWTCLASS, 0, class)
6869
}
6970

7071
// ClassReplace will replace a class to the system.
@@ -82,7 +83,7 @@ func ClassReplace(class Class) error {
8283
// If a class already exist with this parent/handle pair, the class is changed.
8384
// If a class does not already exist with this parent/handle, a new class is created.
8485
func (h *Handle) ClassReplace(class Class) error {
85-
return h.classModify(syscall.RTM_NEWTCLASS, syscall.NLM_F_CREATE, class)
86+
return h.classModify(unix.RTM_NEWTCLASS, unix.NLM_F_CREATE, class)
8687
}
8788

8889
// ClassAdd will add a class to the system.
@@ -95,14 +96,14 @@ func ClassAdd(class Class) error {
9596
// Equivalent to: `tc class add $class`
9697
func (h *Handle) ClassAdd(class Class) error {
9798
return h.classModify(
98-
syscall.RTM_NEWTCLASS,
99-
syscall.NLM_F_CREATE|syscall.NLM_F_EXCL,
99+
unix.RTM_NEWTCLASS,
100+
unix.NLM_F_CREATE|unix.NLM_F_EXCL,
100101
class,
101102
)
102103
}
103104

104105
func (h *Handle) classModify(cmd, flags int, class Class) error {
105-
req := h.newNetlinkRequest(cmd, flags|syscall.NLM_F_ACK)
106+
req := h.newNetlinkRequest(cmd, flags|unix.NLM_F_ACK)
106107
base := class.Attrs()
107108
msg := &nl.TcMsg{
108109
Family: nl.FAMILY_ALL,
@@ -112,12 +113,12 @@ func (h *Handle) classModify(cmd, flags int, class Class) error {
112113
}
113114
req.AddData(msg)
114115

115-
if cmd != syscall.RTM_DELTCLASS {
116+
if cmd != unix.RTM_DELTCLASS {
116117
if err := classPayload(req, class); err != nil {
117118
return err
118119
}
119120
}
120-
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
121+
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
121122
return err
122123
}
123124

@@ -169,7 +170,7 @@ func ClassList(link Link, parent uint32) ([]Class, error) {
169170
// Equivalent to: `tc class show`.
170171
// Generally returns nothing if link and parent are not specified.
171172
func (h *Handle) ClassList(link Link, parent uint32) ([]Class, error) {
172-
req := h.newNetlinkRequest(syscall.RTM_GETTCLASS, syscall.NLM_F_DUMP)
173+
req := h.newNetlinkRequest(unix.RTM_GETTCLASS, unix.NLM_F_DUMP)
173174
msg := &nl.TcMsg{
174175
Family: nl.FAMILY_ALL,
175176
Parent: parent,
@@ -181,7 +182,7 @@ func (h *Handle) ClassList(link Link, parent uint32) ([]Class, error) {
181182
}
182183
req.AddData(msg)
183184

184-
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWTCLASS)
185+
msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWTCLASS)
185186
if err != nil {
186187
return nil, err
187188
}

0 commit comments

Comments
 (0)