Skip to content

Commit f9bd25f

Browse files
jschwinger233brb
authored andcommitted
Collect and output skb->cb when --filter-trace-tc
According to kernel verifier implementation[1], __sk_buff->cb will be mapped to ((struct qdisc_skb_cb*)&sk_buff->cb)->data, let's collect 20 bytes from there and output cb as u32[5] when --filter-trace-tc is turned on. [1] https://elixir.bootlin.com/linux/v6.8/source/net/core/filter.c#L9593 Signed-off-by: gray <[email protected]>
1 parent 55bdaac commit f9bd25f

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

bpf/kprobe_pwru.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct skb_meta {
5454
u32 len;
5555
u32 mtu;
5656
u16 protocol;
57-
u16 pad;
57+
u32 cb[5];
5858
} __attribute__((packed));
5959

6060
struct tuple {
@@ -143,7 +143,8 @@ struct config {
143143
u8 output_shinfo: 1;
144144
u8 output_stack: 1;
145145
u8 output_caller: 1;
146-
u8 output_unused: 2;
146+
u8 output_cb: 1;
147+
u8 output_unused: 1;
147148
u8 is_set: 1;
148149
u8 track_skb: 1;
149150
u8 track_skb_by_stackid: 1;
@@ -443,6 +444,11 @@ set_output(void *ctx, struct sk_buff *skb, struct event_t *event) {
443444
if (cfg->output_stack) {
444445
event->print_stack_id = bpf_get_stackid(ctx, &print_stack_map, BPF_F_FAST_STACK_CMP);
445446
}
447+
448+
if (cfg->output_cb) {
449+
struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)&skb->cb;
450+
bpf_probe_read_kernel(&event->meta.cb, sizeof(event->meta.cb), (void *)&cb->data);
451+
}
446452
}
447453

448454
static __noinline bool

internal/pwru/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
OutputShinfoMask
2323
OutputStackMask
2424
OutputCallerMask
25+
OutputCbMask
2526
)
2627

2728
const (
@@ -69,6 +70,9 @@ func GetConfig(flags *Flags) (cfg FilterCfg, err error) {
6970
if flags.OutputCaller {
7071
cfg.OutputFlags |= OutputCallerMask
7172
}
73+
if flags.FilterTraceTc {
74+
cfg.OutputFlags |= OutputCbMask
75+
}
7276
if flags.FilterTrackSkb {
7377
cfg.FilterFlags |= TrackSkbMask
7478
}

internal/pwru/output.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type jsonPrinter struct {
6464
Proto uint16 `json:"proto,omitempty"`
6565
Mtu uint32 `json:"mtu,omitempty"`
6666
Len uint32 `json:"len,omitempty"`
67+
Cb [5]uint32 `json:"cb,omitempty"`
6768
Tuple *jsonTuple `json:"tuple,omitempty"`
6869
Stack interface{} `json:"stack,omitempty"`
6970
SkbMetadata interface{} `json:"skb_metadata,omitempty"`
@@ -141,6 +142,9 @@ func (o *output) PrintHeader() {
141142
}
142143
if o.flags.OutputMeta {
143144
fmt.Fprintf(o.writer, " %-10s %-8s %16s %-6s %-5s %-5s", "NETNS", "MARK/x", centerAlignString("IFACE", 16), "PROTO", "MTU", "LEN")
145+
if o.flags.FilterTraceTc {
146+
fmt.Fprintf(o.writer, " %-56s", "__sk_buff->cb[]")
147+
}
144148
}
145149
if o.flags.OutputTuple {
146150
fmt.Fprintf(o.writer, " %s", "TUPLE")
@@ -187,6 +191,9 @@ func (o *output) PrintJson(event *Event) {
187191
d.Proto = byteorder.NetworkToHost16(event.Meta.Proto)
188192
d.Mtu = event.Meta.MTU
189193
d.Len = event.Meta.Len
194+
if o.flags.FilterTraceTc {
195+
d.Cb = event.Meta.Cb
196+
}
190197
}
191198

192199
if o.flags.OutputTuple {
@@ -330,6 +337,14 @@ func getMetaData(event *Event, o *output) (metaData string) {
330337
return metaData
331338
}
332339

340+
func getCb(event *Event) (cb string) {
341+
res := []string{}
342+
for _, val := range event.Meta.Cb {
343+
res = append(res, fmt.Sprintf("0x%08X", val))
344+
}
345+
return fmt.Sprintf("[%s]", strings.Join(res, ","))
346+
}
347+
333348
func getOutFuncName(o *output, event *Event, addr uint64) string {
334349
var funcName string
335350

@@ -410,6 +425,9 @@ func (o *output) Print(event *Event) {
410425

411426
if o.flags.OutputMeta {
412427
fmt.Fprintf(o.writer, " %s", getMetaData(event, o))
428+
if o.flags.FilterTraceTc {
429+
fmt.Fprintf(o.writer, " %s", getCb(event))
430+
}
413431
}
414432

415433
if o.flags.OutputTuple {

internal/pwru/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ type Meta struct {
130130
Len uint32
131131
MTU uint32
132132
Proto uint16
133-
Pad uint16
133+
Cb [5]uint32
134134
}
135135

136136
type StackData struct {

0 commit comments

Comments
 (0)