Skip to content

Commit 07452f7

Browse files
authored
Merge pull request #2130 from NathanChase22/simplifiied-bridge-as-parent
Handle attaching instances to managed physical networks backed by bridges
2 parents 4ee508b + 16a276a commit 07452f7

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

internal/server/device/nic_physical.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ func (d *nicPhysical) validateConfig(instConf instance.ConfigReader) error {
130130
// Get actual parent device from network's parent setting.
131131
d.config["parent"] = netConfig["parent"]
132132

133+
// If parent is a bridge, ensure it's managed.
134+
isParentBridge := d.config["parent"] != "" && util.PathExists(fmt.Sprintf("/sys/class/net/%s/bridge", d.config["parent"]))
135+
if isParentBridge && d.network == nil {
136+
return fmt.Errorf("Parent device is a bridge, use nictype=bridged instead")
137+
}
138+
133139
// Copy certain keys verbatim from the network's settings.
134140
for _, field := range optionalFields {
135141
_, found := netConfig[field]
@@ -174,6 +180,25 @@ func (d *nicPhysical) Start() (*deviceConfig.RunConfig, error) {
174180
return nil, err
175181
}
176182

183+
// Handle the case where the parent is a bridge.
184+
isParentBridge := util.PathExists(fmt.Sprintf("/sys/class/net/%s/bridge", d.config["parent"]))
185+
if isParentBridge {
186+
// Convert the device to a nictype=bridged internally.
187+
bridgedConfig := d.config.Clone()
188+
bridgedConfig["type"] = "nic"
189+
bridgedConfig["nictype"] = "bridged"
190+
bridgedConfig["network"] = ""
191+
192+
// Instantiate the new device.
193+
bridged, err := load(d.inst, d.state, d.inst.Project().Name, d.inst.Name(), bridgedConfig, d.volatileGet, d.volatileSet)
194+
if err != nil {
195+
return nil, fmt.Errorf("Failed to initialize bridged device: %w", err)
196+
}
197+
198+
// Forward the start call.
199+
return bridged.Start()
200+
}
201+
177202
// Lock to avoid issues with containers starting in parallel.
178203
networkCreateSharedDeviceLock.Lock()
179204
defer networkCreateSharedDeviceLock.Unlock()
@@ -374,6 +399,25 @@ func (d *nicPhysical) startVMUSB(name string) (*deviceConfig.RunConfig, error) {
374399

375400
// Stop is run when the device is removed from the instance.
376401
func (d *nicPhysical) Stop() (*deviceConfig.RunConfig, error) {
402+
// Handle the case where the parent is a bridge.
403+
isParentBridge := util.PathExists(fmt.Sprintf("/sys/class/net/%s/bridge", d.config["parent"]))
404+
if isParentBridge {
405+
// Convert the device to a nictype=bridged internally.
406+
bridgedConfig := d.config.Clone()
407+
bridgedConfig["type"] = "nic"
408+
bridgedConfig["nictype"] = "bridged"
409+
bridgedConfig["network"] = ""
410+
411+
// Instantiate the new device.
412+
bridged, err := load(d.inst, d.state, d.inst.Project().Name, d.inst.Name(), bridgedConfig, d.volatileGet, d.volatileSet)
413+
if err != nil {
414+
return nil, fmt.Errorf("Failed to initialize bridged device: %w", err)
415+
}
416+
417+
// Forward the stop call.
418+
return bridged.Stop()
419+
}
420+
377421
v := d.volatileGet()
378422

379423
runConf := deviceConfig.RunConfig{

0 commit comments

Comments
 (0)