Skip to content

Commit 9ff9c47

Browse files
authored
Merge pull request #41 from LakshK98/actual-stuff
Actual Pktmon struct
2 parents d715a19 + 75d881c commit 9ff9c47

4 files changed

Lines changed: 129 additions & 131 deletions

File tree

pkg/plugin/ebpfwindows/datapath_drop_windows.go

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ const (
1919

2020
const (
2121
// dropNotifyV1Len is the amount of packet data provided in a v0/v1 drop notification.
22-
dropNotifyV1Len = 36
22+
dropNotifyV1Len = 36
23+
dropPktmonNotifyV1Len = 57
2324
)
2425

2526
var dropNotifyLengthFromVersion = map[uint16]uint{
2627
DropNotifyVersion0: dropNotifyV1Len, // retain backwards compatibility for testing.
2728
DropNotifyVersion1: dropNotifyV1Len,
2829
}
2930

31+
// var PktmonDropNotifyLengthFromVersion = map[uint16]uint{
32+
// DropNotifyVersion0: dropPktmonNotifyV1Len, // retain backwards compatibility for testing.
33+
// }
34+
3035
var (
3136
errUnexpectedDropNotifyLength = errors.New("unexpected DropNotify data length")
3237
errInvalidDropNotifyVersion = errors.New("invalid DropNotify version")
@@ -50,21 +55,41 @@ type DropNotify struct {
5055
Ifindex uint32
5156
}
5257

58+
type NetEventDataHeader struct {
59+
Type uint8 // uint8_t type
60+
Version uint16 // uint16_t version
61+
}
62+
63+
type PktmonEvtStreamPacketDescriptor struct {
64+
PacketOriginalLength uint32 // uint32_t packet_original_length
65+
PacketLoggedLength uint32 // uint32_t packet_logged_length
66+
PacketMetadataLength uint32 // uint32_t packet_metadata_length
67+
}
68+
69+
type PktmonEvtStreamMetadata struct {
70+
PktGroupID uint64 // uint64_t pkt_groupid
71+
PktCount uint16 // uint16_t pkt_count
72+
AppearanceCount uint16 // uint16_t appearance_count
73+
DirectionName uint16 // uint16_t direction_name
74+
PacketType uint16 // uint16_t packet_type
75+
ComponentID uint16 // uint16_t component_id
76+
EdgeID uint16 // uint16_t edge_id
77+
FilterID uint16 // uint16_t filter_id
78+
DropReason uint32 // uint32_t drop_reason
79+
DropLocation uint32 // uint32_t drop_location
80+
ProcNum uint16 // uint16_t proc_num
81+
Timestamp uint64 // uint64_t timestamp
82+
}
83+
84+
type PktmonEvtStreamPacketHeader struct {
85+
EventID uint8 // uint8_t eventid
86+
PacketDescriptor PktmonEvtStreamPacketDescriptor // pktmon_evt_stream_packet_descriptor
87+
Metadata PktmonEvtStreamMetadata // pktmon_evt_stream_metadata
88+
}
89+
5390
type PktmonDropNotify struct {
54-
Type uint8
55-
Version uint16
56-
SubType uint8
57-
Source uint16
58-
Hash uint32
59-
OrigLen uint32
60-
CapLen uint16
61-
SrcLabel identity.NumericIdentity
62-
DstLabel identity.NumericIdentity
63-
DstID uint32
64-
Line uint16
65-
File uint8
66-
ExtError int8
67-
Ifindex uint32
91+
VersionHeader NetEventDataHeader // netevent_data_header_t version_header
92+
PktmonHeader PktmonEvtStreamPacketHeader // pktmon_evt_stream_packet_header pktmon_header
6893
}
6994

7095
// DecodeDropNotify will decode 'data' into the provided DropNotify structure
@@ -73,69 +98,47 @@ func DecodePktmonDrop(data []byte, dn *DropNotify) error {
7398
if err := pdn.decodePktmonDrop(data); err != nil {
7499
return err
75100
}
76-
pdn.Type = 1
77-
dn.Type = pdn.Type
78-
dn.SubType = pdn.SubType
79-
dn.Source = pdn.Source
80-
dn.Hash = pdn.Hash
81-
dn.OrigLen = pdn.OrigLen
82-
dn.CapLen = pdn.CapLen
83-
dn.Version = pdn.Version
84-
dn.SrcLabel = pdn.SrcLabel
85-
dn.DstLabel = pdn.DstLabel
86-
dn.DstID = pdn.DstID
87-
dn.Line = pdn.Line
88-
dn.File = pdn.File
89-
dn.ExtError = pdn.ExtError
90-
dn.Ifindex = pdn.Ifindex
101+
dn.Type = 1
102+
dn.SubType = uint8(pdn.PktmonHeader.Metadata.DropReason)
103+
dn.OrigLen = 128
104+
dn.CapLen = 128
105+
dn.Version = pdn.VersionHeader.Version
91106
return nil
92107
}
93108

94109
func (n *PktmonDropNotify) decodePktmonDrop(data []byte) error {
95-
if l := len(data); l < dropNotifyV1Len {
96-
return fmt.Errorf("%w: expected at least %d but got %d", errUnexpectedDropNotifyLength, dropNotifyV1Len, l)
110+
if l := len(data); l < dropPktmonNotifyV1Len {
111+
return fmt.Errorf("%w: expected at least %d but got %d", errUnexpectedDropNotifyLength, dropPktmonNotifyV1Len, l)
97112
}
98113
version := byteorder.Native.Uint16(data[2:4])
99114

100115
// Check against max version.
101116
if version > DropNotifyVersion1 {
102-
return fmt.Errorf("%w: Unrecognized pktmon drop event version %d\nRaw data bytes: %v\nData size: %d\nType: %d\nVersion (bytes 1-2): %v (uint16: %d)\nSubType: %d\nSource (bytes 4-5): %v (uint16: %d)\nHash (bytes 6-9): %v (uint32: %d)\nOrigLen (bytes 10-13): %v (uint32: %d)\nCapLen (bytes 14-15): %v (uint16: %d)\nSrcLabel (bytes 16-19): %v (uint32: %d)\nDstLabel (bytes 20-23): %v (uint32: %d)\nDstID (bytes 24-27): %v (uint32: %d)\nLine (bytes 28-29): %v (uint16: %d)\nFile (byte 30): %d\nExtError (byte 31): %d\nIfindex (bytes 32-35): %v (uint32: %d)",
117+
return fmt.Errorf("%w: Unrecognized pktmon drop event version %d\nRaw data bytes: %v\nData size: %d\nType: %d\n",
103118
errInvalidDropNotifyVersion, version,
104119
data,
105-
len(data),
106-
data[0],
107-
data[2:4], version,
108-
data[4],
109-
data[6:8], byteorder.Native.Uint16(data[6:8]),
110-
data[8:12], byteorder.Native.Uint32(data[8:12]),
111-
data[12:16], byteorder.Native.Uint32(data[12:16]),
112-
data[16:18], byteorder.Native.Uint16(data[16:18]),
113-
data[18:22], byteorder.Native.Uint32(data[20:24]),
114-
data[22:26], byteorder.Native.Uint32(data[24:28]),
115-
data[26:30], byteorder.Native.Uint32(data[28:32]),
116-
data[30:32], byteorder.Native.Uint16(data[32:34]),
117-
data[32],
118-
int8(data[33]),
119-
data[34:38], byteorder.Native.Uint32(data[34:38]),
120-
)
120+
len(data))
121121
}
122122

123123
// Decode logic for version >= v0/v1.
124-
n.Type = data[0]
125-
n.SubType = data[4]
126-
n.Source = byteorder.Native.Uint16(data[6:8])
127-
n.Hash = byteorder.Native.Uint32(data[8:12])
128-
n.OrigLen = byteorder.Native.Uint32(data[12:16])
129-
n.CapLen = byteorder.Native.Uint16(data[16:18])
130-
n.Version = version
131-
n.SrcLabel = identity.NumericIdentity(byteorder.Native.Uint32(data[20:24]))
132-
n.DstLabel = identity.NumericIdentity(byteorder.Native.Uint32(data[24:28]))
133-
n.DstID = byteorder.Native.Uint32(data[28:32])
134-
n.Line = byteorder.Native.Uint16(data[32:34])
135-
n.File = data[34]
136-
n.ExtError = int8(data[35])
137-
n.Ifindex = byteorder.Native.Uint32(data[36:40])
138-
124+
n.VersionHeader.Type = data[0]
125+
n.VersionHeader.Version = version
126+
n.PktmonHeader.EventID = data[4]
127+
n.PktmonHeader.PacketDescriptor.PacketOriginalLength = byteorder.Native.Uint32(data[6:10])
128+
n.PktmonHeader.PacketDescriptor.PacketLoggedLength = byteorder.Native.Uint32(data[10:14])
129+
n.PktmonHeader.PacketDescriptor.PacketMetadataLength = byteorder.Native.Uint32(data[14:18])
130+
n.PktmonHeader.Metadata.PktGroupID = byteorder.Native.Uint64(data[18:26])
131+
n.PktmonHeader.Metadata.PktCount = byteorder.Native.Uint16(data[26:28])
132+
n.PktmonHeader.Metadata.AppearanceCount = byteorder.Native.Uint16(data[28:30])
133+
n.PktmonHeader.Metadata.DirectionName = byteorder.Native.Uint16(data[30:32])
134+
n.PktmonHeader.Metadata.PacketType = byteorder.Native.Uint16(data[32:34])
135+
n.PktmonHeader.Metadata.ComponentID = byteorder.Native.Uint16(data[34:36])
136+
n.PktmonHeader.Metadata.EdgeID = byteorder.Native.Uint16(data[36:38])
137+
n.PktmonHeader.Metadata.FilterID = byteorder.Native.Uint16(data[38:40])
138+
n.PktmonHeader.Metadata.DropReason = byteorder.Native.Uint32(data[40:44])
139+
n.PktmonHeader.Metadata.DropLocation = byteorder.Native.Uint32(data[44:48])
140+
n.PktmonHeader.Metadata.ProcNum = byteorder.Native.Uint16(data[48:50])
141+
n.PktmonHeader.Metadata.Timestamp = byteorder.Native.Uint64(data[50:58])
139142
return nil
140143
}
141144

pkg/plugin/ebpfwindows/parser_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func (p *Parser) decode(data []byte, decoded *pb.Flow) error {
205205
}
206206
slog.Info("Pktmon DropNotify", "DropNotify", dn)
207207
eventSubType = dn.SubType
208-
offset = 40
208+
offset = 57
209209
if offset > uint(MaxInt) {
210210
return fmt.Errorf("%w: %d", errDataOffsetTooLarge, offset)
211211
}

test/e2e/tools/event-writer/bpf_event_writer.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,11 @@ void create_drop_event(struct drop_notify* drp_elm)
129129
drp_elm->ifindex = 0;
130130
}
131131

132-
void create_pktmon_drop_event(struct pktmon_notify* drp_elm)
132+
void create_pktmon_drop_event(pktmon_notify_t* pktmon_drp_elm)
133133
{
134-
memset(drp_elm, 0, sizeof(struct pktmon_notify));
135-
drp_elm->type = PKTMON_NOTIFY_DROP;
136-
drp_elm->subtype = 7;
137-
drp_elm->source = 11; // random source
138-
drp_elm->hash = 0;
139-
drp_elm->len_orig = 128;
140-
drp_elm->len_cap = 128;
141-
drp_elm->version = 1;
142-
drp_elm->src_label = 0;
143-
drp_elm->dst_label = 0;
144-
drp_elm->dst_id = 0;
145-
drp_elm->line = 0;
146-
drp_elm->file = 0;
147-
drp_elm->ext_error = 0;
148-
drp_elm->ifindex = 1;
134+
memset(pktmon_drp_elm, 0, sizeof(pktmon_notify_t));
135+
pktmon_drp_elm->version_header.type = PKTMON_NOTIFY_DROP;
136+
pktmon_drp_elm->pktmon_header.metadata.drop_reason = 7;
149137
}
150138

151139
int

test/e2e/tools/event-writer/event_writer.h

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,23 @@ struct drop_notify {
167167
};
168168

169169

170-
struct pktmon_notify {
171-
uint8_t type;
172-
uint16_t version;
173-
uint8_t subtype;
174-
uint16_t source;
175-
uint32_t hash;
176-
uint32_t len_orig;
177-
uint16_t len_cap;
178-
uint32_t src_label;
179-
uint32_t dst_label;
180-
uint32_t dst_id; /* 0 for egress */
181-
uint16_t line;
182-
uint8_t file;
183-
int8_t ext_error;
184-
uint32_t ifindex;
185-
uint8_t data[128];
186-
};
170+
// struct pktmon_notify {
171+
// uint8_t type;
172+
// uint16_t version;
173+
// uint8_t subtype;
174+
// uint16_t source;
175+
// uint32_t hash;
176+
// uint32_t len_orig;
177+
// uint16_t len_cap;
178+
// uint32_t src_label;
179+
// uint32_t dst_label;
180+
// uint32_t dst_id; /* 0 for egress */
181+
// uint16_t line;
182+
// uint8_t file;
183+
// int8_t ext_error;
184+
// uint32_t ifindex;
185+
// uint8_t data[128];
186+
// };
187187

188188
// _Static_assert(sizeof(struct pktmon_notify) == 168, "pktmon_notify struct size must be 168 bytes");
189189

@@ -223,45 +223,52 @@ enum _PKTMON_DIRECTION_TAG
223223
PktMonDirTag_Egress
224224
} PKTMON_DIRECTION_TAG;
225225

226+
typedef struct _netevent_data_header
227+
{
228+
uint8_t type;
229+
uint16_t version;
230+
} netevent_data_header_t;
231+
226232
#pragma pack(push, 1)
227233

228-
/* Packet descriptor used for event streaming */
229-
typedef struct _PKTMON_EVT_STREAM_PACKET_DESCRIPTOR
234+
/* packet descriptor used for event streaming */
235+
typedef struct _pktmon_evt_stream_packet_descriptor
230236
{
231-
uint32_t PacketOriginalLength;
232-
uint32_t PacketLoggedLength;
233-
uint32_t PacketMetaDataLength;
234-
} PKTMON_EVT_STREAM_PACKET_DESCRIPTOR;
237+
uint32_t packet_original_length;
238+
uint32_t packet_logged_length;
239+
uint32_t packet_metadata_length;
240+
} pktmon_evt_stream_packet_descriptor;
235241

236-
/* Metadata information used for event streaming */
237-
typedef struct _PKTMON_EVT_STREAM_METADATA
242+
/* metadata information used for event streaming */
243+
typedef struct _pktmon_evt_stream_metadata
238244
{
239-
uint64_t PktGroupId;
240-
uint16_t PktCount;
241-
uint16_t AppearanceCount;
242-
uint16_t DirectionName;
243-
uint16_t PacketType;
244-
uint16_t ComponentId;
245-
uint16_t EdgeId;
246-
uint16_t FilterId;
247-
uint32_t DropReason;
248-
uint32_t DropLocation;
249-
uint16_t ProcNum;
250-
uint64_t TimeStamp;
251-
} PKTMON_EVT_STREAM_METADATA;
252-
253-
/* Packet header used for event streaming */
254-
typedef struct _PKTMON_EVT_STREAM_PACKET_HEADER
245+
uint64_t pkt_groupid;
246+
uint16_t pkt_count;
247+
uint16_t appearance_count;
248+
uint16_t direction_name;
249+
uint16_t packet_type;
250+
uint16_t component_id;
251+
uint16_t edge_id;
252+
uint16_t filter_id;
253+
uint32_t drop_reason;
254+
uint32_t drop_location;
255+
uint16_t proc_num;
256+
uint64_t timestamp;
257+
} pktmon_evt_stream_metadata;
258+
259+
/* packet header used for event streaming */
260+
typedef struct _pktmon_evt_stream_packet_header
255261
{
256-
uint8_t EventId;
257-
PKTMON_EVT_STREAM_PACKET_DESCRIPTOR PacketDescriptor;
258-
PKTMON_EVT_STREAM_METADATA Metadata;
259-
} PKTMON_EVT_STREAM_PACKET_HEADER;
260-
261-
// typedef struct pktmon_notify {
262-
// PKTMON_EVT_STREAM_PACKET_HEADER header;
263-
// uint8_t data[128];
264-
// } pktmon_notify_t;
262+
uint8_t eventid;
263+
pktmon_evt_stream_packet_descriptor packet_descriptor;
264+
pktmon_evt_stream_metadata metadata;
265+
} pktmon_evt_stream_packet_header;
266+
267+
typedef struct pktmon_notify {
268+
netevent_data_header_t version_header;
269+
pktmon_evt_stream_packet_header pktmon_header;
270+
uint8_t data[128];
271+
} pktmon_notify_t;
265272

266273
#pragma pack(pop)
267274

0 commit comments

Comments
 (0)