Skip to content

Commit

Permalink
record real packet size
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Apr 21, 2024
1 parent d4511b3 commit f9bbd57
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions bpf/bpf_x86_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified bpf/bpf_x86_bpfel.o
Binary file not shown.
2 changes: 2 additions & 0 deletions bpf/ptcpdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct packet_event_meta_t {
u32 ifindex;
u32 pid;
u64 payload_len;
u64 packet_size;
char comm[TASK_COMM_LEN];
};

Expand Down Expand Up @@ -400,6 +401,7 @@ static __always_inline int handle_tc(struct __sk_buff *skb, bool egress) {
__builtin_memcpy(&event->meta.comm, &value->comm, sizeof(value->comm));

u64 payload_len = (u64)skb->len;
event->meta.packet_size = payload_len;
payload_len = payload_len < MAX_PAYLOAD_SIZE ? payload_len : MAX_PAYLOAD_SIZE;
event->meta.payload_len = payload_len;

Expand Down
14 changes: 10 additions & 4 deletions internal/event/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ const (
)

type Packet struct {
Type packetType
Device dev.Device
Pid int
Comm string
Type packetType
Device dev.Device
Pid int
Comm string
Truncated bool
Len int

Data []byte
}
Expand All @@ -38,6 +40,10 @@ func ParsePacketEvent(devices map[int]dev.Device, rawSample []byte) (*Packet, er
if event.Meta.PacketType == 1 {
p.Type = packetTypeEgress
}
if event.Meta.PacketSize > event.Meta.PayloadLen {
p.Truncated = true
}
p.Len = int(event.Meta.PacketSize)
p.Device = devices[int(event.Meta.Ifindex)]
p.Data = make([]byte, event.Meta.PayloadLen)
copy(p.Data[:], event.Payload[:event.Meta.PayloadLen])
Expand Down
2 changes: 1 addition & 1 deletion internal/writer/pcapng.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (w *PcapNGWriter) Write(e *event.Packet) error {
info := gopacket.CaptureInfo{
Timestamp: time.Now(),
CaptureLength: payloadLen,
Length: payloadLen,
Length: e.Len,
InterfaceIndex: e.Device.Ifindex,
}
p := w.pcache.Get(e.Pid)
Expand Down

0 comments on commit f9bbd57

Please sign in to comment.