Skip to content

Commit 35398d6

Browse files
committed
Restore L2 drops
1 parent 2a848d8 commit 35398d6

File tree

3 files changed

+103
-59
lines changed

3 files changed

+103
-59
lines changed

Diff for: pkg/decode/decode_protobuf.go

+27-31
Original file line numberDiff line numberDiff line change
@@ -67,40 +67,36 @@ func PBFlowToMap(flow *pbflow.Record) config.GenericMap {
6767
}
6868

6969
ethType := ethernet.EtherType(flow.EthProtocol)
70+
if ethType == ethernet.EtherTypeIPv4 || ethType == ethernet.EtherTypeIPv6 {
71+
out["SrcAddr"] = ipToStr(flow.Network.GetSrcAddr())
72+
out["DstAddr"] = ipToStr(flow.Network.GetDstAddr())
73+
out["Proto"] = flow.Transport.GetProtocol()
74+
out["Dscp"] = flow.Network.GetDscp()
75+
proto := flow.Transport.GetProtocol()
76+
if proto == syscall.IPPROTO_ICMP || proto == syscall.IPPROTO_ICMPV6 {
77+
out["IcmpType"] = flow.GetIcmpType()
78+
out["IcmpCode"] = flow.GetIcmpCode()
79+
}
7080

71-
if ethType != ethernet.EtherTypeIPv4 && ethType != ethernet.EtherTypeIPv6 {
72-
// e.g. ARP
73-
return out
74-
}
75-
76-
out["SrcAddr"] = ipToStr(flow.Network.GetSrcAddr())
77-
out["DstAddr"] = ipToStr(flow.Network.GetDstAddr())
78-
out["Proto"] = flow.Transport.GetProtocol()
79-
out["Dscp"] = flow.Network.GetDscp()
80-
proto := flow.Transport.GetProtocol()
81-
if proto == syscall.IPPROTO_ICMP || proto == syscall.IPPROTO_ICMPV6 {
82-
out["IcmpType"] = flow.GetIcmpType()
83-
out["IcmpCode"] = flow.GetIcmpCode()
84-
}
85-
86-
if proto == syscall.IPPROTO_TCP || proto == syscall.IPPROTO_UDP || proto == syscall.IPPROTO_SCTP {
87-
if proto == syscall.IPPROTO_TCP {
88-
out["SrcPort"] = flow.Transport.GetSrcPort()
89-
out["DstPort"] = flow.Transport.GetDstPort()
90-
out["Flags"] = flow.Flags
91-
} else {
92-
out["SrcPort"] = flow.Transport.GetSrcPort()
93-
out["DstPort"] = flow.Transport.GetDstPort()
81+
if proto == syscall.IPPROTO_TCP || proto == syscall.IPPROTO_UDP || proto == syscall.IPPROTO_SCTP {
82+
if proto == syscall.IPPROTO_TCP {
83+
out["SrcPort"] = flow.Transport.GetSrcPort()
84+
out["DstPort"] = flow.Transport.GetDstPort()
85+
out["Flags"] = flow.Flags
86+
} else {
87+
out["SrcPort"] = flow.Transport.GetSrcPort()
88+
out["DstPort"] = flow.Transport.GetDstPort()
89+
}
9490
}
95-
}
9691

97-
out["DnsErrno"] = flow.GetDnsErrno()
98-
if flow.GetDnsId() != 0 {
99-
out["DnsLatencyMs"] = flow.DnsLatency.AsDuration().Milliseconds()
100-
out["DnsId"] = flow.GetDnsId()
101-
out["DnsFlags"] = flow.GetDnsFlags()
102-
out["DnsFlagsResponseCode"] = DNSRcodeToStr(flow.GetDnsFlags() & 0xF)
103-
out["DnsErrno"] = uint32(0)
92+
out["DnsErrno"] = flow.GetDnsErrno()
93+
if flow.GetDnsId() != 0 {
94+
out["DnsLatencyMs"] = flow.DnsLatency.AsDuration().Milliseconds()
95+
out["DnsId"] = flow.GetDnsId()
96+
out["DnsFlags"] = flow.GetDnsFlags()
97+
out["DnsFlagsResponseCode"] = DNSRcodeToStr(flow.GetDnsFlags() & 0xF)
98+
out["DnsErrno"] = uint32(0)
99+
}
104100
}
105101

106102
if flow.GetPktDropLatestDropCause() != 0 {

Diff for: pkg/exporter/convert_flp.go

+25-28
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,35 @@ func ConvertToFLP(fr *flow.Record) config.GenericMap {
3838
out["Packets"] = fr.Metrics.Packets
3939
}
4040

41-
if fr.Id.EthProtocol != uint16(ethernet.EtherTypeIPv4) && fr.Id.EthProtocol != uint16(ethernet.EtherTypeIPv6) {
42-
// e.g. ARP
43-
return out
44-
}
45-
46-
out["SrcAddr"] = flow.IP(fr.Id.SrcIp).String()
47-
out["DstAddr"] = flow.IP(fr.Id.DstIp).String()
48-
out["Proto"] = fr.Id.TransportProtocol
49-
out["Dscp"] = fr.Metrics.Dscp
41+
if fr.Id.EthProtocol == uint16(ethernet.EtherTypeIPv4) || fr.Id.EthProtocol == uint16(ethernet.EtherTypeIPv6) {
42+
out["SrcAddr"] = flow.IP(fr.Id.SrcIp).String()
43+
out["DstAddr"] = flow.IP(fr.Id.DstIp).String()
44+
out["Proto"] = fr.Id.TransportProtocol
45+
out["Dscp"] = fr.Metrics.Dscp
5046

51-
if fr.Id.TransportProtocol == syscall.IPPROTO_ICMP || fr.Id.TransportProtocol == syscall.IPPROTO_ICMPV6 {
52-
out["IcmpType"] = fr.Id.IcmpType
53-
out["IcmpCode"] = fr.Id.IcmpCode
54-
} else if fr.Id.TransportProtocol == syscall.IPPROTO_TCP || fr.Id.TransportProtocol == syscall.IPPROTO_UDP || fr.Id.TransportProtocol == syscall.IPPROTO_SCTP {
55-
out["SrcPort"] = fr.Id.SrcPort
56-
out["DstPort"] = fr.Id.DstPort
57-
if fr.Id.TransportProtocol == syscall.IPPROTO_TCP {
58-
out["Flags"] = fr.Metrics.Flags
47+
if fr.Id.TransportProtocol == syscall.IPPROTO_ICMP || fr.Id.TransportProtocol == syscall.IPPROTO_ICMPV6 {
48+
out["IcmpType"] = fr.Id.IcmpType
49+
out["IcmpCode"] = fr.Id.IcmpCode
50+
} else if fr.Id.TransportProtocol == syscall.IPPROTO_TCP || fr.Id.TransportProtocol == syscall.IPPROTO_UDP || fr.Id.TransportProtocol == syscall.IPPROTO_SCTP {
51+
out["SrcPort"] = fr.Id.SrcPort
52+
out["DstPort"] = fr.Id.DstPort
53+
if fr.Id.TransportProtocol == syscall.IPPROTO_TCP {
54+
out["Flags"] = fr.Metrics.Flags
55+
}
5956
}
60-
}
6157

62-
out["DnsErrno"] = fr.Metrics.DnsRecord.Errno
63-
dnsID := fr.Metrics.DnsRecord.Id
64-
if dnsID != 0 {
65-
out["DnsId"] = dnsID
66-
out["DnsFlags"] = fr.Metrics.DnsRecord.Flags
67-
out["DnsFlagsResponseCode"] = decode.DNSRcodeToStr(uint32(fr.Metrics.DnsRecord.Flags) & 0xF)
68-
if fr.Metrics.DnsRecord.Latency != 0 {
69-
out["DnsLatencyMs"] = fr.DNSLatency.Milliseconds()
58+
out["DnsErrno"] = fr.Metrics.DnsRecord.Errno
59+
dnsID := fr.Metrics.DnsRecord.Id
60+
if dnsID != 0 {
61+
out["DnsId"] = dnsID
62+
out["DnsFlags"] = fr.Metrics.DnsRecord.Flags
63+
out["DnsFlagsResponseCode"] = decode.DNSRcodeToStr(uint32(fr.Metrics.DnsRecord.Flags) & 0xF)
64+
if fr.Metrics.DnsRecord.Latency != 0 {
65+
out["DnsLatencyMs"] = fr.DNSLatency.Milliseconds()
66+
}
67+
// Not sure about the logic here, why erasing errno?
68+
out["DnsErrno"] = uint32(0)
7069
}
71-
// Not sure about the logic here, why erasing errno?
72-
out["DnsErrno"] = uint32(0)
7370
}
7471

7572
if fr.Metrics.PktDrops.LatestDropCause != 0 {

Diff for: pkg/exporter/converters_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,57 @@ func TestConversions(t *testing.T) {
260260
"AgentIP": "10.11.12.13",
261261
},
262262
},
263+
{
264+
name: "L2 drops",
265+
flow: &flow.Record{
266+
RawRecord: flow.RawRecord{
267+
Id: ebpf.BpfFlowId{
268+
EthProtocol: 2054, // ARP protocol
269+
Direction: flow.DirectionEgress,
270+
SrcMac: flow.MacAddr{0x04, 0x05, 0x06, 0x07, 0x08, 0x09},
271+
DstMac: flow.MacAddr{0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
272+
SrcIp: flow.IPAddr{},
273+
DstIp: flow.IPAddr{},
274+
SrcPort: 0,
275+
DstPort: 0,
276+
TransportProtocol: 0,
277+
},
278+
Metrics: ebpf.BpfFlowMetrics{
279+
Bytes: 500,
280+
Packets: 128,
281+
PktDrops: ebpf.BpfPktDropsT{
282+
Packets: 10,
283+
Bytes: 100,
284+
LatestFlags: 0x200,
285+
LatestState: 0,
286+
LatestDropCause: 2,
287+
},
288+
},
289+
},
290+
Interface: "eth0",
291+
TimeFlowStart: someTime,
292+
TimeFlowEnd: someTime,
293+
AgentIP: net.IPv4(0x0a, 0x0b, 0x0c, 0x0d),
294+
},
295+
expected: &config.GenericMap{
296+
"FlowDirection": 1,
297+
"Bytes": 500,
298+
"DstMac": "0A:0B:0C:0D:0E:0F",
299+
"SrcMac": "04:05:06:07:08:09",
300+
"Duplicate": false,
301+
"Etype": 2054,
302+
"Packets": 128,
303+
"TimeFlowStartMs": someTime.UnixMilli(),
304+
"TimeFlowEndMs": someTime.UnixMilli(),
305+
"Interface": "eth0",
306+
"AgentIP": "10.11.12.13",
307+
"PktDropBytes": 100,
308+
"PktDropPackets": 10,
309+
"PktDropLatestFlags": 0x200,
310+
"PktDropLatestState": "TCP_INVALID_STATE",
311+
"PktDropLatestDropCause": "SKB_DROP_REASON_NOT_SPECIFIED",
312+
},
313+
},
263314
{
264315
name: "TCP + drop + DNS + RTT record",
265316
flow: &flow.Record{

0 commit comments

Comments
 (0)