-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change to use perf_event for exec events
- Loading branch information
Showing
11 changed files
with
179 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package bpf | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/binary" | ||
"errors" | ||
"github.com/cilium/ebpf/perf" | ||
"golang.org/x/xerrors" | ||
"log" | ||
"unsafe" | ||
) | ||
|
||
func (b *BPF) PullPacketEvents(ctx context.Context) (<-chan BpfPacketEventT, error) { | ||
reader, err := perf.NewReader(b.objs.PacketEvents, 1500*1000) | ||
if err != nil { | ||
return nil, xerrors.Errorf(": %w", err) | ||
} | ||
ch := make(chan BpfPacketEventT, 10) | ||
go func() { | ||
defer close(ch) | ||
defer reader.Close() | ||
b.handlePacketEvents(ctx, reader, ch) | ||
}() | ||
|
||
return ch, nil | ||
} | ||
|
||
func (b *BPF) handlePacketEvents(ctx context.Context, reader *perf.Reader, ch chan<- BpfPacketEventT) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
} | ||
|
||
record, err := reader.Read() | ||
if err != nil { | ||
if errors.Is(err, perf.ErrClosed) { | ||
return | ||
} | ||
log.Printf("read packet event failed: %s", err) | ||
continue | ||
} | ||
event, err := parsePacketEvent(record.RawSample) | ||
if err != nil { | ||
log.Printf("parse packet event failed: %s", err) | ||
} else { | ||
ch <- *event | ||
} | ||
if record.LostSamples > 0 { | ||
// TODO: XXX | ||
} | ||
} | ||
} | ||
|
||
func parsePacketEvent(rawSample []byte) (*BpfPacketEventT, error) { | ||
event := BpfPacketEventT{} | ||
if err := binary.Read(bytes.NewBuffer(rawSample), binary.LittleEndian, &event.Meta); err != nil { | ||
return nil, xerrors.Errorf("parse meta: %w", err) | ||
} | ||
copy(event.Payload[:], rawSample[unsafe.Offsetof(event.Payload):]) | ||
return &event, nil | ||
} | ||
|
||
func (b *BPF) PullExecEvents(ctx context.Context) (<-chan BpfExecEventT, error) { | ||
reader, err := perf.NewReader(b.objs.ExecEvents, 1024*256) | ||
if err != nil { | ||
return nil, xerrors.Errorf(": %w", err) | ||
} | ||
ch := make(chan BpfExecEventT, 10) | ||
go func() { | ||
defer close(ch) | ||
defer reader.Close() | ||
b.handleExecEvents(ctx, reader, ch) | ||
}() | ||
|
||
return ch, nil | ||
} | ||
|
||
func (b *BPF) handleExecEvents(ctx context.Context, reader *perf.Reader, ch chan<- BpfExecEventT) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
} | ||
|
||
record, err := reader.Read() | ||
if err != nil { | ||
if errors.Is(err, perf.ErrClosed) { | ||
return | ||
} | ||
log.Printf("read exec event failed: %s", err) | ||
continue | ||
} | ||
event, err := parseExecEvent(record.RawSample) | ||
if err != nil { | ||
log.Printf("parse exec event failed: %s", err) | ||
} else { | ||
ch <- *event | ||
} | ||
if record.LostSamples > 0 { | ||
// TODO: XXX | ||
} | ||
} | ||
} | ||
|
||
func parseExecEvent(rawSample []byte) (*BpfExecEventT, error) { | ||
event := BpfExecEventT{} | ||
if err := binary.Read(bytes.NewBuffer(rawSample), binary.LittleEndian, &event); err != nil { | ||
return nil, xerrors.Errorf("parse event: %w", err) | ||
} | ||
return &event, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.