Skip to content

Commit e37dda4

Browse files
huhong-webhao022
authored andcommitted
sync remaining changes from my-huatuo-review
- ICMPInfo.Detail: output seq and checksum fields alongside id - pcapfilter: rename InjectFilter→Apply in doc comment and test - symbol.FormatStackLines: inline loop, remove separate FormatStack helper - server_test: delegate connect to Client.handshake instead of sendConnect; keep deferred rawClient.Close in TestUnexpectedClose so handleConn exits - types/dropwatch: add DropSourceTypesEvent and DropSourceTypesTool constants - SKB_DROP_REASON: support kernel skb drop reason Change-Id: If1ffcb02728f5c6c1355c8eff3bfe5603ecd192f
1 parent 48f72c5 commit e37dda4

8 files changed

Lines changed: 39 additions & 71 deletions

File tree

bpf/dropwatch.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
#define SK_FL_TYPE_SHIFT 16
2121
#define SK_FL_TYPE_MASK 0xffff0000
2222

23-
/* Drop reason (kernel skb_drop_reason enum; UNKNOWN until plumbed). */
24-
#define DROP_REASON_UNKNOWN 0
25-
#define DROP_REASON_UNSUPPORT 1
23+
/* Reserved for future kernel skb_drop_reason (SKB_DROP_REASON_NOT_SPECIFIED,
24+
* ...) passthrough; (u32)-1 is out of band: kernel reason values grow upward
25+
* from 0 (low 16 bits code, high 16 bits subsystem since v6.4) and can never
26+
* reach it. */
27+
#define SKB_DROP_REASON_UNSUPPORT ((u32)-1)
2628
#define PKT_RAW_LEN 120
2729

2830
struct packet_meta {
@@ -192,7 +194,7 @@ int bpf_kfree_skb_prog(struct trace_event_raw_kfree_skb *ctx)
192194
bpf_get_current_comm(&data->meta.comm, sizeof(data->meta.comm));
193195
data->meta.kfree_skb_addr = (u64)(unsigned long)ctx->location;
194196
data->meta.queue_mapping = BPF_CORE_READ(skb, queue_mapping);
195-
data->meta.drop_reason = DROP_REASON_UNKNOWN;
197+
data->meta.drop_reason = SKB_DROP_REASON_UNSUPPORT;
196198
data->meta.type = 0;
197199

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

cmd/dropwatch/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func loadBPFWithFilter(bpfPath, filterExpr string, consts map[string]any) (bpf.B
103103
// deviceFilterConsts resolves devName to an ifindex and returns the consts map
104104
// expected by bpf_skb_filter.h. Returns nil consts when devName is empty
105105
// (filter disabled at the BPF level by leaving filter_ifindex_included == 0).
106-
func deviceFilterConsts(devName string, exclude bool) (map[string]any, error) {
106+
func deviceFilterConsts(devName string, excluded bool) (map[string]any, error) {
107107
if devName == "" {
108108
return nil, nil
109109
}
@@ -112,7 +112,7 @@ func deviceFilterConsts(devName string, exclude bool) (map[string]any, error) {
112112
return nil, fmt.Errorf("--device %q: %w", devName, err)
113113
}
114114
var excl uint32
115-
if exclude {
115+
if excluded {
116116
excl = 1
117117
}
118118
return map[string]any{
@@ -182,7 +182,7 @@ func mainAction(c *cli.Context) error {
182182
defer sockClient.End()
183183
}
184184

185-
consts, err := deviceFilterConsts(c.String("device"), c.Bool("device-exclude"))
185+
consts, err := deviceFilterConsts(c.String("device"), c.Bool("device-excluded"))
186186
if err != nil {
187187
return fmt.Errorf("dropwatch: %w", err)
188188
}
@@ -268,7 +268,7 @@ func main() {
268268
Usage: "interface to filter on (e.g. eth0); empty = all devices",
269269
},
270270
&cli.BoolFlag{
271-
Name: "device-exclude",
271+
Name: "device-excluded",
272272
Usage: "treat --device as a blacklist (drop matching) instead of whitelist (pass matching)",
273273
},
274274
&cli.IntFlag{

internal/packet/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ func (t *UDPInfo) Detail() string {
144144
}
145145

146146
func (t *ICMPInfo) Detail() string {
147-
return fmt.Sprintf("[ICMP %s] %s > %s id=%d smac=%s dmac=%s",
148-
t.ICMPType, t.Saddr, t.Daddr, t.ID, t.SrcMAC, t.DstMAC)
147+
return fmt.Sprintf("[ICMP %s] %s > %s id=%d seq=%d chk=0x%04x smac=%s dmac=%s",
148+
t.ICMPType, t.Saddr, t.Daddr, t.ID, t.Seq, t.Checksum, t.SrcMAC, t.DstMAC)
149149
}
150150

151151
func (t *ARPInfo) Detail() string {

internal/pcapfilter/bpf_filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// Usage:
1919
//
2020
// spec, _ := ebpf.LoadCollectionSpec("prog.o")
21-
// if err := pcapfilter.InjectFilter(spec, "host 10.0.0.1"); err != nil { ... }
21+
// if err := pcapfilter.Apply(spec, "host 10.0.0.1"); err != nil { ... }
2222
// coll, _ := ebpf.NewCollection(spec)
2323
//
2424
// Limitations inherited from go-pcap's pure-Go compiler:

internal/pcapfilter/load_default_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func dumpPrograms(t *testing.T, spec *ebpf.CollectionSpec, prefix string) {
6262
}
6363
}
6464

65-
func TestInjectFilter(t *testing.T) {
65+
func TestApply(t *testing.T) {
6666
if os.Getuid() != 0 {
6767
t.Skip("Skipping: requires root")
6868
}
@@ -79,7 +79,7 @@ func TestInjectFilter(t *testing.T) {
7979

8080
filterExpr := "ip and src net 192.168.1.0/24 and tcp dst port 3306"
8181
if err := Apply(specs, filterExpr); err != nil {
82-
t.Fatalf("InjectFilter: %v", err)
82+
t.Fatalf("Apply: %v", err)
8383
}
8484

8585
dumpPrograms(t, specs, "Program")

internal/symbol/symbols.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -370,30 +370,18 @@ func lowerDirFromMountInfo(pid uint32) (string, error) {
370370
return "", fmt.Errorf("lowerdir not found for pid %d", pid)
371371
}
372372

373-
// FormatStack formats a stack trace (newline-separated addresses)
374-
// into a readable form with frame indices.
375-
// Returns nil if stack is empty.
376-
func FormatStack(stack string) []string {
377-
if stack == "" {
378-
return nil
379-
}
380-
381-
var lines []string
373+
// FormatStackLines formats a stack trace (newline-separated addresses)
374+
// and writes it to w with frame indices.
375+
func FormatStackLines(w io.Writer, stack string) error {
376+
i := 0
382377
for _, frame := range strings.Split(strings.TrimRight(stack, "\n"), "\n") {
383-
if frame != "" {
384-
lines = append(lines, frame)
378+
if frame == "" {
379+
continue
385380
}
386-
}
387-
return lines
388-
}
389-
390-
// FormatStackLines formats stack lines for output with indices.
391-
func FormatStackLines(w io.Writer, stack string) error {
392-
lines := FormatStack(stack)
393-
for i, frame := range lines {
394381
if _, err := fmt.Fprintf(w, "\t#%-2d %s\n", i, frame); err != nil {
395382
return err
396383
}
384+
i++
397385
}
398386
return nil
399387
}

internal/toolstream/transport/server_test.go

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,10 @@ func newPipedServer(t *testing.T) (rawClient net.Conn, clientEnc *capnp.Encoder,
9191
}
9292
}
9393

94-
// sendConnect sends a Connect frame via enc.
95-
func sendConnect(t *testing.T, enc *capnp.Encoder, toolName, version, taskID string) error {
94+
// handshake sends a Connect frame via enc using Client.handshake.
95+
func handshake(t *testing.T, enc *capnp.Encoder, rawClient net.Conn, toolName, version, taskID string) error {
9696
t.Helper()
97-
m, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
98-
if err != nil {
99-
return fmt.Errorf("send connect new message: %w", err)
100-
}
101-
root, err := NewRootMessage(seg)
102-
if err != nil {
103-
return fmt.Errorf("send connect new root: %w", err)
104-
}
105-
c, err := root.NewConnect()
106-
if err != nil {
107-
return fmt.Errorf("send connect new connect: %w", err)
108-
}
109-
if err := c.SetToolName(toolName); err != nil {
110-
return fmt.Errorf("send connect set tool name: %w", err)
111-
}
112-
if err := c.SetVersion(version); err != nil {
113-
return fmt.Errorf("send connect set version: %w", err)
114-
}
115-
if err := c.SetTaskID(taskID); err != nil {
116-
return fmt.Errorf("send connect set task id: %w", err)
117-
}
118-
c.SetProtoVersion(1)
119-
if err := enc.Encode(m); err != nil {
120-
return fmt.Errorf("send connect encode: %w", err)
121-
}
122-
return nil
97+
return (&Client{encoder: enc, conn: rawClient}).handshake(toolName, version, taskID)
12398
}
12499

125100
// sendChunk sends a Chunk frame via enc.
@@ -159,8 +134,7 @@ func TestNormalPath(t *testing.T) {
159134
rawClient, clientEnc, rec, wait := newPipedServer(t)
160135

161136
go func() {
162-
defer rawClient.Close()
163-
if err := sendConnect(t, clientEnc, "dropwatch", "1.0", ""); err != nil {
137+
if err := handshake(t, clientEnc, rawClient, "dropwatch", "1.0", ""); err != nil {
164138
t.Logf("send connect: %v", err)
165139
return
166140
}
@@ -202,8 +176,7 @@ func TestErrorEnd(t *testing.T) {
202176
rawClient, clientEnc, rec, wait := newPipedServer(t)
203177

204178
go func() {
205-
defer rawClient.Close()
206-
if err := sendConnect(t, clientEnc, "tool", "v", ""); err != nil {
179+
if err := handshake(t, clientEnc, rawClient, "tool", "v", ""); err != nil {
207180
t.Logf("send connect: %v", err)
208181
return
209182
}
@@ -229,8 +202,7 @@ func TestDataAndEndCombined(t *testing.T) {
229202
rawClient, clientEnc, rec, wait := newPipedServer(t)
230203

231204
go func() {
232-
defer rawClient.Close()
233-
if err := sendConnect(t, clientEnc, "tool", "v", ""); err != nil {
205+
if err := handshake(t, clientEnc, rawClient, "tool", "v", ""); err != nil {
234206
t.Logf("send connect: %v", err)
235207
return
236208
}
@@ -257,7 +229,7 @@ func TestUnexpectedClose(t *testing.T) {
257229

258230
go func() {
259231
defer rawClient.Close()
260-
if err := sendConnect(t, clientEnc, "tool", "v", ""); err != nil {
232+
if err := handshake(t, clientEnc, rawClient, "tool", "v", ""); err != nil {
261233
t.Logf("send connect: %v", err)
262234
return
263235
}
@@ -294,7 +266,7 @@ func TestEmptyToolName(t *testing.T) {
294266
rawClient, clientEnc, rec, wait := newPipedServer(t)
295267

296268
go func() {
297-
if err := sendConnect(t, clientEnc, "", "v", ""); err != nil {
269+
if err := handshake(t, clientEnc, rawClient, "", "v", ""); err != nil {
298270
t.Logf("send connect: %v", err)
299271
return
300272
}
@@ -313,9 +285,9 @@ func TestEmptyToolName(t *testing.T) {
313285
// omits the client-side close so that "accept-then-loop" behavior would hang
314286
// here until wait() trips its 2s timeout.
315287
func TestEmptyToolNameClosesEarly(t *testing.T) {
316-
_, clientEnc, rec, wait := newPipedServer(t)
288+
rawClient, clientEnc, rec, wait := newPipedServer(t)
317289

318-
if err := sendConnect(t, clientEnc, "", "v", ""); err != nil {
290+
if err := handshake(t, clientEnc, rawClient, "", "v", ""); err != nil {
319291
t.Logf("send connect: %v", err)
320292
return
321293
}
@@ -333,7 +305,7 @@ func TestDecodeFailureAfterConnect(t *testing.T) {
333305
rawClient, clientEnc, rec, wait := newPipedServer(t)
334306

335307
go func() {
336-
if err := sendConnect(t, clientEnc, "tool", "v", ""); err != nil {
308+
if err := handshake(t, clientEnc, rawClient, "tool", "v", ""); err != nil {
337309
t.Logf("send connect: %v", err)
338310
return
339311
}

pkg/types/dropwatch.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ type DropWatchTracing struct {
4040
Source string `json:"source,omitempty"`
4141
Stack string `json:"stack"`
4242
}
43+
44+
// Values for DropWatchTracing.Source.
45+
const (
46+
DropSourceTypesEvent = "events"
47+
DropSourceTypesTool = "tools"
48+
)

0 commit comments

Comments
 (0)