Skip to content

Commit 7fc5f83

Browse files
committed
chore(sync): align codebase with upstream open version
Synchronize 51 files across bpf, core, integration, internal, pkg and vendor modules. Key changes: - add xfs procfs parser and metrics integration tests - add dev procfs parser for block device resolution - extend watch and netutil test coverage - refine dropwatch, hungtask and iolatency bpf programs - update grafana dashboards and vendor procfs/xfs dependency
1 parent bb9f3ee commit 7fc5f83

55 files changed

Lines changed: 2204 additions & 910 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bpf/dropwatch.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
#define SK_FL_TYPE_SHIFT 16
2222
#define SK_FL_TYPE_MASK 0xffff0000
2323

24-
/* Reserved for future kernel skb_drop_reason (SKB_DROP_REASON_NOT_SPECIFIED,
25-
* ...) passthrough; (u32)-1 is out of band: kernel reason values grow upward
26-
* from 0 (low 16 bits code, high 16 bits subsystem since v6.4) and can never
27-
* reach it. */
28-
#define SKB_DROP_REASON_UNSUPPORT ((u32)-1)
2924
#define PKT_RAW_LEN 120
3025

3126
struct packet_meta {
@@ -38,11 +33,10 @@ struct packet_meta {
3833
u32 dev_flags; /* 4 */
3934
u32 queue_mapping; /* 4 */
4035
u32 drop_reason; /* 4 */
41-
u32 type; /* 4 */
4236
u32 net_inum; /* 4 */
4337
u8 dev_name[IFNAMSIZ]; /* 16 */
4438
u8 comm[COMPAT_TASK_COMM_LEN]; /* 16 */
45-
}; /* total: 96 bytes */
39+
}; /* 92 bytes + 4 tail pad = 96 */
4640

4741
struct packet_raw {
4842
u16 eth_proto; /* 2 */
@@ -86,6 +80,31 @@ char __license[] SEC("license") = "Dual MIT/GPL";
8680
static const struct drop_packet_event zero_data = {};
8781
static const u32 stackmap_key = 0;
8882

83+
/* kfree_skb gained an skb drop reason field in v5.17, absent from the BTF this
84+
* object is compiled against. Carry it in a CO-RE flavor relocated at load time:
85+
* the anonymous enum field matches the kernel's enum skb_drop_reason by the name
86+
* "reason". No reason constants are hardcoded (their values shift across kernels);
87+
* names are resolved at runtime from kernel BTF in userspace (loadDropReasonNames).
88+
*
89+
* SKB_DROP_REASON_UNSUPPORT = -1 satisfies C's "enum needs >=1 enumerator" rule
90+
* and is the out-of-band fallback for kernels predating the field: as (u32)-1 it
91+
* can never collide with real reasons (which grow from 0). */
92+
struct trace_event_raw_kfree_skb___reason {
93+
enum { SKB_DROP_REASON_UNSUPPORT = -1 } reason;
94+
} __attribute__((preserve_access_index));
95+
96+
/* Return the kernel skb drop reason when the running kernel supports it,
97+
* otherwise the out-of-band SKB_DROP_REASON_UNSUPPORT sentinel. */
98+
static inline u32 skb_get_drop_reason(struct trace_event_raw_kfree_skb *ctx)
99+
{
100+
struct trace_event_raw_kfree_skb___reason *ctx_reason = (void *)ctx;
101+
102+
if (bpf_core_field_exists(ctx_reason->reason))
103+
return BPF_CORE_READ(ctx_reason, reason);
104+
105+
return SKB_DROP_REASON_UNSUPPORT;
106+
}
107+
89108
struct sock___5_10 {
90109
u16 sk_type;
91110
u16 sk_protocol;
@@ -187,7 +206,7 @@ int bpf_kfree_skb_prog(struct trace_event_raw_kfree_skb *ctx)
187206
return 0;
188207

189208
/* pcap filter via bpf_pcap_stub.h: pass-through stub patched at load
190-
* time by internal/pcapinject with the compiled tcpdump expression.
209+
* time by internal/pcapfilter with the compiled tcpdump expression.
191210
*/
192211
if (!PCAP_STUB_PASS_SKB(skb))
193212
return 0;
@@ -205,16 +224,11 @@ int bpf_kfree_skb_prog(struct trace_event_raw_kfree_skb *ctx)
205224

206225
/* meta */
207226
data->meta.ktime_ns = bpf_ktime_get_ns();
208-
/* tgid_pid and comm in SoftIRQ Context do not accurately reflect
209-
* the process where the drop occurred, only for reference.
210-
*/
211227
data->meta.tgid_pid = bpf_get_current_pid_tgid();
212228
bpf_get_current_comm(&data->meta.comm, sizeof(data->meta.comm));
213-
214229
data->meta.kfree_skb_addr = (u64)(unsigned long)ctx->location;
215230
data->meta.queue_mapping = BPF_CORE_READ(skb, queue_mapping);
216-
data->meta.drop_reason = SKB_DROP_REASON_UNSUPPORT;
217-
data->meta.type = 0;
231+
data->meta.drop_reason = skb_get_drop_reason(ctx);
218232

219233
data->pkt_hdr.pkt_len = BPF_CORE_READ(skb, len);
220234

bpf/hungtask.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <bpf/bpf_tracing.h>
66

77
#include "bpf_common.h"
8-
#include "bpf_compat_7_0.h"
98
#include "bpf_ratelimit.h"
109

1110
char __license[] SEC("license") = "Dual MIT/GPL";

bpf/include/bpf_pcap_stub.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
/*
1111
* pcap_stub_l{2,3}: reserved-NOP stub functions patched at load time by
12-
* internal/pcapinject with compiled tcpdump filter bytecode. The three
12+
* internal/pcapfilter with compiled tcpdump filter bytecode. The three
1313
* ctx parameters reserve R1/R2/R3; the patched bytecode overwrites them.
1414
* Unpatched body is a pass-through: data != data_end && three-way ctx
1515
* equality both hold, so the stub always returns true.
@@ -20,7 +20,7 @@
2020
static __noinline bool pcap_stub_l3(void *_ctx, void *__ctx, void *___ctx,
2121
void *data, void *data_end)
2222
{
23-
/* 512 × 8-byte NOP insns; must equal nopAreaSize in internal/pcapinject/elfpatch.go */
23+
/* 512 × 8-byte NOP insns; must equal stubReservedInsns in internal/pcapfilter/elfpatch.go */
2424
asm volatile(".rept 512\n\t"
2525
".byte 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00\n\t"
2626
".endr\n\t");
@@ -30,7 +30,7 @@ static __noinline bool pcap_stub_l3(void *_ctx, void *__ctx, void *___ctx,
3030
static __noinline bool pcap_stub_l2(void *_ctx, void *__ctx, void *___ctx,
3131
void *data, void *data_end)
3232
{
33-
/* 512 × 8-byte NOP insns; must equal nopAreaSize in internal/pcapinject/elfpatch.go */
33+
/* 512 × 8-byte NOP insns; must equal stubReservedInsns in internal/pcapfilter/elfpatch.go */
3434
asm volatile(".rept 512\n\t"
3535
".byte 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00\n\t"
3636
".endr\n\t");

bpf/include/vmlinux_net.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
#define IP_MF 0x2000 /* Flag: "More Fragments" */
1818
#define IP_OFFSET 0x1FFF /* "Fragment Offset" part */
1919

20+
// skb_mac_header - return the MAC header pointer from sk_buff
21+
static inline unsigned char *skb_mac_header(struct sk_buff *skb)
22+
{
23+
return BPF_CORE_READ(skb, head) + BPF_CORE_READ(skb, mac_header);
24+
}
25+
2026
// skb_network_header - get the network header from sk_buff
2127
static inline unsigned char *skb_network_header(struct sk_buff *skb)
2228
{
@@ -28,10 +34,4 @@ static inline unsigned char *skb_transport_header(struct sk_buff *skb)
2834
{
2935
return BPF_CORE_READ(skb, head) + BPF_CORE_READ(skb, transport_header);
3036
}
31-
32-
// skb_mac_header - return the MAC header pointer from sk_buff
33-
static inline unsigned char *skb_mac_header(struct sk_buff *skb)
34-
{
35-
return BPF_CORE_READ(skb, head) + BPF_CORE_READ(skb, mac_header);
36-
}
3737
#endif

build/docker/grafana/dashboards/metrics-container.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3952,4 +3952,4 @@
39523952
"title": "Metric 大盘 - 容器视图",
39533953
"uid": "metrics-container",
39543954
"version": 1
3955-
}
3955+
}

cmd/huatuo-bamai/config/config_test.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func writeConfigFile(t *testing.T, dir, name, content string) string {
2626

2727
path := filepath.Join(dir, name)
2828
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
29-
t.Errorf("write config file %s: %v", path, err)
29+
t.Fatalf("write config file %s: %v", path, err)
3030
return ""
3131
}
3232

@@ -61,8 +61,7 @@ ExcludedOnContainer = "writeback"
6161
}
6262

6363
if err := Load(path); err != nil {
64-
t.Errorf("Load returned error: %v", err)
65-
return
64+
t.Fatalf("Load returned error: %v", err)
6665
}
6766

6867
if len(Get().BlackList) != 2 {
@@ -110,8 +109,7 @@ ExcludedOnContainer = "writeback"
110109
}
111110

112111
if err := Load(path); err != nil {
113-
t.Errorf("Load returned error: %v", err)
114-
return
112+
t.Fatalf("Load returned error: %v", err)
115113
}
116114

117115
Set("BlackList", []string{"netdev_hw", "metax_gpu"})
@@ -121,13 +119,11 @@ ExcludedOnContainer = "writeback"
121119
Set("MetricCollector.Vmstat.IncludedOnContainer", "workingset_refault_file")
122120

123121
if err := Sync(); err != nil {
124-
t.Errorf("Sync returned error: %v", err)
125-
return
122+
t.Fatalf("Sync returned error: %v", err)
126123
}
127124

128125
if err := Load(path); err != nil {
129-
t.Errorf("Load after Sync returned error: %v", err)
130-
return
126+
t.Fatalf("Load after Sync returned error: %v", err)
131127
}
132128

133129
if len(Get().BlackList) != 2 {
@@ -148,8 +144,7 @@ ExcludedOnContainer = "writeback"
148144

149145
raw, err := os.ReadFile(path)
150146
if err != nil {
151-
t.Errorf("read synced config: %v", err)
152-
return
147+
t.Fatalf("read synced config: %v", err)
153148
}
154149
if !strings.Contains(string(raw), "[AutoTracing]") || !strings.Contains(string(raw), "IssuesList = [[\"cpuidle\", \"perf\"]]") {
155150
t.Errorf("synced config should persist AutoTracing.IssuesList, got %s", string(raw))

core/events/memory_reclaim_events.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ import (
2626
"huatuo-bamai/pkg/tracing"
2727
)
2828

29-
type memoryReclaimTracing struct{}
30-
31-
type memoryReclaimPerfEvent struct {
32-
Comm [bpf.TaskCommLen]byte
33-
Deltatime uint64
34-
CSS uint64
35-
Pid uint64
36-
}
29+
type (
30+
memoryReclaimTracing struct{}
31+
memoryReclaimPerfEvent struct {
32+
Comm [bpf.TaskCommLen]byte
33+
Deltatime uint64
34+
CSS uint64
35+
Pid uint64
36+
}
37+
)
3738

3839
// MemoryReclaimTracingData is the full data structure.
3940
type MemoryReclaimTracingData struct {
@@ -56,9 +57,9 @@ func newMemoryReclaim() (*tracing.EventTracingAttr, error) {
5657

5758
const cssCacheTTL = 5 * time.Second
5859

59-
//go:generate $BPF_COMPILE $BPF_INCLUDE -s $BPF_DIR/memory_reclaim_events.c -o $BPF_DIR/memory_reclaim_events.o
60-
6160
// Start detect work, load bpf and wait data form perfevent
61+
//
62+
//go:generate $BPF_COMPILE $BPF_INCLUDE -s $BPF_DIR/memory_reclaim_events.c -o $BPF_DIR/memory_reclaim_events.o
6263
func (c *memoryReclaimTracing) Start(ctx context.Context) error {
6364
b, err := bpf.LoadBpf(bpf.ThisBpfOBJ(), map[string]any{
6465
"deltath": cfg.MemoryReclaim.BlockedThreshold,

core/events/net_rx_latency.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ func (c *netRecvLatTracing) Start(ctx context.Context) error {
124124

125125
log.Debugf("net_rx_latency offset of mono to walltime: %v ns", monoWallOffset)
126126

127-
// Enable skb software RX timestamps before starting the tracer.
127+
// for tracing 'net_rx_latency' keep the skb timestamp enabled,
128+
// kernel func net_enable_timestamp() is system wide, can enable by set SOF_TIMESTAMPING_RX_SOFTWARE,
129+
// ref: https://www.kernel.org/doc/html/latest/networking/timestamping.html.
128130
tsfd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
129131
if err != nil {
130132
return fmt.Errorf("create timestamp socket: %w", err)
@@ -234,6 +236,7 @@ func (c *netRecvLatTracing) Start(ctx context.Context) error {
234236
}
235237
log.Debugf("net_rx_latency tracerData: %+v", tracerData)
236238

239+
// save storage
237240
if err := tracing.Save(&tracing.WriteRequest{
238241
TracerName: "net_rx_latency",
239242
ContainerID: containerID,
@@ -251,7 +254,7 @@ func ignore(pid uint64, comm string, hostNetnsInode uint64) (containerID string,
251254
dstInode, err := netutil.NetNSInodeByPid(int(pid))
252255
if err != nil {
253256
// ignore the missing program
254-
if errors.Is(err, syscall.ENOENT) {
257+
if errors.Is(err, syscall.ENOENT) || errors.Is(err, unix.EACCES) || errors.Is(err, unix.ESRCH) {
255258
return "", true, nil
256259
}
257260
return "", skip, fmt.Errorf("get netns inode of pid %v failed: %w", pid, err)

0 commit comments

Comments
 (0)