/
nic_macvlan.go
204 lines (169 loc) · 5.73 KB
/
nic_macvlan.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
package device
import (
"fmt"
deviceConfig "github.com/lxc/lxd/lxd/device/config"
"github.com/lxc/lxd/lxd/instance"
"github.com/lxc/lxd/lxd/instance/instancetype"
"github.com/lxc/lxd/lxd/network"
"github.com/lxc/lxd/lxd/revert"
"github.com/lxc/lxd/shared"
)
type nicMACVLAN struct {
deviceCommon
}
// validateConfig checks the supplied config for correctness.
func (d *nicMACVLAN) validateConfig(instConf instance.ConfigReader) error {
if !instanceSupported(instConf.Type(), instancetype.Container, instancetype.VM) {
return ErrUnsupportedDevType
}
requiredFields := []string{"parent"}
optionalFields := []string{
"name",
"mtu",
"hwaddr",
"vlan",
"maas.subnet.ipv4",
"maas.subnet.ipv6",
"boot.priority",
}
err := d.config.Validate(nicValidationRules(requiredFields, optionalFields))
if err != nil {
return err
}
return nil
}
// validateEnvironment checks the runtime environment for correctness.
func (d *nicMACVLAN) validateEnvironment() error {
if d.inst.Type() == instancetype.Container && d.config["name"] == "" {
return fmt.Errorf("Requires name property to start")
}
if !shared.PathExists(fmt.Sprintf("/sys/class/net/%s", d.config["parent"])) {
return fmt.Errorf("Parent device '%s' doesn't exist", d.config["parent"])
}
return nil
}
// Start is run when the device is added to a running instance or instance is starting up.
func (d *nicMACVLAN) Start() (*deviceConfig.RunConfig, error) {
err := d.validateEnvironment()
if err != nil {
return nil, err
}
// Lock to avoid issues with containers starting in parallel.
networkCreateSharedDeviceLock.Lock()
defer networkCreateSharedDeviceLock.Unlock()
revert := revert.New()
defer revert.Fail()
saveData := make(map[string]string)
// Decide which parent we should use based on VLAN setting.
actualParentName := network.GetHostDevice(d.config["parent"], d.config["vlan"])
// Record the temporary device name used for deletion later.
saveData["host_name"] = networkRandomDevName("mac")
// Create VLAN parent device if needed.
statusDev, err := networkCreateVlanDeviceIfNeeded(d.state, d.config["parent"], actualParentName, d.config["vlan"])
if err != nil {
return nil, err
}
// Record whether we created the parent device or not so it can be removed on stop.
saveData["last_state.created"] = fmt.Sprintf("%t", statusDev != "existing")
if shared.IsTrue(saveData["last_state.created"]) {
revert.Add(func() {
networkRemoveInterfaceIfNeeded(d.state, actualParentName, d.inst, d.config["parent"], d.config["vlan"])
})
}
if d.inst.Type() == instancetype.Container {
// Create MACVLAN interface.
_, err = shared.RunCommand("ip", "link", "add", "dev", saveData["host_name"], "link", actualParentName, "type", "macvlan", "mode", "bridge")
if err != nil {
return nil, err
}
} else if d.inst.Type() == instancetype.VM {
// Create MACVTAP interface.
_, err = shared.RunCommand("ip", "link", "add", "dev", saveData["host_name"], "link", actualParentName, "type", "macvtap", "mode", "bridge")
if err != nil {
return nil, err
}
}
revert.Add(func() { NetworkRemoveInterface(saveData["host_name"]) })
// Set the MAC address.
if d.config["hwaddr"] != "" {
_, err := shared.RunCommand("ip", "link", "set", "dev", saveData["host_name"], "address", d.config["hwaddr"])
if err != nil {
return nil, fmt.Errorf("Failed to set the MAC address: %s", err)
}
}
// Set the MTU.
if d.config["mtu"] != "" {
_, err := shared.RunCommand("ip", "link", "set", "dev", saveData["host_name"], "mtu", d.config["mtu"])
if err != nil {
return nil, fmt.Errorf("Failed to set the MTU: %s", err)
}
}
if d.inst.Type() == instancetype.VM {
// Bring the interface up on host side.
_, err := shared.RunCommand("ip", "link", "set", "dev", saveData["host_name"], "up")
if err != nil {
return nil, fmt.Errorf("Failed to bring up interface %s: %v", saveData["host_name"], err)
}
}
err = d.volatileSet(saveData)
if err != nil {
return nil, err
}
runConf := deviceConfig.RunConfig{}
runConf.NetworkInterface = []deviceConfig.RunConfigItem{
{Key: "name", Value: d.config["name"]},
{Key: "type", Value: "phys"},
{Key: "flags", Value: "up"},
{Key: "link", Value: saveData["host_name"]},
}
if d.inst.Type() == instancetype.VM {
runConf.NetworkInterface = append(runConf.NetworkInterface,
[]deviceConfig.RunConfigItem{
{Key: "devName", Value: d.name},
{Key: "hwaddr", Value: d.config["hwaddr"]},
}...)
}
revert.Success()
return &runConf, nil
}
// Stop is run when the device is removed from the instance.
func (d *nicMACVLAN) Stop() (*deviceConfig.RunConfig, error) {
v := d.volatileGet()
runConf := deviceConfig.RunConfig{
PostHooks: []func() error{d.postStop},
NetworkInterface: []deviceConfig.RunConfigItem{
{Key: "link", Value: v["host_name"]},
},
}
return &runConf, nil
}
// postStop is run after the device is removed from the instance.
func (d *nicMACVLAN) postStop() error {
defer d.volatileSet(map[string]string{
"host_name": "",
"last_state.hwaddr": "",
"last_state.mtu": "",
"last_state.created": "",
})
errs := []error{}
v := d.volatileGet()
// Delete the detached device.
if v["host_name"] != "" && shared.PathExists(fmt.Sprintf("/sys/class/net/%s", v["host_name"])) {
err := NetworkRemoveInterface(v["host_name"])
if err != nil {
errs = append(errs, err)
}
}
// This will delete the parent interface if we created it for VLAN parent.
if shared.IsTrue(v["last_state.created"]) {
actualParentName := network.GetHostDevice(d.config["parent"], d.config["vlan"])
err := networkRemoveInterfaceIfNeeded(d.state, actualParentName, d.inst, d.config["parent"], d.config["vlan"])
if err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return fmt.Errorf("%v", errs)
}
return nil
}