forked from sandia-minimega/minimega
/
ip.go
128 lines (103 loc) · 2.88 KB
/
ip.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
// Copyright 2016-2021 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain
// rights in this software.
package bridge
import (
"fmt"
log "minilog"
"strings"
)
// upInterface activates an interface using the `ip` command. promisc controls
// whether the interface is brought up in promiscuous mode or not.
func upInterface(name string, promisc bool) error {
log.Info("up interface: %v", name)
args := []string{"ip", "link", "set", name, "up"}
if promisc {
args = append(args, "promisc", "on")
}
out, err := processWrapper(args...)
if err != nil {
return fmt.Errorf("up interface failed: %v: %v", err, out)
}
return nil
}
// downInterface deactivates an interface using the `ip` command.
func downInterface(name string) error {
log.Info("down interface: %v", name)
out, err := processWrapper("ip", "link", "set", name, "down")
if err != nil {
return fmt.Errorf("down interface failed: %v: %v", err, out)
}
return nil
}
// createTap creates a tuntap of the specified name using the `ip` command.
func createTap(name string) error {
log.Info("creating tuntap: %v", name)
out, err := processWrapper("ip", "tuntap", "add", "mode", "tap", name)
if strings.Contains(out, "Device or resource busy") {
return errAlreadyExists
} else if err != nil {
return fmt.Errorf("create tap failed: %v: %v", err, out)
}
return nil
}
// createVeth creates a veth of the specified name using the `ip` command.
func createVeth(tap, name, netnsname string) error {
log.Debug("creating veth: %v on %v in netns %v", name, tap, netnsname)
args := []string{
"ip",
"link",
"add",
tap,
"type",
"veth",
"peer",
name,
"netns",
netnsname,
}
out, err := processWrapper(args...)
if err != nil {
return fmt.Errorf("create veth failed: %v: %v", err, out)
}
return nil
}
// setMAC sets the MAC address for a container interface using the `ip` command.
func setMAC(netnsname, iface, mac string) error {
log.Debug("setting MAC: %v %v %v", netnsname, iface, mac)
args := []string{
"ip",
"netns",
"exec",
netnsname,
"ip",
"link",
"set",
"dev",
iface,
"address",
mac,
}
out, err := processWrapper(args...)
if err != nil {
return fmt.Errorf("set MAC failed: %v: %v", err, out)
}
return nil
}
// DestroyTap destroys an `unmanaged` tap using the `ip` command. This can be
// used when cleaning up from a crash or when a tap is not connected to a
// bridges. See `Bridge.DestroyTap` for managed taps.
func DestroyTap(name string) error {
bridgeLock.Lock()
defer bridgeLock.Unlock()
return destroyTap(name)
}
// destroyTap destroys a tuntap device.
func destroyTap(name string) error {
log.Info("destroying tuntap: %v", name)
out, err := processWrapper("ip", "link", "del", name)
if err != nil {
return fmt.Errorf("destroy tap failed: %v: %v", err, out)
}
return nil
}