Skip to content

Commit 6c18c08

Browse files
committed
incusd/ip: Merge GetLinkInfoByName and LinkFromName into LinkByName
Signed-off-by: Gwendolyn <[email protected]>
1 parent bd77a4d commit 6c18c08

File tree

4 files changed

+38
-87
lines changed

4 files changed

+38
-87
lines changed

internal/server/ip/link.go

+33-15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ type Link struct {
2929
Up bool
3030
}
3131

32+
// LinkInfo has additional information about a link.
33+
type LinkInfo struct {
34+
Link
35+
OperationalState string
36+
SlaveKind string
37+
VlanID int
38+
}
39+
3240
// args generate common arguments for the virtual link.
3341
func (l *Link) args() []string {
3442
var result []string
@@ -128,19 +136,19 @@ func (l *Link) netlinkAttrs() (netlink.LinkAttrs, error) {
128136
return linkAttrs, nil
129137
}
130138

131-
// LinkFromName returns a Link from a device name.
132-
func LinkFromName(name string) (*Link, error) {
139+
// LinkByName returns a Link from a device name.
140+
func LinkByName(name string) (LinkInfo, error) {
133141
link, err := linkByName(name)
134142
if err != nil {
135-
return nil, err
143+
return LinkInfo{}, err
136144
}
137145

138146
var parent, master string
139147

140148
if link.Attrs().ParentIndex != 0 {
141149
parentLink, err := netlink.LinkByIndex(link.Attrs().ParentIndex)
142150
if err != nil {
143-
return nil, err
151+
return LinkInfo{}, err
144152
}
145153

146154
parent = parentLink.Attrs().Name
@@ -149,22 +157,32 @@ func LinkFromName(name string) (*Link, error) {
149157
if link.Attrs().MasterIndex != 0 {
150158
masterLink, err := netlink.LinkByIndex(link.Attrs().MasterIndex)
151159
if err != nil {
152-
return nil, err
160+
return LinkInfo{}, err
153161
}
154162

155163
master = masterLink.Attrs().Name
156164
}
157165

158-
return &Link{
159-
Name: link.Attrs().Name,
160-
Kind: link.Type(),
161-
MTU: uint32(link.Attrs().MTU),
162-
Parent: parent,
163-
Address: link.Attrs().HardwareAddr,
164-
TXQueueLength: uint32(link.Attrs().TxQLen),
165-
AllMulticast: link.Attrs().Allmulti == 1,
166-
Master: master,
167-
Up: (link.Attrs().Flags & net.FlagUp) != 0,
166+
var vlanID int
167+
vlan, ok := link.(*netlink.Vlan)
168+
if ok {
169+
vlanID = vlan.VlanId
170+
}
171+
172+
return LinkInfo{
173+
Link: Link{
174+
Name: link.Attrs().Name,
175+
Kind: link.Type(),
176+
MTU: uint32(link.Attrs().MTU),
177+
Parent: parent,
178+
Address: link.Attrs().HardwareAddr,
179+
TXQueueLength: uint32(link.Attrs().TxQLen),
180+
AllMulticast: link.Attrs().Allmulti == 1,
181+
Master: master,
182+
Up: (link.Attrs().Flags & net.FlagUp) != 0,
183+
},
184+
OperationalState: link.Attrs().OperState.String(),
185+
VlanID: vlanID,
168186
}, nil
169187
}
170188

internal/server/ip/utils.go

-67
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,6 @@ const (
2323
FamilyV6 Family = unix.AF_INET6
2424
)
2525

26-
// LinkInfo represents the IP link details.
27-
type LinkInfo struct {
28-
InterfaceName string
29-
Link string
30-
Master string
31-
Address string
32-
TXQueueLength uint32
33-
MTU uint32
34-
OperationalState string
35-
Info struct {
36-
Kind string
37-
SlaveKind string
38-
Data struct {
39-
Protocol string
40-
ID int
41-
}
42-
}
43-
}
44-
4526
func linkByName(name string) (netlink.Link, error) {
4627
link, err := netlink.LinkByName(name)
4728
if err != nil {
@@ -51,54 +32,6 @@ func linkByName(name string) (netlink.Link, error) {
5132
return link, nil
5233
}
5334

54-
// GetLinkInfoByName returns the detailed information for the given link.
55-
func GetLinkInfoByName(name string) (LinkInfo, error) {
56-
info := LinkInfo{}
57-
58-
link, err := linkByName(name)
59-
if err != nil {
60-
return info, err
61-
}
62-
63-
info.InterfaceName = link.Attrs().Name
64-
65-
if link.Attrs().ParentIndex != 0 {
66-
parentLink, err := netlink.LinkByIndex(link.Attrs().ParentIndex)
67-
if err != nil {
68-
return info, fmt.Errorf("failed to get parent link %d of %q: %w", link.Attrs().ParentIndex, name, err)
69-
}
70-
71-
info.Link = parentLink.Attrs().Name
72-
}
73-
74-
if link.Attrs().MasterIndex != 0 {
75-
masterLink, err := netlink.LinkByIndex(link.Attrs().MasterIndex)
76-
if err != nil {
77-
return info, fmt.Errorf("failed to get master link %d of %q: %w", link.Attrs().ParentIndex, name, err)
78-
}
79-
80-
info.Master = masterLink.Attrs().Name
81-
}
82-
83-
info.Address = link.Attrs().HardwareAddr.String()
84-
info.TXQueueLength = uint32(link.Attrs().TxQLen)
85-
info.MTU = uint32(link.Attrs().MTU)
86-
info.OperationalState = link.Attrs().OperState.String()
87-
info.Info.Kind = link.Type()
88-
89-
if link.Attrs().Slave != nil {
90-
info.Info.Kind = link.Attrs().Slave.SlaveType()
91-
}
92-
93-
vlan, ok := link.(*netlink.Vlan)
94-
if ok {
95-
info.Info.Data.ID = vlan.VlanId
96-
info.Info.Data.Protocol = vlan.VlanProtocol.String()
97-
}
98-
99-
return info, nil
100-
}
101-
10235
func parseHandle(id string) (uint32, error) {
10336
if id == "root" {
10437
return netlink.HANDLE_ROOT, nil

internal/server/network/driver_bridge.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1121,12 +1121,12 @@ func (n *bridge) setup(oldConfig map[string]string) error {
11211121
}
11221122
} else if vlanID > 0 {
11231123
// If the interface exists and VLAN ID was provided, ensure it has the same parent and VLAN ID and is not attached to a different network.
1124-
linkInfo, err := ip.GetLinkInfoByName(entry)
1124+
linkInfo, err := ip.LinkByName(entry)
11251125
if err != nil {
11261126
return fmt.Errorf("Failed to get link info for external interface %q", entry)
11271127
}
11281128

1129-
if linkInfo.Info.Kind != "vlan" || linkInfo.Link != ifParent || linkInfo.Info.Data.ID != vlanID || !(linkInfo.Master == "" || linkInfo.Master == n.name) {
1129+
if linkInfo.Kind != "vlan" || linkInfo.Parent != ifParent || linkInfo.VlanID != vlanID || (linkInfo.Master != "" && linkInfo.Master != n.name) {
11301130
return fmt.Errorf("External interface %q already in use", entry)
11311131
}
11321132
}
@@ -3129,7 +3129,7 @@ func (n *bridge) deleteChildren() error {
31293129
}
31303130

31313131
for _, iface := range ifaces {
3132-
l, err := ip.LinkFromName(iface.Name)
3132+
l, err := ip.LinkByName(iface.Name)
31333133
if err != nil {
31343134
return err
31353135
}

internal/server/network/driver_ovn.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2491,12 +2491,12 @@ func (n *ovn) setup(update bool) error {
24912491
}
24922492
} else if vlanID > 0 {
24932493
// If the interface exists and VLAN ID was provided, ensure it has the same parent and VLAN ID and is not attached to a different network.
2494-
linkInfo, err := ip.GetLinkInfoByName(entry)
2494+
linkInfo, err := ip.LinkByName(entry)
24952495
if err != nil {
24962496
return fmt.Errorf("Failed to get link info for external interface %q", entry)
24972497
}
24982498

2499-
if linkInfo.Info.Kind != "vlan" || linkInfo.Link != ifParent || linkInfo.Info.Data.ID != vlanID || !(linkInfo.Master == "" || linkInfo.Master == n.name) {
2499+
if linkInfo.Kind != "vlan" || linkInfo.Parent != ifParent || linkInfo.VlanID != vlanID || (linkInfo.Master != "" && linkInfo.Master != n.name) {
25002500
return fmt.Errorf("External interface %q already in use", entry)
25012501
}
25022502
}

0 commit comments

Comments
 (0)