Skip to content

Commit f5946af

Browse files
committed
gneigh: add interface name and hardware address accessor methods
Add the Interface.Name and Interface.HardwareAddr to simplify access to the respective fields of the underlying *net.Interface. This reduces stutter in the access to these attributes in existing code and also allows users outside the package to access them. Signed-off-by: Tobias Klauser <tobias@cilium.io>
1 parent 28b375c commit f5946af

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

pkg/datapath/gneigh/gneigh.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ type Interface struct {
6262
iface *net.Interface
6363
}
6464

65+
// Name returns the interface name.
66+
func (i Interface) Name() string {
67+
return i.iface.Name
68+
}
69+
70+
// HardwareAddr returns the interface hardware address.
71+
func (i Interface) HardwareAddr() net.HardwareAddr {
72+
return i.iface.HardwareAddr
73+
}
74+
6575
// InterfaceFromNetInterface constructs an Interface from the given *net.Interface.
6676
func InterfaceFromNetInterface(iface *net.Interface) Interface {
6777
return Interface{iface: iface}
@@ -90,7 +100,7 @@ func (s *sender) NewArpSender(iface Interface) (ArpSender, error) {
90100

91101
return &arpSender{
92102
cl: cl,
93-
srcHW: iface.iface.HardwareAddr,
103+
srcHW: iface.HardwareAddr(),
94104
}, nil
95105
}
96106

@@ -123,7 +133,7 @@ func (s *sender) NewNdSender(iface Interface) (NdSender, error) {
123133

124134
return &ndSender{
125135
cl: cl,
126-
srcHW: iface.iface.HardwareAddr,
136+
srcHW: iface.HardwareAddr(),
127137
}, nil
128138
}
129139

pkg/datapath/gneigh/processor_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func TestProcessorSingleInterface(t *testing.T) {
194194
garps := collect(garpSent)
195195
require.Len(t, garps, 1)
196196
require.Equal(t, garps[0].addr.String(), ep1.IPv4.String())
197-
require.Equal(t, garps[0].iface.iface.Name, cfg.L2PodAnnouncementsInterface)
197+
require.Equal(t, garps[0].iface.Name(), cfg.L2PodAnnouncementsInterface)
198198

199199
// On second event we expect no GARP to be sent.
200200
proc.EndpointCreated(ep1)
@@ -205,7 +205,7 @@ func TestProcessorSingleInterface(t *testing.T) {
205205
garps = collect(garpSent)
206206
require.Len(t, garps, 1)
207207
require.Equal(t, garps[0].addr.String(), ep2.IPv4.String())
208-
require.Equal(t, garps[0].iface.iface.Name, cfg.L2PodAnnouncementsInterface)
208+
require.Equal(t, garps[0].iface.Name(), cfg.L2PodAnnouncementsInterface)
209209

210210
// Second event for second endpoint should not trigger a GARP.
211211
proc.EndpointCreated(ep2)
@@ -220,7 +220,7 @@ func TestProcessorSingleInterface(t *testing.T) {
220220
garps = collect(garpSent)
221221
require.Len(t, garps, 1)
222222
require.Equal(t, garps[0].addr.String(), ep1.IPv4.String())
223-
require.Equal(t, garps[0].iface.iface.Name, cfg.L2PodAnnouncementsInterface)
223+
require.Equal(t, garps[0].iface.Name(), cfg.L2PodAnnouncementsInterface)
224224

225225
// But GARP should still not be set for recreated ep2.
226226
proc.EndpointCreated(ep2)
@@ -241,8 +241,8 @@ func TestProcessorHappyPathMultipleInterface(t *testing.T) {
241241
garps := collect(garpSent)
242242
require.Len(t, garps, 2)
243243
require.Equal(t, garps[0].addr.String(), ep1.IPv4.String())
244-
gotEth0 := garps[0].iface.iface.Name == "eth0" || garps[1].iface.iface.Name == "eth0"
245-
gotEns1 := garps[0].iface.iface.Name == "ens1" || garps[1].iface.iface.Name == "ens1"
244+
gotEth0 := garps[0].iface.Name() == "eth0" || garps[1].iface.Name() == "eth0"
245+
gotEns1 := garps[0].iface.Name() == "ens1" || garps[1].iface.Name() == "ens1"
246246
if !(gotEth0 && gotEns1) {
247247
t.Fatalf("Expected GARP to be sent on both eth0 and ens1, got: %v", garps)
248248
}
@@ -261,7 +261,7 @@ func TestProcessorOnlySelected(t *testing.T) {
261261
garps := collect(garpSent)
262262
for _, d := range fakeDevices {
263263
contains := slices.ContainsFunc(garps, func(g fakeGarp) bool {
264-
return g.iface.iface.Name == d.Name
264+
return g.iface.Name() == d.Name
265265
})
266266
if d.Selected && !contains {
267267
t.Fatalf("Expected GARP to be sent on selected interface %s", d.Name)

0 commit comments

Comments
 (0)