Skip to content

Commit

Permalink
disable packetloss stack and disable flow port by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jzwlqx committed Apr 2, 2024
1 parent e51ccc5 commit 46a019e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 112 deletions.
19 changes: 0 additions & 19 deletions bpf/headers/feature_switch.h

This file was deleted.

7 changes: 2 additions & 5 deletions bpf/packetloss.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <bpf_tracing.h>
#include <bpf_core_read.h>
#include <inspector.h>
#include <feature_switch.h>

struct kfree_skb_args {
/* The first 8 bytes is not allowed to read */
Expand All @@ -24,6 +23,7 @@ struct insp_pl_event_t {
};

const struct insp_pl_event_t *unused_insp_pl_event_t __attribute__((unused));
const volatile u8 enable_packetloss_stack = 0;

struct {
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
Expand All @@ -40,14 +40,11 @@ SEC("tracepoint/skb/kfree_skb")
int kfree_skb(struct kfree_skb_args *args) {
struct sk_buff *skb = (struct sk_buff *)args->skb;
struct insp_pl_event_t event = {0};
enum insp_switch_key switch_key = enable_packetloss_stack;


set_tuple(skb, &event.tuple);
event.location = (u64)args->location;

bool *enable = bpf_map_lookup_elem(&insp_feature_switch, &switch_key);
if (enable && *enable){
if (enable_packetloss_stack){
event.stack_id = bpf_get_stackid(args, &insp_pl_stack,
KERN_STACKID_FLAGS);
}
Expand Down
6 changes: 6 additions & 0 deletions deploy/net-exporter-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ metrics:
- name: udp
- name: kernellatency
- name: packetloss
args:
enableStack: false
- name: flow
args:
enablePortInLabel: false
- name: tcpretrans
event:
probes:
- name: biolatency
- name: kernellatency
- name: packetloss
args:
enableStack: false
- name: tcpreset
- name: tcpretrans
sinks:
Expand Down
45 changes: 0 additions & 45 deletions pkg/exporter/probe/feature-switch.go

This file was deleted.

106 changes: 63 additions & 43 deletions pkg/exporter/probe/tracepacketloss/packetloss.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,24 @@ func (p *packetLossProbe) stop(probeType probe.Type) error {
p.probeConfig[probeType] = nil

if probeType == probe.ProbeTypeEvent {
if p.perfReader != nil {
p.perfReader.Close()
}
p.closePerfReader()
}

if p.probeCount() == 0 {
return p.cleanup()
p.cleanup()
}

return nil
}

func (p *packetLossProbe) cleanup() error {
func (p *packetLossProbe) closePerfReader() {
if p.perfReader != nil {
p.perfReader.Close()
p.perfReader = nil
}
}

func (p *packetLossProbe) cleanup() {
for _, link := range p.links {
link.Close()
}
Expand All @@ -212,22 +217,9 @@ func (p *packetLossProbe) cleanup() error {

p.objs.Close()

return nil
}

func (p *packetLossProbe) updateSwitch() error {
enableStack := uint8(0)
for _, cfg := range p.probeConfig {
log.Infof("cfg: %v", cfg)
if cfg != nil && cfg.enableStack {
enableStack = 1
}
}
log.Infof("%s update feature switch, set enablePacketLossStack to %d", probeName, enableStack)
return probe.UpdateSwitch(probe.EnablePacketLossStack, enableStack)
}

func (p *packetLossProbe) start(probeType probe.Type, cfg *probeConfig) (err error) {
func (p *packetLossProbe) start(probeType probe.Type, cfg *probeConfig) error {
p.lock.Lock()
defer p.lock.Unlock()

Expand All @@ -236,26 +228,42 @@ func (p *packetLossProbe) start(probeType probe.Type, cfg *probeConfig) (err err
}

p.probeConfig[probeType] = cfg
if p.probeCount() == 1 {
if err = p.loadAndAttachBPF(); err != nil {
log.Errorf("%s failed load and attach bpf, err: %v", probeName, err)
_ = p.cleanup()
return

if err := p.reinstallBPFLocked(); err != nil {
return fmt.Errorf("%s failed install ebpf: %w", probeName, err)
}

var err error

if probeType == probe.ProbeTypeEvent {
p.perfReader, err = perf.NewReader(p.objs.bpfMaps.InspPlEvent, int(unsafe.Sizeof(bpfInspPlEventT{})))
if err != nil {
log.Errorf("%s error create perf reader, err: %v", probeName, err)
return err
}

go p.perfLoop()
}

if err = p.updateSwitch(); err != nil {
log.Errorf("%s failed update feature switch, err: %v", probeName, err)
_ = p.cleanup()
return
return nil
}

func (p *packetLossProbe) reinstallBPFLocked() error {
p.closePerfReader()
p.cleanup()

if err := p.loadAndAttachBPF(); err != nil {
log.Errorf("%s failed load and attach bpf, err: %v", probeName, err)
p.cleanup()
return err
}

if probeType == probe.ProbeTypeEvent {
if p.probeConfig[probe.ProbeTypeEvent] != nil {
var err error
p.perfReader, err = perf.NewReader(p.objs.bpfMaps.InspPlEvent, int(unsafe.Sizeof(bpfInspPlEventT{})))
if err != nil {
log.Errorf("%s error create perf reader, err: %v", probeName, err)
_ = p.stop(probeType)
return
return err
}

go p.perfLoop()
Expand All @@ -264,35 +272,47 @@ func (p *packetLossProbe) start(probeType probe.Type, cfg *probeConfig) (err err
return nil
}

func (p *packetLossProbe) enableStack() bool {
for _, cfg := range p.probeConfig {
if cfg != nil && cfg.enableStack {
return true
}
}
return false
}

func (p *packetLossProbe) loadAndAttachBPF() error {
// Allow the current process to lock memory for eBPF resources.
if err := rlimit.RemoveMemlock(); err != nil {
return fmt.Errorf("remove limit failed: %s", err.Error())
}

switchMap, err := probe.LoadBPFSwitchMap()
spec, err := loadBpf()
if err != nil {
return fmt.Errorf("failed load switch map:%v", err)
return fmt.Errorf("failed loading bpf: %w", err)
}

opts := ebpf.CollectionOptions{
MapReplacements: map[string]*ebpf.Map{
probe.FeatureSwitchMapName: switchMap,
},
if p.enableStack() {
m := map[string]interface{}{
"enable_packetloss_stack": uint8(1),
}
if err := spec.RewriteConstants(m); err != nil {
return fmt.Errorf("failed rewrite constants: %w", err)
}
}

opts.Programs = ebpf.ProgramOptions{
KernelTypes: bpfutil.LoadBTFSpecOrNil(),
opts := ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
KernelTypes: bpfutil.LoadBTFSpecOrNil(),
},
}

// Load pre-compiled programs and maps into the kernel.
if err := loadBpfObjects(&p.objs, &opts); err != nil {
if err := spec.LoadAndAssign(p.objs, &opts); err != nil {
return fmt.Errorf("loading objects: %s", err.Error())
}

pl, err := link.Tracepoint("skb", "kfree_skb", p.objs.KfreeSkb, &link.TracepointOptions{})
if err != nil {
return fmt.Errorf("link tracepoint kfree_skb failed: %s", err.Error())
return fmt.Errorf("link tracepoint kfree_skb failed: %w", err)
}
p.links = append(p.links, pl)
return nil
Expand Down

0 comments on commit 46a019e

Please sign in to comment.