Skip to content

Commit

Permalink
set flow port to 0 in bpfmap to avoid duplicate labels
Browse files Browse the repository at this point in the history
  • Loading branch information
jzwlqx committed Apr 7, 2024
1 parent dfcfff1 commit 7fc62d6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
4 changes: 3 additions & 1 deletion bpf/flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#define TC_ACT_OK 0

volatile const bool enable_flow_port = 0;

//todo aggregate all flow based metrics in one map to save memory.
struct flow_metrics {
u64 packets;
Expand All @@ -24,7 +26,7 @@ struct {

static inline int __do_flow(struct __sk_buff *skb){
struct flow_tuple_4 tuple = {0};
if(set_flow_tuple4(skb, &tuple) < 0){
if(set_flow_tuple4(skb, &tuple, enable_flow_port) < 0){
goto out;
}

Expand Down
5 changes: 4 additions & 1 deletion bpf/headers/inspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static __always_inline u32 get_netns(struct sk_buff *skb) {
return netns;
}

static __always_inline int set_flow_tuple4(struct __sk_buff *skb, struct flow_tuple_4 *tuple){
static __always_inline int set_flow_tuple4(struct __sk_buff *skb, struct flow_tuple_4 *tuple, bool port){
void *data = (void *)(long)skb->data;
struct ethhdr *eth = data;
void *data_end = (void *)(long)skb->data_end;
Expand All @@ -130,6 +130,9 @@ static __always_inline int set_flow_tuple4(struct __sk_buff *skb, struct flow_tu
tuple->src = iph->saddr;
tuple->dst = iph->daddr;
tuple->proto = iph->protocol;
if (!port){
return 0;
}

l4_off = sizeof(*eth) + iph->ihl * 4;

Expand Down
2 changes: 1 addition & 1 deletion bpf/packetloss.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +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;
const volatile bool enable_packetloss_stack = 0;

struct {
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
Expand Down
26 changes: 20 additions & 6 deletions pkg/exporter/probe/flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,30 @@ func (p *metricsProbe) loadBPF() error {
return fmt.Errorf("remove limit failed: %s", err.Error())
}

opts := ebpf.CollectionOptions{}
spec, err := loadBpf()
if err != nil {
return fmt.Errorf("failed loading bpf: %w", err)
}

if p.enablePort {
m := map[string]interface{}{
"enable_flow_port": 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.bpfObjs, &opts); err != nil {
return fmt.Errorf("failed loading objects: %w", err)
if err := spec.LoadAndAssign(&p.bpfObjs, &opts); err != nil {
return fmt.Errorf("loading objects: %s", err.Error())
}

return nil
}

Expand Down

0 comments on commit 7fc62d6

Please sign in to comment.