Skip to content

Commit 7b2acf3

Browse files
committed
incusd/ip: Merge GetLinkInfoByName and LinkFromName into LinkByName
Signed-off-by: Gwendolyn <[email protected]>
1 parent 8e74676 commit 7b2acf3

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
@@ -124,19 +132,19 @@ func (l *Link) netlinkAttrs() (netlink.LinkAttrs, error) {
124132
}, nil
125133
}
126134

127-
// LinkFromName returns a Link from a device name.
128-
func LinkFromName(name string) (*Link, error) {
135+
// LinkByName returns a Link from a device name.
136+
func LinkByName(name string) (LinkInfo, error) {
129137
link, err := linkByName(name)
130138
if err != nil {
131-
return nil, err
139+
return LinkInfo{}, err
132140
}
133141

134142
var parent, master string
135143

136144
if link.Attrs().ParentIndex != 0 {
137145
parentLink, err := netlink.LinkByIndex(link.Attrs().ParentIndex)
138146
if err != nil {
139-
return nil, err
147+
return LinkInfo{}, err
140148
}
141149

142150
parent = parentLink.Attrs().Name
@@ -145,22 +153,32 @@ func LinkFromName(name string) (*Link, error) {
145153
if link.Attrs().MasterIndex != 0 {
146154
masterLink, err := netlink.LinkByIndex(link.Attrs().MasterIndex)
147155
if err != nil {
148-
return nil, err
156+
return LinkInfo{}, err
149157
}
150158

151159
master = masterLink.Attrs().Name
152160
}
153161

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

internal/server/ip/utils.go

-67
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,6 @@ const (
1818
FamilyV6 Family = unix.AF_INET6
1919
)
2020

21-
// LinkInfo represents the IP link details.
22-
type LinkInfo struct {
23-
InterfaceName string
24-
Link string
25-
Master string
26-
Address string
27-
TXQueueLength uint32
28-
MTU uint32
29-
OperationalState string
30-
Info struct {
31-
Kind string
32-
SlaveKind string
33-
Data struct {
34-
Protocol string
35-
ID int
36-
}
37-
}
38-
}
39-
4021
func linkByName(name string) (netlink.Link, error) {
4122
link, err := netlink.LinkByName(name)
4223
if err != nil {
@@ -46,54 +27,6 @@ func linkByName(name string) (netlink.Link, error) {
4627
return link, nil
4728
}
4829

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