Skip to content

Commit c7041b2

Browse files
authored
add support for specifying the anchor to use when tcx attaching programs to ingress and egress (#822)
Signed-off-by: Alex Price <[email protected]>
1 parent fda361c commit c7041b2

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

pkg/agent/agent.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ func FlowsAgent(cfg *config.Agent) (*Flows, error) {
161161
ebpfConfig := &tracer.FlowFetcherConfig{
162162
EnableIngress: ingress,
163163
EnableEgress: egress,
164+
IngressTCXAnchor: cfg.TCXAttachAnchorIngress,
165+
EgressTCXAnchor: cfg.TCXAttachAnchorEgress,
164166
Debug: debug,
165167
Sampling: cfg.Sampling,
166168
CacheMaxSize: cfg.CacheMaxFlows,

pkg/agent/packets_agent.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,16 @@ func PacketsAgent(cfg *config.Agent) (*Packets, error) {
9090
})
9191
}
9292
ebpfConfig := &tracer.FlowFetcherConfig{
93-
EnableIngress: ingress,
94-
EnableEgress: egress,
95-
Debug: debug,
96-
Sampling: cfg.Sampling,
97-
CacheMaxSize: cfg.CacheMaxFlows,
98-
EnablePCA: cfg.EnablePCA,
99-
UseEbpfManager: cfg.EbpfProgramManagerMode,
100-
FilterConfig: filterRules,
93+
EnableIngress: ingress,
94+
EnableEgress: egress,
95+
IngressTCXAnchor: cfg.TCXAttachAnchorIngress,
96+
EgressTCXAnchor: cfg.TCXAttachAnchorEgress,
97+
Debug: debug,
98+
Sampling: cfg.Sampling,
99+
CacheMaxSize: cfg.CacheMaxFlows,
100+
EnablePCA: cfg.EnablePCA,
101+
UseEbpfManager: cfg.EbpfProgramManagerMode,
102+
FilterConfig: filterRules,
101103
}
102104

103105
fetcher, err := tracer.NewPacketFetcher(ebpfConfig)

pkg/config/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ type Agent struct {
147147
// TCAttachRetries defines the number of retries in case of attach/detach failures.
148148
// Valid only for 'tc' and 'tcx' attach modes.
149149
TCAttachRetries int `env:"TC_ATTACH_RETRIES" envDefault:"4"`
150+
// TCXAttachAnchorIngress defines the anchor to use when attaching eBPF programs to interfaces using tcx mode for
151+
// ingress.
152+
// none (default): no specific anchor is used and the eBPF program is generally inserted at the end.
153+
// head: eBPF program is inserted at the head.
154+
// tail: eBPF program is inserted at the tail.
155+
TCXAttachAnchorIngress string `env:"TCX_ATTACH_ANCHOR_INGRESS" envDefault:"none"`
156+
// TCXAttachAnchorEgress defines the anchor to use when attaching eBPF programs to interfaces using tcx mode for
157+
// egress.
158+
// none (default): no specific anchor is used and the eBPF program is generally inserted at the end.
159+
// head: eBPF program is inserted at the head.
160+
// tail: eBPF program is inserted at the tail.
161+
TCXAttachAnchorEgress string `env:"TCX_ATTACH_ANCHOR_EGRESS" envDefault:"none"`
150162
// ListenInterfaces specifies the mechanism used by the agent to listen for added or removed
151163
// network interfaces. Accepted values are "watch" (default) or "poll".
152164
// If the value is "watch", interfaces are traced immediately after they are created. This is

pkg/tracer/tracer.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ const (
6565
constEnableIPsec = "enable_ipsec"
6666
)
6767

68+
const (
69+
tcxAnchorNone = "none"
70+
tcxAnchorHead = "head"
71+
tcxAnchorTail = "tail"
72+
)
73+
6874
var log = logrus.WithField("component", "ebpf.FlowFetcher")
6975
var plog = logrus.WithField("component", "ebpf.PacketFetcher")
7076

@@ -86,6 +92,8 @@ type FlowFetcher struct {
8692
rttKprobeLink link.Link
8793
egressTCXLink map[ifaces.InterfaceKey]link.Link
8894
ingressTCXLink map[ifaces.InterfaceKey]link.Link
95+
egressTCXAnchor link.Anchor
96+
ingressTCXAnchor link.Anchor
8997
networkEventsMonitoringLink link.Link
9098
nfNatManIPLink link.Link
9199
xfrmInputKretProbeLink link.Link
@@ -100,6 +108,8 @@ type FlowFetcher struct {
100108
type FlowFetcherConfig struct {
101109
EnableIngress bool
102110
EnableEgress bool
111+
IngressTCXAnchor string
112+
EgressTCXAnchor string
103113
Debug bool
104114
Sampling int
105115
CacheMaxSize int
@@ -369,6 +379,8 @@ func NewFlowFetcher(cfg *FlowFetcherConfig, m *metrics.Metrics) (*FlowFetcher, e
369379
xfrmOutputKProbeLink: xfrmOutputKProbeLink,
370380
egressTCXLink: egressTCXLink,
371381
ingressTCXLink: ingressTCXLink,
382+
egressTCXAnchor: tcxAnchor(cfg.EgressTCXAnchor),
383+
ingressTCXAnchor: tcxAnchor(cfg.IngressTCXAnchor),
372384
networkEventsMonitoringLink: networkEventsMonitoringLink,
373385
lookupAndDeleteSupported: true, // this will be turned off later if found to be not supported
374386
useEbpfManager: cfg.UseEbpfManager,
@@ -378,15 +390,15 @@ func NewFlowFetcher(cfg *FlowFetcherConfig, m *metrics.Metrics) (*FlowFetcher, e
378390

379391
func (m *FlowFetcher) AttachTCX(iface *ifaces.Interface) error {
380392
if m.enableEgress {
381-
egrLink, err := m.attachTCXOnDirection(iface, "Egress", m.objects.BpfPrograms.TcxEgressFlowParse, cilium.AttachTCXEgress)
393+
egrLink, err := m.attachTCXOnDirection(iface, "Egress", m.objects.BpfPrograms.TcxEgressFlowParse, cilium.AttachTCXEgress, m.egressTCXAnchor)
382394
if err != nil {
383395
return err
384396
}
385397
m.egressTCXLink[iface.InterfaceKey] = egrLink
386398
}
387399

388400
if m.enableIngress {
389-
ingLink, err := m.attachTCXOnDirection(iface, "Ingress", m.objects.BpfPrograms.TcxIngressFlowParse, cilium.AttachTCXIngress)
401+
ingLink, err := m.attachTCXOnDirection(iface, "Ingress", m.objects.BpfPrograms.TcxIngressFlowParse, cilium.AttachTCXIngress, m.ingressTCXAnchor)
390402
if err != nil {
391403
return err
392404
}
@@ -396,13 +408,14 @@ func (m *FlowFetcher) AttachTCX(iface *ifaces.Interface) error {
396408
return nil
397409
}
398410

399-
func (m *FlowFetcher) attachTCXOnDirection(iface *ifaces.Interface, dirName string, prg *cilium.Program, attach cilium.AttachType) (link.Link, error) {
411+
func (m *FlowFetcher) attachTCXOnDirection(iface *ifaces.Interface, dirName string, prg *cilium.Program, attach cilium.AttachType, anchor link.Anchor) (link.Link, error) {
400412
ilog := log.WithField("iface", iface)
401413

402414
lnk, err := link.AttachTCX(link.TCXOptions{
403415
Program: prg,
404416
Attach: attach,
405417
Interface: iface.Index,
418+
Anchor: anchor,
406419
})
407420
if err != nil {
408421
errPrefix := "Attach" + dirName
@@ -1357,6 +1370,8 @@ type PacketFetcher struct {
13571370
cacheMaxSize int
13581371
enableIngress bool
13591372
enableEgress bool
1373+
ingressAnchor link.Anchor
1374+
egressAnchor link.Anchor
13601375
egressTCXLink map[ifaces.InterfaceKey]link.Link
13611376
ingressTCXLink map[ifaces.InterfaceKey]link.Link
13621377
lookupAndDeleteSupported bool
@@ -1605,6 +1620,7 @@ func (p *PacketFetcher) AttachTCX(iface *ifaces.Interface) error {
16051620
Program: p.objects.BpfPrograms.TcxEgressPcaParse,
16061621
Attach: cilium.AttachTCXEgress,
16071622
Interface: iface.Index,
1623+
Anchor: p.egressAnchor,
16081624
})
16091625
if err != nil {
16101626
if errors.Is(err, fs.ErrExist) {
@@ -1640,6 +1656,7 @@ func (p *PacketFetcher) AttachTCX(iface *ifaces.Interface) error {
16401656
Program: p.objects.BpfPrograms.TcxIngressPcaParse,
16411657
Attach: cilium.AttachTCXIngress,
16421658
Interface: iface.Index,
1659+
Anchor: p.ingressAnchor,
16431660
})
16441661
if err != nil {
16451662
if errors.Is(err, fs.ErrExist) {
@@ -1944,3 +1961,16 @@ func configureFlowSpecVariables(spec *cilium.CollectionSpec, cfg *FlowFetcherCon
19441961

19451962
return nil
19461963
}
1964+
1965+
func tcxAnchor(anchor string) link.Anchor {
1966+
switch anchor {
1967+
case tcxAnchorHead:
1968+
return link.Head()
1969+
case tcxAnchorTail:
1970+
return link.Tail()
1971+
case tcxAnchorNone:
1972+
return nil
1973+
default:
1974+
return nil
1975+
}
1976+
}

0 commit comments

Comments
 (0)