Skip to content

Commit

Permalink
Network: Parse host physical NIC MAC address for storing in device co…
Browse files Browse the repository at this point in the history
…nfiguration (#14905)

Fixes #14879.

This PR adds parsing of the host physical NIC MAC address, allowing
`agent.nic_config` to be set to `true` for physical NIC devices. I've
also updated the metadata and included a small completion fix for a bug
I discovered while testing this.

Summary of changes:
- Fixes conditional completions for `lxc config device add`.
- Updates metadata for physical NIC `hwaddr` config key.
- Updates the physical NIC `Start` method to retrieve host physical NIC
MAC address and store in device config (for use in `addNetDevConfig` ->
`writeNICDevConfig`).
  • Loading branch information
tomponline authored Feb 3, 2025
2 parents 35f7005 + 0c41189 commit b98b242
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ This option specifies whether to register the VLAN using the GARP VLAN Registrat
```

```{config:option} hwaddr device-nic-physical-device-conf
:defaultdesc: "randomly assigned"
:defaultdesc: "parent MAC address"
:managed: "no"
:shortdesc: "MAC address of the new interface"
:type: "string"
Expand Down
7 changes: 4 additions & 3 deletions lxc/config_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,13 @@ lxc profile device add [<remote>:]profile1 <device-name> disk pool=some-pool sou
}
}

if len(args) == 1 {
// The second positional argument is used for the device name, so we provide device completions for the third positional argument.
if len(args) == 2 {
return c.global.cmpInstanceAllDevices(toComplete)
}

if len(args) == 2 {
return c.global.cmpInstanceAllDeviceOptions(args[0], args[1])
if len(args) == 3 {
return c.global.cmpInstanceAllDeviceOptions(args[0], args[2])
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
10 changes: 9 additions & 1 deletion lxd/device/nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,22 @@ func nicValidationRules(requiredFields []string, optionalFields []string, instCo
// defaultdesc: `false`
// shortdesc: Whether to use GARP VLAN Registration Protocol
"gvrp": validate.Optional(validate.IsBool),
// lxdmeta:generate(entities=device-nic-{bridged+macvlan+sriov+physical+ovn}; group=device-conf; key=hwaddr)
// lxdmeta:generate(entities=device-nic-{bridged+macvlan+sriov+ovn}; group=device-conf; key=hwaddr)
//
// ---
// type: string
// defaultdesc: randomly assigned
// managed: no
// shortdesc: MAC address of the new interface

// lxdmeta:generate(entities=device-nic-physical; group=device-conf; key=hwaddr)
//
// ---
// type: string
// defaultdesc: parent MAC address
// managed: no
// shortdesc: MAC address of the new interface

// lxdmeta:generate(entities=device-nic-{ipvlan+p2p+routed}; group=device-conf; key=hwaddr)
//
// ---
Expand Down
9 changes: 9 additions & 0 deletions lxd/device/nic_physical.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ func (d *nicPhysical) Start() (*deviceConfig.RunConfig, error) {
// pciIOMMUGroup, used for VM physical passthrough.
var pciIOMMUGroup uint64

var hwaddr string

// If VM, then try and load the vfio-pci module first.
if d.inst.Type() == instancetype.VM {
err = util.LoadModule("vfio-pci")
Expand Down Expand Up @@ -200,6 +202,12 @@ func (d *nicPhysical) Start() (*deviceConfig.RunConfig, error) {
}
}
} else if d.inst.Type() == instancetype.VM {
// Try to get MAC address of the parent interface.
hwaddr, err = NetworkGetDevMAC(saveData["host_name"])
if err != nil {
return nil, err
}

// Try to get PCI information about the network interface.
ueventPath := fmt.Sprintf("/sys/class/net/%s/device/uevent", saveData["host_name"])
pciDev, err := pcidev.ParseUeventFile(ueventPath)
Expand Down Expand Up @@ -245,6 +253,7 @@ func (d *nicPhysical) Start() (*deviceConfig.RunConfig, error) {
{Key: "devName", Value: d.name},
{Key: "pciSlotName", Value: saveData["last_state.pci.slot.name"]},
{Key: "pciIOMMUGroup", Value: fmt.Sprintf("%d", pciIOMMUGroup)},
{Key: "hwaddr", Value: hwaddr},
}...)
}

Expand Down
2 changes: 1 addition & 1 deletion lxd/metadata/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@
},
{
"hwaddr": {
"defaultdesc": "randomly assigned",
"defaultdesc": "parent MAC address",
"longdesc": "",
"managed": "no",
"shortdesc": "MAC address of the new interface",
Expand Down

0 comments on commit b98b242

Please sign in to comment.