-
Notifications
You must be signed in to change notification settings - Fork 61
/
link_linux.go
333 lines (280 loc) · 10.3 KB
/
link_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package tenus
import (
"fmt"
"net"
"os"
"syscall"
"github.com/docker/libcontainer/netlink"
"github.com/docker/libcontainer/system"
)
// LinkOptions allows you to specify network link options.
type LinkOptions struct {
// MAC address
MacAddr string
// Maximum Transmission Unit
MTU int
// Link network flags i.e. FlagUp, FlagLoopback, FlagMulticast
Flags net.Flags
// Network namespace in which the network link should be created
Ns int
}
// Linker is a generic Linux network link
type Linker interface {
// NetInterface returns the link's logical network interface
NetInterface() *net.Interface
// DeleteLink deletes the link from Linux host
DeleteLink() error
// SetLinkMTU sets the link's MTU.
SetLinkMTU(int) error
// SetLinkMacAddress sets the link's MAC address.
SetLinkMacAddress(string) error
// SetLinkUp brings the link up
SetLinkUp() error
// SetLinkDown brings the link down
SetLinkDown() error
// SetLinkIp configures the link's IP address
SetLinkIp(net.IP, *net.IPNet) error
// UnsetLinkIp remove and IP address from the link
UnsetLinkIp(net.IP, *net.IPNet) error
// SetLinkDefaultGw configures the link's default gateway
SetLinkDefaultGw(*net.IP) error
// SetLinkNetNsPid moves the link to network namespace specified by PID
SetLinkNetNsPid(int) error
// SetLinkNetInNs configures network settings of the link in network namespace
SetLinkNetInNs(int, net.IP, *net.IPNet, *net.IP) error
}
// Link has a logical network interface
type Link struct {
ifc *net.Interface
}
// NewLink creates new network link on Linux host.
//
// It is equivalent of running: ip link add name ${ifcName} type dummy
// NewLink returns Linker which is initialized to a pointer of type Link if the
// link was created successfully on the Linux host.
// It returns error if the network link could not be created on Linux host.
func NewLink(ifcName string) (Linker, error) {
if ok, err := NetInterfaceNameValid(ifcName); !ok {
return nil, err
}
if _, err := net.InterfaceByName(ifcName); err == nil {
return nil, fmt.Errorf("Interface name %s already assigned on the host", ifcName)
}
if err := netlink.NetworkLinkAdd(ifcName, "dummy"); err != nil {
return nil, fmt.Errorf("Could not create new link %s: %s", ifcName, err)
}
newIfc, err := net.InterfaceByName(ifcName)
if err != nil {
return nil, fmt.Errorf("Could not find the new interface: %s", err)
}
return &Link{
ifc: newIfc,
}, nil
}
// NewLinkFrom creates new tenus link on Linux host from an existing interface of given name
func NewLinkFrom(ifcName string) (Linker, error) {
if ok, err := NetInterfaceNameValid(ifcName); !ok {
return nil, err
}
newIfc, err := net.InterfaceByName(ifcName)
if err != nil {
return nil, fmt.Errorf("Could not find the new interface: %s", err)
}
return &Link{
ifc: newIfc,
}, nil
}
// NewLinkWithOptions creates new network link on Linux host and sets some of its network
// parameters passed in as LinkOptions
//
// Calling NewLinkWithOptions is equivalent of running following commands one after another if
// particular option is passed in as a parameter:
// ip link add name ${ifcName} type dummy
// ip link set dev ${ifcName} address ${MAC address}
// ip link set dev ${ifcName} mtu ${MTU value}
// ip link set dev ${ifcName} up
// NewLinkWithOptions returns Linker which is initialized to a pointer of type Link if the network
// link with given LinkOptions was created successfully on the Linux host.
// It attempts to delete the link if any of the LinkOptions are incorrect or if setting the options
// failed and returns error.
func NewLinkWithOptions(ifcName string, opts LinkOptions) (Linker, error) {
if ok, err := NetInterfaceNameValid(ifcName); !ok {
return nil, err
}
if _, err := net.InterfaceByName(ifcName); err == nil {
return nil, fmt.Errorf("Interface name %s already assigned on the host", ifcName)
}
if err := netlink.NetworkLinkAdd(ifcName, "dummy"); err != nil {
return nil, fmt.Errorf("Could not create new link %s: %s", ifcName, err)
}
newIfc, err := net.InterfaceByName(ifcName)
if err != nil {
return nil, fmt.Errorf("Could not find the new interface: %s", err)
}
if (opts != LinkOptions{}) {
errOpts := setLinkOptions(newIfc, opts)
if errOpts != nil {
if errDel := DeleteLink(newIfc.Name); err != nil {
return nil, fmt.Errorf("Incorrect options specified: %s. Attempt to delete the link failed: %s",
errOpts, errDel)
}
return nil, fmt.Errorf("Could not set link options: %s", errOpts)
}
}
return &Link{
ifc: newIfc,
}, nil
}
// DeleteLink deletes netowrk link from Linux Host
// It is equivalent of running: ip link delete dev ${name}
func DeleteLink(name string) error {
return netlink.NetworkLinkDel(name)
}
// NetInterface returns link's logical network interface.
func (l *Link) NetInterface() *net.Interface {
return l.ifc
}
// DeleteLink deletes link interface on Linux host.
// It is equivalent of running: ip link delete dev ${interface name}
func (l *Link) DeleteLink() error {
return netlink.NetworkLinkDel(l.NetInterface().Name)
}
// SetLinkMTU sets link's MTU.
// It is equivalent of running: ip link set dev ${interface name} mtu ${MTU value}
func (l *Link) SetLinkMTU(mtu int) error {
return netlink.NetworkSetMTU(l.NetInterface(), mtu)
}
// SetLinkMacAddress sets link's MAC address.
// It is equivalent of running: ip link set dev ${interface name} address ${address}
func (l *Link) SetLinkMacAddress(macaddr string) error {
return netlink.NetworkSetMacAddress(l.NetInterface(), macaddr)
}
// SetLinkUp brings the link up.
// It is equivalent of running: ip link set dev ${interface name} up
func (l *Link) SetLinkUp() error {
return netlink.NetworkLinkUp(l.NetInterface())
}
// SetLinkDown brings the link down.
// It is equivalent of running: ip link set dev ${interface name} down
func (l *Link) SetLinkDown() error {
return netlink.NetworkLinkDown(l.NetInterface())
}
// SetLinkIp configures the link's IP address.
// It is equivalent of running: ip address add ${address}/${mask} dev ${interface name}
func (l *Link) SetLinkIp(ip net.IP, network *net.IPNet) error {
return netlink.NetworkLinkAddIp(l.NetInterface(), ip, network)
}
// UnsetLinkIp configures the link's IP address.
// It is equivalent of running: ip address del ${address}/${mask} dev ${interface name}
func (l *Link) UnsetLinkIp(ip net.IP, network *net.IPNet) error {
return netlink.NetworkLinkDelIp(l.NetInterface(), ip, network)
}
// SetLinkDefaultGw configures the link's default Gateway.
// It is equivalent of running: ip route add default via ${ip address}
func (l *Link) SetLinkDefaultGw(gw *net.IP) error {
return netlink.AddDefaultGw(gw.String(), l.NetInterface().Name)
}
// SetLinkNetNsPid moves the link to Network namespace specified by PID.
func (l *Link) SetLinkNetNsPid(nspid int) error {
return netlink.NetworkSetNsPid(l.NetInterface(), nspid)
}
// SetLinkNetInNs configures network settings of the link in network namespace specified by PID.
func (l *Link) SetLinkNetInNs(nspid int, ip net.IP, network *net.IPNet, gw *net.IP) error {
origNs, _ := NetNsHandle(os.Getpid())
defer syscall.Close(int(origNs))
defer system.Setns(origNs, syscall.CLONE_NEWNET)
if err := SetNetNsToPid(nspid); err != nil {
return fmt.Errorf("Setting network namespace failed: %s", err)
}
if err := netlink.NetworkLinkAddIp(l.NetInterface(), ip, network); err != nil {
return fmt.Errorf("Unable to set IP: %s in pid: %d network namespace", ip.String(), nspid)
}
if err := netlink.NetworkLinkUp(l.ifc); err != nil {
return fmt.Errorf("Unable to bring %s interface UP: %d", l.ifc.Name, nspid)
}
if gw != nil {
if err := netlink.AddDefaultGw(gw.String(), l.NetInterface().Name); err != nil {
return fmt.Errorf("Unable to set Default gateway: %s in pid: %d network namespace", gw.String(), nspid)
}
}
return nil
}
// SetLinkNsFd sets the link's Linux namespace to the one specified by filesystem path.
func (l *Link) SetLinkNsFd(nspath string) error {
fd, err := syscall.Open(nspath, syscall.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("Could not attach to Network namespace: %s", err)
}
return netlink.NetworkSetNsFd(l.NetInterface(), fd)
}
// SetLinkNsToDocker sets the link's Linux namespace to a running Docker one specified by Docker name.
func (l *Link) SetLinkNsToDocker(name string, dockerHost string) error {
pid, err := DockerPidByName(name, dockerHost)
if err != nil {
return fmt.Errorf("Failed to find docker %s : %s", name, err)
}
return l.SetLinkNetNsPid(pid)
}
// RenameInterfaceByName renames an interface of given name.
func RenameInterfaceByName(old string, newName string) error {
iface, err := net.InterfaceByName(old)
if err != nil {
return err
}
return netlink.NetworkChangeName(iface, newName)
}
// setLinkOptions validates and sets link's various options passed in as LinkOptions.
func setLinkOptions(ifc *net.Interface, opts LinkOptions) error {
macaddr, mtu, flags, ns := opts.MacAddr, opts.MTU, opts.Flags, opts.Ns
// if MTU is passed in LinkOptions
if mtu != 0 {
if err := validMtu(mtu); err != nil {
return err
}
if err := netlink.NetworkSetMTU(ifc, mtu); err != nil {
return fmt.Errorf("Unable to set MTU: %s", err)
}
}
// if MacAddress is passed in LinkOptions
if macaddr != "" {
if err := validMacAddress(macaddr); err != nil {
return err
}
if err := netlink.NetworkSetMacAddress(ifc, macaddr); err != nil {
return fmt.Errorf("Unable to set MAC Address: %s", err)
}
}
// if ns is passed in LinkOptions
if ns != 0 {
if err := validNs(ns); err != nil {
return err
}
if err := netlink.NetworkSetNsPid(ifc, ns); err != nil {
return fmt.Errorf("Unable to set Network namespace: %s", err)
}
}
// if flags is passed in LinkOptions
if flags != 0 {
if err := validFlags(flags); err != nil {
return err
}
if ns != 0 && (ns != 1 || ns != os.Getpid()) {
if (flags & syscall.IFF_UP) == syscall.IFF_UP {
origNs, _ := NetNsHandle(os.Getpid())
defer syscall.Close(int(origNs))
defer system.Setns(origNs, syscall.CLONE_NEWNET)
if err := SetNetNsToPid(ns); err != nil {
return fmt.Errorf("Switching to %d network namespace failed: %s", ns, err)
}
if err := netlink.NetworkLinkUp(ifc); err != nil {
return fmt.Errorf("Unable to bring %s interface UP: %d", ifc.Name, ns)
}
}
} else {
if err := netlink.NetworkLinkUp(ifc); err != nil {
return fmt.Errorf("Could not bring up network link %s: %s", ifc.Name, err)
}
}
}
return nil
}