Skip to content

Commit

Permalink
Use using bit fields for FilterCfg
Browse files Browse the repository at this point in the history
No logic change.

Signed-off-by: gray <[email protected]>
  • Loading branch information
jschwinger233 authored and brb committed Jun 11, 2024
1 parent 05fe67e commit 0c793e9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
18 changes: 10 additions & 8 deletions bpf/kprobe_pwru.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,16 @@ struct config {
u32 netns;
u32 mark;
u32 ifindex;
u8 output_meta;
u8 output_tuple;
u8 output_skb;
u8 output_shinfo;
u8 output_stack;
u8 is_set;
u8 track_skb;
u8 track_skb_by_stackid;
u8 output_meta: 1;
u8 output_tuple: 1;
u8 output_skb: 1;
u8 output_shinfo: 1;
u8 output_stack: 1;
u8 output_unused: 3;
u8 is_set: 1;
u8 track_skb: 1;
u8 track_skb_by_stackid: 1;
u8 unused: 5;
} __attribute__((packed));

static volatile const struct config CFG;
Expand Down
42 changes: 24 additions & 18 deletions internal/pwru/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import (
"golang.org/x/sys/unix"
)

const (
OutputMetaMask uint8 = 1 << iota
OutputTupleMask
OutputSkbMask
OutputShinfoMask
OutputStackMask
)

const (
IsSetMask uint8 = 1 << iota
TrackSkbMask
TrackSkbByStackidMask
)

// Version is the pwru version and is set at compile time via LDFLAGS-
var Version string = "version unknown"

Expand All @@ -23,43 +37,35 @@ type FilterCfg struct {
FilterMark uint32
FilterIfindex uint32

// TODO: if there are more options later, then you can consider using a bit map
OutputMeta uint8
OutputTuple uint8
OutputSkb uint8
OutputShinfo uint8
OutputStack uint8

IsSet byte
TrackSkb byte
TrackSkbByStackid byte
OutputFlags uint8
FilterFlags uint8
}

func GetConfig(flags *Flags) (cfg FilterCfg, err error) {
cfg = FilterCfg{
FilterMark: flags.FilterMark,
IsSet: 1,
}
cfg.FilterFlags |= IsSetMask
if flags.OutputSkb {
cfg.OutputSkb = 1
cfg.OutputFlags |= OutputSkbMask
}
if flags.OutputShinfo {
cfg.OutputShinfo = 1
cfg.OutputFlags |= OutputShinfoMask
}
if flags.OutputMeta {
cfg.OutputMeta = 1
cfg.OutputFlags |= OutputMetaMask
}
if flags.OutputTuple {
cfg.OutputTuple = 1
cfg.OutputFlags |= OutputTupleMask
}
if flags.OutputStack {
cfg.OutputStack = 1
cfg.OutputFlags |= OutputStackMask
}
if flags.FilterTrackSkb {
cfg.TrackSkb = 1
cfg.FilterFlags |= TrackSkbMask
}
if flags.FilterTrackSkbByStackid {
cfg.TrackSkbByStackid = 1
cfg.FilterFlags |= TrackSkbByStackidMask
}

netnsID, ns, err := parseNetns(flags.FilterNetns)
Expand Down

0 comments on commit 0c793e9

Please sign in to comment.