Skip to content

Commit

Permalink
loader: Remove duplicated Loader interface
Browse files Browse the repository at this point in the history
There was no pressing need for having two separate Loader interfaces.
Simplify things by just keeping the datapath types version.

Refactor DetachXDP() to take an interface name to avoid leaking netlink.Link
in the export methods. It anyway did a link lookup again anyway, so there was
no benefit to reusing the passed in Link.

Signed-off-by: Jussi Maki <jussi@isovalent.com>
  • Loading branch information
joamaki committed May 31, 2024
1 parent 7209275 commit 0a02d60
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 52 deletions.
2 changes: 1 addition & 1 deletion cilium-dbg/cmd/post_uninstall_cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func removeXDPAttachments(links []netlink.Link) error {
})

for _, link := range links {
if err := loader.DetachXDP(link, bpf.CiliumPath(), "cil_xdp_entry"); err != nil {
if err := loader.DetachXDP(link.Attrs().Name, bpf.CiliumPath(), "cil_xdp_entry"); err != nil {
return err
}
fmt.Printf("removed cilium xdp of %s\n", link.Attrs().Name)
Expand Down
3 changes: 1 addition & 2 deletions pkg/datapath/cells.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/cilium/cilium/pkg/datapath/linux/sysctl"
"github.com/cilium/cilium/pkg/datapath/linux/utime"
"github.com/cilium/cilium/pkg/datapath/loader"
loaderTypes "github.com/cilium/cilium/pkg/datapath/loader/types"
"github.com/cilium/cilium/pkg/datapath/orchestrator"
"github.com/cilium/cilium/pkg/datapath/prefilter"
"github.com/cilium/cilium/pkg/datapath/tables"
Expand Down Expand Up @@ -218,7 +217,7 @@ type datapathParams struct {

TunnelConfig tunnel.Config

Loader loaderTypes.Loader
Loader types.Loader

NodeManager nodeManager.NodeManager

Expand Down
2 changes: 2 additions & 0 deletions pkg/datapath/fake/cells.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/cilium/cilium/pkg/datapath/iptables/ipset"
"github.com/cilium/cilium/pkg/datapath/linux/bigtcp"
"github.com/cilium/cilium/pkg/datapath/linux/sysctl"
"github.com/cilium/cilium/pkg/datapath/loader"
"github.com/cilium/cilium/pkg/datapath/tables"
"github.com/cilium/cilium/pkg/datapath/tunnel"
"github.com/cilium/cilium/pkg/datapath/types"
Expand Down Expand Up @@ -54,6 +55,7 @@ var Cell = cell.Module(
func() mtu.MTU { return &fakeTypes.MTU{} },
func() *wg.Agent { return nil },
func() types.Loader { return &fakeTypes.FakeLoader{} },
loader.NewCompilationLock,
func() sysctl.Sysctl { return &Sysctl{} },

tables.NewDeviceTable,
Expand Down
4 changes: 4 additions & 0 deletions pkg/datapath/fake/types/datapath.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ func (f *FakeLoader) RestoreTemplates(stateDir string) error {
return nil
}

func (f *FakeLoader) DetachXDP(ifaceName string, bpffsBase, progName string) error {
return nil
}

type FakeOrchestrator struct{}

func (f *FakeOrchestrator) Reinitialize(ctx context.Context) error {
Expand Down
5 changes: 2 additions & 3 deletions pkg/datapath/linux/datapath.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package linux
import (
"github.com/cilium/statedb"

loader "github.com/cilium/cilium/pkg/datapath/loader/types"
"github.com/cilium/cilium/pkg/datapath/tables"
datapath "github.com/cilium/cilium/pkg/datapath/types"
"github.com/cilium/cilium/pkg/maps/lbmap"
Expand All @@ -31,7 +30,7 @@ type linuxDatapath struct {
nodeIDHandler datapath.NodeIDHandler
nodeNeighbors datapath.NodeNeighbors
nodeAddressing datapath.NodeAddressing
loader loader.Loader
loader datapath.Loader
wgAgent datapath.WireguardAgent
lbmap datapath.LBMap
bwmgr datapath.BandwidthManager
Expand All @@ -46,7 +45,7 @@ type DatapathParams struct {
BWManager datapath.BandwidthManager
NodeAddressing datapath.NodeAddressing
MTU datapath.MTUConfiguration
Loader loader.Loader
Loader datapath.Loader
NodeManager manager.NodeManager
DB *statedb.DB
Devices statedb.Table[*tables.Device]
Expand Down
6 changes: 3 additions & 3 deletions pkg/datapath/loader/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/cilium/hive/cell"
"github.com/spf13/pflag"

loaderTypes "github.com/cilium/cilium/pkg/datapath/loader/types"
datapath "github.com/cilium/cilium/pkg/datapath/types"
)

var Cell = cell.Module(
Expand All @@ -16,11 +16,11 @@ var Cell = cell.Module(

cell.Config(DefaultConfig),
cell.Provide(NewLoader),
cell.Provide(newCompilationLock),
cell.Provide(NewCompilationLock),
)

// NewLoader returns a new loader.
func NewLoader(p Params) loaderTypes.Loader {
func NewLoader(p Params) datapath.Loader {
return newLoader(p)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/datapath/loader/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,6 @@ type compilationLock struct {
lock.RWMutex
}

func newCompilationLock() types.CompilationLock {
func NewCompilationLock() types.CompilationLock {
return &compilationLock{}
}
27 changes: 0 additions & 27 deletions pkg/datapath/loader/types/types.go

This file was deleted.

17 changes: 8 additions & 9 deletions pkg/datapath/loader/xdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (l *loader) maybeUnloadObsoleteXDPPrograms(xdpDevs []string, xdpMode, bpffs
}
}
if !used {
if err := l.DetachXDP(link, bpffsBase, symbolFromHostNetdevXDP); err != nil {
if err := l.DetachXDP(link.Attrs().Name, bpffsBase, symbolFromHostNetdevXDP); err != nil {
log.WithError(err).Warn("Failed to detach obsolete XDP program")
}
}
Expand Down Expand Up @@ -276,9 +276,14 @@ func attachXDPProgram(iface netlink.Link, prog *ebpf.Program, progName, bpffsDir
//
// bpffsBase is typically /sys/fs/bpf/cilium, but can be overridden to a tempdir
// during tests.
func (l *loader) DetachXDP(iface netlink.Link, bpffsBase, progName string) error {
func (l *loader) DetachXDP(ifaceName string, bpffsBase, progName string) error {
iface, err := netlink.LinkByName(ifaceName)
if err != nil {
return fmt.Errorf("getting link '%s' by name: %w", ifaceName, err)
}

pin := filepath.Join(bpffsDeviceLinksDir(bpffsBase, iface), progName)
err := bpf.UnpinLink(pin)
err = bpf.UnpinLink(pin)
if err == nil {
return nil
}
Expand All @@ -287,12 +292,6 @@ func (l *loader) DetachXDP(iface netlink.Link, bpffsBase, progName string) error
return fmt.Errorf("unpinning XDP program using bpf_link: %w", err)
}

// Refresh the link info since the caller may have constructed iface manually.
iface, err = netlink.LinkByName(iface.Attrs().Name)
if err != nil {
return fmt.Errorf("getting link '%s' by name: %w", iface.Attrs().Name, err)
}

xdp := iface.Attrs().Xdp
if xdp == nil || !xdp.Attached {
return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/datapath/loader/xdp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestAttachXDPWithExistingLink(t *testing.T) {
require.NoError(t, err)

// Detach the program.
err = newTestLoader(t).DetachXDP(veth, basePath, "test")
err = newTestLoader(t).DetachXDP(veth.Attrs().Name, basePath, "test")
require.NoError(t, err)

err = netlink.LinkDel(veth)
Expand Down Expand Up @@ -214,11 +214,11 @@ func TestDetachXDPWithPreviousAttach(t *testing.T) {
require.True(t, getLink(t, veth).Attrs().Xdp.Attached)

// Detach with the wrong name, leaving the program attached.
err = newTestLoader(t).DetachXDP(veth, basePath, "foo")
err = newTestLoader(t).DetachXDP(veth.Attrs().Name, basePath, "foo")
require.NoError(t, err)
require.True(t, getLink(t, veth).Attrs().Xdp.Attached)

err = newTestLoader(t).DetachXDP(veth, basePath, "test")
err = newTestLoader(t).DetachXDP(veth.Attrs().Name, basePath, "test")
require.NoError(t, err)
require.False(t, getLink(t, veth).Attrs().Xdp.Attached)

Expand Down
2 changes: 1 addition & 1 deletion pkg/datapath/orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/cilium/hive/cell"

"github.com/cilium/cilium/pkg/datapath/iptables"
"github.com/cilium/cilium/pkg/datapath/loader/types"
"github.com/cilium/cilium/pkg/datapath/tunnel"
"github.com/cilium/cilium/pkg/datapath/types"
"github.com/cilium/cilium/pkg/mtu"
"github.com/cilium/cilium/pkg/proxy"
)
Expand Down
1 change: 1 addition & 0 deletions pkg/datapath/types/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Loader interface {
Reinitialize(ctx context.Context, tunnelConfig tunnel.Config, deviceMTU int, iptMgr IptablesManager, p Proxy) error
HostDatapathInitialized() <-chan struct{}
RestoreTemplates(stateDir string) error
DetachXDP(iface string, bpffsBase, progName string) error
}

// PreFilter an interface for an XDP pre-filter.
Expand Down
2 changes: 0 additions & 2 deletions test/controlplane/suite/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
fakecni "github.com/cilium/cilium/daemon/cmd/cni/fake"
fakeDatapath "github.com/cilium/cilium/pkg/datapath/fake"
fakeTypes "github.com/cilium/cilium/pkg/datapath/fake/types"
"github.com/cilium/cilium/pkg/datapath/loader"
"github.com/cilium/cilium/pkg/datapath/prefilter"
datapathTables "github.com/cilium/cilium/pkg/datapath/tables"
fqdnproxy "github.com/cilium/cilium/pkg/fqdn/proxy"
Expand Down Expand Up @@ -78,7 +77,6 @@ func (h *agentHandle) setupCiliumAgentHive(clientset k8sClient.Clientset, extraC
),
fakeDatapath.Cell,
prefilter.Cell,
loader.Cell,
monitorAgent.Cell,
metrics.Cell,
store.Cell,
Expand Down

0 comments on commit 0a02d60

Please sign in to comment.