/
nic_ipvlan.go
244 lines (201 loc) · 7.37 KB
/
nic_ipvlan.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
package device
import (
"fmt"
"strings"
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/util"
"github.com/lxc/lxd/shared"
)
type nicIPVLAN struct {
deviceCommon
}
func (d *nicIPVLAN) CanHotPlug() (bool, []string) {
return false, []string{}
}
// validateConfig checks the supplied config for correctness.
func (d *nicIPVLAN) validateConfig(instConf instance.ConfigReader) error {
if !instanceSupported(instConf.Type(), instancetype.Container) {
return ErrUnsupportedDevType
}
requiredFields := []string{"parent"}
optionalFields := []string{
"name",
"mtu",
"hwaddr",
"vlan",
}
rules := nicValidationRules(requiredFields, optionalFields)
rules["ipv4.address"] = func(value string) error {
if value == "" {
return nil
}
return NetworkValidAddressV4List(value)
}
rules["ipv6.address"] = func(value string) error {
if value == "" {
return nil
}
return NetworkValidAddressV6List(value)
}
err := d.config.Validate(rules)
if err != nil {
return err
}
return nil
}
// validateEnvironment checks the runtime environment for correctness.
func (d *nicIPVLAN) 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"])
}
extensions := d.state.OS.LXCFeatures
if !extensions["network_ipvlan"] || !extensions["network_l2proxy"] || !extensions["network_gateway_device_route"] {
return fmt.Errorf("Requires liblxc has following API extensions: network_ipvlan, network_l2proxy, network_gateway_device_route")
}
if d.config["ipv4.address"] != "" {
// Check necessary sysctls are configured for use with l2proxy parent in IPVLAN l3s mode.
ipv4FwdPath := fmt.Sprintf("net/ipv4/conf/%s/forwarding", d.config["parent"])
sysctlVal, err := util.SysctlGet(ipv4FwdPath)
if err != nil || sysctlVal != "1\n" {
return fmt.Errorf("Error reading net sysctl %s: %v", ipv4FwdPath, err)
}
if sysctlVal != "1\n" {
return fmt.Errorf("IPVLAN in L3S mode requires sysctl net.ipv4.conf.%s.forwarding=1", d.config["parent"])
}
}
if d.config["ipv6.address"] != "" {
// Check necessary sysctls are configured for use with l2proxy parent in IPVLAN l3s mode.
ipv6FwdPath := fmt.Sprintf("net/ipv6/conf/%s/forwarding", d.config["parent"])
sysctlVal, err := util.SysctlGet(ipv6FwdPath)
if err != nil {
return fmt.Errorf("Error reading net sysctl %s: %v", ipv6FwdPath, err)
}
if sysctlVal != "1\n" {
return fmt.Errorf("IPVLAN in L3S mode requires sysctl net.ipv6.conf.%s.forwarding=1", d.config["parent"])
}
ipv6ProxyNdpPath := fmt.Sprintf("net/ipv6/conf/%s/proxy_ndp", d.config["parent"])
sysctlVal, err = util.SysctlGet(ipv6ProxyNdpPath)
if err != nil {
return fmt.Errorf("Error reading net sysctl %s: %v", ipv6ProxyNdpPath, err)
}
if sysctlVal != "1\n" {
return fmt.Errorf("IPVLAN in L3S mode requires sysctl net.ipv6.conf.%s.proxy_ndp=1", d.config["parent"])
}
}
return nil
}
// Start is run when the instance is starting up (IPVLAN doesn't support hot plugging).
func (d *nicIPVLAN) 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()
saveData := make(map[string]string)
// Decide which parent we should use based on VLAN setting.
parentName := network.GetHostDevice(d.config["parent"], d.config["vlan"])
statusDev, err := networkCreateVlanDeviceIfNeeded(d.state, d.config["parent"], parentName, d.config["vlan"])
if err != nil {
return nil, err
}
// Record whether we created this device or not so it can be removed on stop.
saveData["last_state.created"] = fmt.Sprintf("%t", statusDev != "existing")
// If we created a VLAN interface, we need to setup the sysctls on that interface.
if statusDev == "created" {
err := d.setupParentSysctls(parentName)
if err != nil {
return nil, err
}
}
err = d.volatileSet(saveData)
if err != nil {
return nil, err
}
runConf := deviceConfig.RunConfig{}
nic := []deviceConfig.RunConfigItem{
{Key: "name", Value: d.config["name"]},
{Key: "type", Value: "ipvlan"},
{Key: "flags", Value: "up"},
{Key: "ipvlan.mode", Value: "l3s"},
{Key: "ipvlan.isolation", Value: "bridge"},
{Key: "l2proxy", Value: "1"},
{Key: "link", Value: parentName},
}
if d.config["mtu"] != "" {
nic = append(nic, deviceConfig.RunConfigItem{Key: "mtu", Value: d.config["mtu"]})
}
if d.config["ipv4.address"] != "" {
for _, addr := range strings.Split(d.config["ipv4.address"], ",") {
addr = strings.TrimSpace(addr)
nic = append(nic, deviceConfig.RunConfigItem{Key: "ipv4.address", Value: fmt.Sprintf("%s/32", addr)})
}
nic = append(nic, deviceConfig.RunConfigItem{Key: "ipv4.gateway", Value: "dev"})
}
if d.config["ipv6.address"] != "" {
for _, addr := range strings.Split(d.config["ipv6.address"], ",") {
addr = strings.TrimSpace(addr)
nic = append(nic, deviceConfig.RunConfigItem{Key: "ipv6.address", Value: fmt.Sprintf("%s/128", addr)})
}
nic = append(nic, deviceConfig.RunConfigItem{Key: "ipv6.gateway", Value: "dev"})
}
runConf.NetworkInterface = nic
return &runConf, nil
}
// setupParentSysctls configures the required sysctls on the parent to allow l2proxy to work.
// Because of our policy not to modify sysctls on existing interfaces, this should only be called
// if we created the parent interface.
func (d *nicIPVLAN) setupParentSysctls(parentName string) error {
if d.config["ipv4.address"] != "" {
// Set necessary sysctls for use with l2proxy parent in IPVLAN l3s mode.
ipv4FwdPath := fmt.Sprintf("net/ipv4/conf/%s/forwarding", parentName)
err := util.SysctlSet(ipv4FwdPath, "1")
if err != nil {
return fmt.Errorf("Error setting net sysctl %s: %v", ipv4FwdPath, err)
}
}
if d.config["ipv6.address"] != "" {
// Set necessary sysctls use with l2proxy parent in IPVLAN l3s mode.
ipv6FwdPath := fmt.Sprintf("net/ipv6/conf/%s/forwarding", parentName)
err := util.SysctlSet(ipv6FwdPath, "1")
if err != nil {
return fmt.Errorf("Error reading net sysctl %s: %v", ipv6FwdPath, err)
}
ipv6ProxyNdpPath := fmt.Sprintf("net/ipv6/conf/%s/proxy_ndp", parentName)
err = util.SysctlSet(ipv6ProxyNdpPath, "1")
if err != nil {
return fmt.Errorf("Error reading net sysctl %s: %v", ipv6ProxyNdpPath, err)
}
}
return nil
}
// Stop is run when the device is removed from the instance.
func (d *nicIPVLAN) 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 *nicIPVLAN) postStop() error {
defer d.volatileSet(map[string]string{
"last_state.created": "",
})
v := d.volatileGet()
// This will delete the parent interface if we created it for VLAN parent.
if shared.IsTrue(v["last_state.created"]) {
parentName := network.GetHostDevice(d.config["parent"], d.config["vlan"])
err := networkRemoveInterfaceIfNeeded(d.state, parentName, d.inst, d.config["parent"], d.config["vlan"])
if err != nil {
return err
}
}
return nil
}