forked from canonical/lxd
/
nic_p2p.go
208 lines (168 loc) · 5.56 KB
/
nic_p2p.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
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/lxd/util"
)
type nicP2P struct {
deviceCommon
}
// CanHotPlug returns whether the device can be managed whilst the instance is running. Returns true.
func (d *nicP2P) CanHotPlug() bool {
return true
}
// validateConfig checks the supplied config for correctness.
func (d *nicP2P) validateConfig(instConf instance.ConfigReader) error {
if !instanceSupported(instConf.Type(), instancetype.Container, instancetype.VM) {
return ErrUnsupportedDevType
}
optionalFields := []string{
"name",
"mtu",
"hwaddr",
"host_name",
"limits.ingress",
"limits.egress",
"limits.max",
"ipv4.routes",
"ipv6.routes",
"boot.priority",
}
err := d.config.Validate(nicValidationRules([]string{}, optionalFields, instConf))
if err != nil {
return err
}
return nil
}
// validateEnvironment checks the runtime environment for correctness.
func (d *nicP2P) validateEnvironment() error {
if d.inst.Type() == instancetype.Container && d.config["name"] == "" {
return fmt.Errorf("Requires name property to start")
}
return nil
}
// UpdatableFields returns a list of fields that can be updated without triggering a device remove & add.
func (d *nicP2P) UpdatableFields(oldDevice Type) []string {
// Check old and new device types match.
_, match := oldDevice.(*nicP2P)
if !match {
return []string{}
}
return []string{"limits.ingress", "limits.egress", "limits.max", "ipv4.routes", "ipv6.routes"}
}
// Start is run when the device is added to a running instance or instance is starting up.
func (d *nicP2P) Start() (*deviceConfig.RunConfig, error) {
err := d.validateEnvironment()
if err != nil {
return nil, err
}
revert := revert.New()
defer revert.Fail()
saveData := make(map[string]string)
saveData["host_name"] = d.config["host_name"]
var peerName string
// Create veth pair and configure the peer end with custom hwaddr and mtu if supplied.
if d.inst.Type() == instancetype.Container {
if saveData["host_name"] == "" {
saveData["host_name"] = network.RandomDevName("veth")
}
peerName, err = networkCreateVethPair(saveData["host_name"], d.config)
} else if d.inst.Type() == instancetype.VM {
if saveData["host_name"] == "" {
saveData["host_name"] = network.RandomDevName("tap")
}
peerName = saveData["host_name"] // VMs use the host_name to link to the TAP FD.
err = networkCreateTap(saveData["host_name"], d.config)
}
if err != nil {
return nil, err
}
revert.Add(func() { network.InterfaceRemove(saveData["host_name"]) })
// Populate device config with volatile fields if needed.
networkVethFillFromVolatile(d.config, saveData)
// Apply host-side routes to veth interface.
err = networkNICRouteAdd(d.config["host_name"], append(util.SplitNTrimSpace(d.config["ipv4.routes"], ",", -1, true), util.SplitNTrimSpace(d.config["ipv6.routes"], ",", -1, true)...)...)
if err != nil {
return nil, err
}
// Apply host-side limits.
err = networkSetupHostVethLimits(d.config)
if err != nil {
return nil, err
}
err = d.volatileSet(saveData)
if err != nil {
return nil, err
}
runConf := deviceConfig.RunConfig{}
runConf.NetworkInterface = []deviceConfig.RunConfigItem{
{Key: "type", Value: "phys"},
{Key: "name", Value: d.config["name"]},
{Key: "flags", Value: "up"},
{Key: "link", Value: peerName},
}
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
}
// Update applies configuration changes to a started device.
func (d *nicP2P) Update(oldDevices deviceConfig.Devices, isRunning bool) error {
if !isRunning {
return nil
}
err := d.validateEnvironment()
if err != nil {
return err
}
oldConfig := oldDevices[d.name]
v := d.volatileGet()
// Populate device config with volatile fields if needed.
networkVethFillFromVolatile(d.config, v)
networkVethFillFromVolatile(oldConfig, v)
// Remove old host-side routes from veth interface.
networkNICRouteDelete(oldConfig["host_name"], append(util.SplitNTrimSpace(oldConfig["ipv4.routes"], ",", -1, true), util.SplitNTrimSpace(oldConfig["ipv6.routes"], ",", -1, true)...)...)
// Apply host-side routes to veth interface.
err = networkNICRouteAdd(d.config["host_name"], append(util.SplitNTrimSpace(d.config["ipv4.routes"], ",", -1, true), util.SplitNTrimSpace(d.config["ipv6.routes"], ",", -1, true)...)...)
if err != nil {
return err
}
// Apply host-side limits.
err = networkSetupHostVethLimits(d.config)
if err != nil {
return err
}
return nil
}
// Stop is run when the device is removed from the instance.
func (d *nicP2P) Stop() (*deviceConfig.RunConfig, error) {
runConf := deviceConfig.RunConfig{
PostHooks: []func() error{d.postStop},
}
return &runConf, nil
}
// postStop is run after the device is removed from the instance.
func (d *nicP2P) postStop() error {
defer d.volatileSet(map[string]string{
"host_name": "",
})
v := d.volatileGet()
networkVethFillFromVolatile(d.config, v)
if d.config["host_name"] != "" && network.InterfaceExists(d.config["host_name"]) {
// Removing host-side end of veth pair will delete the peer end too.
err := network.InterfaceRemove(d.config["host_name"])
if err != nil {
return fmt.Errorf("Failed to remove interface %s: %s", d.config["host_name"], err)
}
}
return nil
}